When nginx deploys multiple tomcat applications (application A, application B), session conflicts lead to a successful login, and the session expiration of other tomcats

出问题的配置:
        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";
        }

Problem phenomenon:

1. Log in to http://ip:port/app1/ and you can log in successfully;

2. Log in to http://ip:port/app2/ again and you can log in successfully, but app1 will prompt timeout (session expired);

3. It is found that after app2 logs in, the cookie value SESSION under the directory will change, causing app1 to prompt that the session has expired;

4. Conversely: logging in to app2 first, and then logging in to app1 will also cause the session of app2 to expire;

solution:

Add the configuration of the cookie path in the location section of the respective App:

1. On 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. On 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. The settings will generate corresponding sessions in their respective directories;

4. After restarting nginx, both app1 and app2 can log in normally, the sessions do not affect each other, and the problem is solved;

 

Guess you like

Origin blog.csdn.net/songchaofly/article/details/120881559