解决nginx使用proxy_pass反向代理时,session,cookie丢失的问题

https://www.cnblogs.com/zangdalei/p/6021352.html

 这2天在测试Nginx作为反向代理到Tomcat应用时,session丢失的问题。经过一系列查看官方文档和测试,发现如下:
1、如果只是host、端口转换,则session不会丢失。例如:
      location /testwx {
             proxy_pass   http://127.0.0.1:8080/testwx;
      }
      通过浏览器访问http://127.0.0.1/testwx时,浏览器的cookie内有jsessionid。再次访问时,浏览器会发送当前的cookie。
2、如果路径也变化了,则需要设置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了。

猜你喜欢

转载自blog.csdn.net/michaelwubo/article/details/81216991
今日推荐