使用Redis存储Nginx+Tomcat负载均衡集群的Session (笔记)

引用:http://blog.csdn.net/xlgen157387/article/details/52024139

注意事项:

nginx配置文件nginx.conf:

 upstream mynginxserver {
    #weigth参数表示权值,权值越高被分配到的几率越大
    #本机上的Squid开启3128端口
        server 127.0.0.1:8080 weight=1;
        server 127.0.0.1:8060 weight=1;
    }

    server {
        listen       80;

  #注意这地方是你本机的ip地址,不能随意写,我之前测试以为可以随意指定呢
        server_name  192.168.22.38;
        #access_log  logs/host.access.log  main;

        location / {
            #root   html;
            #index  index.html index.htm;
            proxy_pass http://mynginxserver;
        }

tomcat配置文件context.xml:

<!-- tomcat-redis-session -->
    <Valve className="com.orangefunction.tomcat.redissessions.RedisSessionHandlerValve" />  
        <Manager className="com.orangefunction.tomcat.redissessions.RedisSessionManager"

    #注意该ip地址为redis配置的host地址,否则会出现拒绝连接(我之前就是配置的192.168.22.38,出现问题百思不得其解,后来发现是redis host配置为“127.0.0.1”)
         host="127.0.0.1"   
         port="6379"   
         database="0"   
         maxInactiveInterval="60" />

总结:nginx 同过server_name 配置访问地址,然后通过proxy_pass http://mynginxserver,实现负载均衡,在访问tomcat应用获取session时通过RedisSessionManager去redis数据库中查找;

即:访问redis需要host="127.0.0.1" 和redis保持一致

想的都是好
原文地址:https://www.cnblogs.com/freezone/p/8297598.html