windows 下 nginx + tomcat + redis 叢集實作session共享
nginx:nginx-1.14.0
redis : Redis-x64-3.2.100
tomcat 版本:apache-tomcat-6.0.32
nginx 配置:
# user admin;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
#access_log logs/access.log main;
sendfile on;
tcp_nopush on; #防止網絡阻塞
keepalive_timeout 60s;
#gzip on;
upstream backend_server{
server 192.168.88.1:8081 weight=1 max_fails=1 fail_timeout=10s;
server 192.168.88.1:8082 weight=1 max_fails=1 fail_timeout=10s;
server 192.168.88.1:8083 weight=1 max_fails=1 fail_timeout=10s;
keepalive 100;
}
server {
listen 8090;
server_name 192.168.88.128;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
location /test/{
#Proxy Settings
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_connect_timeout 3s; #預設值60s, nginx連接配接到後端伺服器的連接配接逾時時間
proxy_http_version 1.1;
proxy_set_header Connection "";
proxy_pass http://backend_server;
}
# 狀态監控子產品
location /status{
stub_status on;
access_log off;
allow 192.168.88.1;
deny all;
}
}
}
redis 配置
修訂redis.windows.conf
# 端口綁定
bind 0.0.0.0
# 資料存儲
save ""
# 最大記憶體
maxmemory 512M
# 最大記憶體政策
maxmemory-policy allkeys-lru
tomcat 配置:
部署3個tomcat
redis 依賴包
放置在tomcat/lib 目錄下
tomcat-redis-session-manager-1.2-tomcat-6.jar
commons-pool-1.6.jar
jedis-2.1.0.jar
百度位址:https://pan.baidu.com/s/1nEyidPqp9ffHhiolWzDlTw
配置context.xml
<Valve className="com.radiadesign.catalina.session.RedisSessionHandlerValve" />
<Manager className="com.radiadesign.catalina.session.RedisSessionManager"
host="127.0.0.1"
port="6379"
database="0"
maxInactiveInterval="1800"/>
配置server.xml
Server SHUTDOWN port:8081,8082,8083
Connector HTTP/1.1 :8081,8082,8083
Connector AJP/1.3 :8019,8029,8039
分别在webapps下建立test目錄,建立檔案 index.jsp,内容如下:
<%@ page language="java" pageEncoding="GBK"%>
<html>
<head><title>session test</title></head>
<body>
<h1><font color="blue">Tomcat Server</h1>
<table align="centre" >
<tr>
<td>Session ID</td>
<% session.setAttribute("my.name","zhangSan"); %>
<td><%= session.getId() %></td>
</tr>
<tr>
<td>Server IP</td>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getLocalAddr() +":"+request.getLocalPort()+path+"/";
%>
<td><%=basePath%></td>
</tr>
<tr>
<td>Created on</td>
<td><%= session.getCreationTime() %></td>
</tr>
</table>
</body>
</html>
按順序啟動服務
redis
nginx
tomcat
session 同步測試
浏覽器打開位址,如果session id一緻,則配置成功
http://192.168.88.128:8090/test/index.jsp
