如果后方服务器返回302,此时,如果不进行特殊处理,客户端也收到302,但是如果客户端访问不到内部的服务器就要让nginx主动跟随302的地址
这是一个例子
curl -I http://192.168.240.153:10002/
HTTP/1.1 302 Found
Server: nginx
Date: Thu, 13 Jul 2023 01:56:15 GMT
Content-Type: text/html; charset=utf-8
Connection: keep-alive
Cache-control: no-cache,must-revalidate
Location: /center/Login/index.html
Set-Cookie: userCookieUrlInfo::xm2=Index%2Findex; expires=Thu, 13-Jul-2023 01:56:25 GMT; Max-Age=10; path=/
Set-Cookie: PHPSESSID=5c0fd7d1d95622ba00a136bcd921add4; expires=Fri, 14-Jul-2023 01:56:15 GMT; Max-Age=86400; path=/
Access-Control-Allow-Origin: *
Access-Control-Allow-Headers: X-Requested-With
Access-Control-Allow-Methods: GET,POST,OPTIONS
反向代理做如下修改
添加对错误的处理,在服务器返回301 302 307的情况下,执行handleredirects规则,
在handleredirects规则中, 设置变量savedredirectlocation 为新获取的http地址,然后再跳转过去
server {
...
location / {
proxy_pass http://192.168.240.153:10002/;
proxy_intercept_errors on;
error_page 301 302 307 = @handle_redirects;
}
location @handle_redirects {
set $saved_redirect_location '$upstream_http_location';
# 解决cookie跨域问题
proxy_set_header Cookie $http_cookie;
proxy_pass $saved_redirect_location;
}
}