解决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了。
相关文章
相关标签/搜索