在用nginx的反向代理tomcat的路徑中,可能會出現session丟失問題。每發送一次請求 JESSIONID 都會發生改變,說明上一次形成的session丟失,從而創建新的session。
第一種情況:
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;
}
由于當前對的nginx只是監聽一個端口,不設定路徑,所有一般不會出現session丟失的問題。
第二種情況:
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;
}
這種情況,指定了tomcat的文件夾,不僅僅是一個端口監聽,會導致每次請求都會發生變化,導致session丟失。
第三種情況:
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/ /; //設置cookie路徑,從而不導致每次發生請求發生變化。
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;
}
這時候,發現你的問題依然沒有解決,這時候你在想,我明明已經設置cookie路徑了,怎么還不行呢,那是因為你請求的時候沒有發送cookie。
第四種情況:
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; //請求發送時攜帶cookie信息
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;
}
希望你在茫茫網絡,找到一個正確的解決方法。
到此這篇關于Nginx session丟失問題處理解決方法的文章就介紹到這了,更多相關Nginx session丟失內容請搜索腳本之家以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持腳本之家!