Nginx session missing problem handling solution
- 2020-05-24 06:47:52
- OfStack
The session missing problem can occur in the path of a reverse proxy tomcat using nginx. Every time you send a request, JESSIONID changes, meaning that the session formed in the previous request is lost, and a new session is created.
Case 1:
server{
listen 80;
server_name www.jiahemdata.com www.jiahemdata.cn;
charset utf-8;
location /{
proxy_redirect off;
proxy_pass http://127.0.0.1:8093;
proxy_set_header Host $host;
proxy_set_header Referer $http_referer ;
proxy_set_header X-Real-Ip $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
access_log logs/tomcat_access.log;
}
Since the current nginx is only listening on one port and no path is set, the problem of session loss will not occur in 1.
Case 2:
server{
listen 80;
server_name www.jiahemdata.com www.jiahemdata.cn;
root /opt/tomcat-jhyx/webapps/jhyx/;
charset utf-8;
location /{
proxy_pass http://127.0.0.1:8093/jhyx/;
proxy_set_header Host $host;
proxy_set_header Referer $http_referer ;
proxy_set_header X-Real-Ip $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
access_log logs/tomcat_access.log;
}
In this case, specifying a folder for tomcat, rather than just one port listener, will cause each request to change, causing session to be lost.
Case 3:
server{
listen 80;
server_name www.jiahemdata.com www.jiahemdata.cn;
root /opt/tomcat-jhyx/webapps/jhyx/;
charset utf-8;
location /{
proxy_redirect off;
proxy_pass http://127.0.0.1:8093/jhyx/;
proxy_cookie_path /jhyx/ /; // Set up the cookie Path so that it does not change every time a request occurs.
proxy_cookie_path /jhyx /;
proxy_set_header Host $host;
proxy_set_header Referer $http_referer;
proxy_set_header X-Real-Ip $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
access_log logs/tomcat_access.log;
}
At this moment, you find that your problem is still unresolved. At this moment, you are thinking, I have already set the cookie path, why not? That is because you did not send cookie when you asked.
Case 4:
server{
listen 80;
server_name www.jiahemdata.com www.jiahemdata.cn;
root /opt/tomcat-jhyx/webapps/jhyx/;
charset utf-8;
location /{
proxy_redirect off;
proxy_pass http://127.0.0.1:8093/jhyx/;
proxy_cookie_path /jhyx/ /;
proxy_cookie_path /jhyx /;
proxy_set_header Host $host;
proxy_set_header Referer $http_referer;
proxy_set_header Cookie $http_cookie; // Request is sent with cookie information
proxy_set_header X-Real-Ip $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
access_log logs/tomcat_access.log;
}
I hope you find a correct solution in the vast network.