在用nginx的反向代理tomcat的路徑中,可能會(huì)出現(xiàn)session丟失問題。每發(fā)送一次請(qǐng)求 JESSIONID 都會(huì)發(fā)生改變,說明上一次形成的session丟失,從而創(chuàng)建新的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;
}
由于當(dāng)前對(duì)的nginx只是監(jiān)聽一個(gè)端口,不設(shè)定路徑,所有一般不會(huì)出現(xiàn)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的文件夾,不僅僅是一個(gè)端口監(jiān)聽,會(huì)導(dǎo)致每次請(qǐng)求都會(huì)發(fā)生變化,導(dǎo)致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/ /; //設(shè)置cookie路徑,從而不導(dǎo)致每次發(fā)生請(qǐng)求發(fā)生變化。
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;
}
這時(shí)候,發(fā)現(xiàn)你的問題依然沒有解決,這時(shí)候你在想,我明明已經(jīng)設(shè)置cookie路徑了,怎么還不行呢,那是因?yàn)槟阏?qǐng)求的時(shí)候沒有發(fā)送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; //請(qǐng)求發(fā)送時(shí)攜帶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;
}
希望你在茫茫網(wǎng)絡(luò),找到一個(gè)正確的解決方法。
到此這篇關(guān)于Nginx session丟失問題處理解決方法的文章就介紹到這了,更多相關(guān)Nginx session丟失內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!