nginx部署多个tomcat应用时(应用A、应用B),session冲突导致一个登录成功,其他tomcat的Session过期问题

出问题的配置:
        location ^~ /app1 {
            proxy_pass  http://127.0.0.1:8080/;
            index  index index.html index.htm index.jsp;
            proxy_set_header   Host             $host;
            proxy_set_header   X-Real-IP        $remote_addr;
            proxy_set_header   X-Forwarded-For  $remote_addr;  # $proxy_add_x_forwarded_for;
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection "Upgrade";
        }

        location ^~ /app2 {
            proxy_pass  http://127.0.0.1:8082/;
            index  index index.html index.htm index.jsp;
            proxy_set_header   Host             $host;
            proxy_set_header   X-Real-IP        $remote_addr;
            proxy_set_header   X-Forwarded-For  $remote_addr;  # $proxy_add_x_forwarded_for;
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection "Upgrade";
        }

问题现象:

1、登录http://ip:port/app1/可以登录成功;

2、再登录http://ip:port/app2/  也可以登录成功,但是app1就会提示超时(session过期);

3、发现app2登录后,跟目录下的cookie值SESSION就会变化,导致app1提示session过期;

4、反之:先登录app2,再登录app1也会导致app2的session过期;

解决方案:

在各自App的location段增加cookie路径的配置:

1、在app1上:

        location ^~ /app1 {
            proxy_pass  http://127.0.0.1:8080/;
            index  index index.html index.htm index.jsp;
            proxy_set_header   Host             $host;
            proxy_set_header   X-Real-IP        $remote_addr;
            proxy_set_header   X-Forwarded-For  $remote_addr;  # $proxy_add_x_forwarded_for;
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection "Upgrade";

            proxy_set_header Cookie $http_cookie;
            proxy_cookie_path /    /app1/;

        }

2、在app2上:

location ^~ /app2 {
            proxy_pass  http://127.0.0.1:8082/;
            index  index index.html index.htm index.jsp;
            proxy_set_header   Host             $host;
            proxy_set_header   X-Real-IP        $remote_addr;
            proxy_set_header   X-Forwarded-For  $remote_addr;  # $proxy_add_x_forwarded_for;
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection "Upgrade";

            proxy_set_header Cookie $http_cookie;
            proxy_cookie_path  /    /app2/;

        }

3、设置会在各自的目录下生成对应的session;

4、重启nginx后,app1和app2都可以正常登录,session相互不影响,问题解决;

猜你喜欢

转载自blog.csdn.net/songchaofly/article/details/120881559