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

1. 如果只是host、端口转换,则cookie不会丢失。例如:
    location /project {
        proxy_pass   http://127.0.0.1:8080/project;

    }

 

通过浏览器访问http://127.0.0.1/project时,浏览器的cookie内有jsessionid。再次访问时,浏览器会发送当前的cookie。


2. 如果路径也变化了,则需要设置cookie的路径转换,nginx.conf的配置如下
    location /proxy_path {
        proxy_pass   http://127.0.0.1:8080/project;

    }

 

通过浏览器访问http://127.0.0.1/proxy_path时,浏览器的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  /project /proxy_path;

则可以将project的cookie输出到proxy_path上。正确的配置是:

 

    location /proxy_path {
        proxy_pass   http://127.0.0.1:8080/project;
        proxy_cookie_path  /project /proxy_path;
    }

posted @ 2016-12-06 10:34  shiningrise  阅读(2176)  评论(0编辑  收藏  举报