Nginx 反向代理以及 cookie
proxy_cookie_path 语法
proxy_cookie_path source target;
source 源路径
target 目标路径
使用原因
cookie 的 path 与地址栏上的 path 不一致
浏览器就不会接受这个 cookie,无法传入 JSESSIONID 的 cookie
导致登录验证失败
使用场景
当 nginx 配置的反向代理的路径和源地址路径不一致时使用
使用 Demo
upstream tomcatserver1 {
server localhost:5173;
}
upstream tomcatserver2 {
server 127.0.0.1:84;
}
server {
listen 80;
server_name d.com;
location /ws/ {
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://tomcatserver2/;
proxy_cookie_path / /ws/;
proxy_set_header Cookie $http_cookie;
index index.html index.htm;
}
location / {
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://tomcatserver1;
proxy_set_header Cookie $http_cookie;
index index.html index.htm;
proxy_cookie_path / /ws/;
proxy_set_header Cookie $http_cookie;
}
}

