解決nginx使用proxy_pass反向代理時,session丟失的問題

 
 這2天在測試Nginx做爲反向代理到Tomcat應用時,session丟失的問題。通過一系列查看官方文檔和測試,發現以下:
一、若是隻是host、端口轉換,則session不會丟失。例如:
      location /testwx {
             proxy_pass    http://127.0.0.1:8080/testwx;
      }
      經過瀏覽器訪問 http://127.0.0.1/testwx時,瀏覽器的cookie內有jsessionid。再次訪問時,瀏覽器會發送當前的cookie。
二、若是路徑也變化了,則須要設置cookie的路徑轉換,nginx.conf的配置以下
      location /testwx {
             proxy_pass    http://127.0.0.1:8080/wx;
      }
      經過瀏覽器訪問 http://127.0.0.1/testwx時,瀏覽器的cookie內沒有jsessionid。再次訪問時,後臺固然沒法獲取到cookie了。
      詳細看了文檔: http://nginx.org/en/docs/http/ngx_http_proxy_module.html?&_ga=1.161910972.1696054694.1422417685#proxy_cookie_path
     加上路徑轉換:proxy_cookie_path  /wx /testwx;則能夠將wx的cookie輸出到testwx上,Tomcat的session正常了。正確的配置是:
    
      location /testwx {
             proxy_pass    http://127.0.0.1:8080/wx;
             proxy_cookie_path  /wx /testwx;#這裏的路徑要注意對應關係
      }
能夠經過微軟應用商城實現一鍵部署 market.azure.cn

      若是須要更復雜的路徑轉換可用通配符的方式進行轉換,詳情要查看 http://nginx.org/en/docs/http/ngx_http_proxy_module.html?&_ga=1.161910972.1696054694.1422417685#proxy_cookie_path了。
相關文章
相關標籤/搜索