天天看点

windows 下 nginx + tomcat + redis 集群实现session共享windows 下 nginx + tomcat + redis 集群实现session共享nginx 配置:redis 配置tomcat 配置:

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

windows 下 nginx + tomcat + redis 集群实现session共享windows 下 nginx + tomcat + redis 集群实现session共享nginx 配置:redis 配置tomcat 配置:
windows 下 nginx + tomcat + redis 集群实现session共享windows 下 nginx + tomcat + redis 集群实现session共享nginx 配置:redis 配置tomcat 配置:
windows 下 nginx + tomcat + redis 集群实现session共享windows 下 nginx + tomcat + redis 集群实现session共享nginx 配置:redis 配置tomcat 配置: