nginx 反向代理后台web服務器cookiepath導致的session 失效,特此總結下配置方法:
配置:
location ^~ /2016tyjf_dev/djwechat {
proxy_pass http://192.168.23.108/;
proxy_redirect http://192.168.23.108 http://wx.zone139.com/2016tyjf_dev/djwechat/;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header Cookie $http_cookie;
proxy_cookie_path / /2016tyjf_dev/djwechat;
}
說明:
1. proxy_pass 要轉發后台web服務器的地址
2. proxy_redirect web后台返回301 http頭字段的location 改寫(https://blog.csdn.net/u010391029/article/details/50395680)
3. proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
配置上面的主要目的是讓web 后台服務器獲取真正的客戶端地址。
4. proxy_set_header Cookie $http_cookie; 設置 代理cookie http 頭。
5. proxy_cookie_path / /2016tyjf_dev/djwechat; 設置代理替換 cookie path
原理:
Nginx是http協議工作層轉發,cookie 對cookiepath路徑敏感,只有在cookiepath 內的屬性才可以保持會話不被重新生成
注意:
Nginx 使用路徑路由,如果需要對內部web 服務器地址隱藏,使用上面路徑協議的轉發,cookiepath 也必須對應
---------------------------------------------------------------------------------------------
~~ 如有問題,還望各位不吝賜教