我们使用nginx作为反向代理。我们希望nginx遵循代理的重定向并使用302上的Location头。我可以使用拦截错误(proxy_intercept_errors on)并在从上游服务器收到302时使用Location头重定向。但是,这个响应的http响应代码仍然是302.我尝试了浏览器和curl。这是预期的行为吗? 发布相关的配置。希望这有帮助。
server {
location / {
proxy_intercept_errors on;
error_page 302 @handle_redirects;
} location @handle_redirects {
set $redirect_upstream_http_location $upstream_http_location;
proxy_pass $redirect_upstream_http_location;
}
}
由于
答案 0 :(得分:0)
将配置更改为以下
server {
location / {
proxy_intercept_errors on;
error_page 302 301 307 = @handle_redirects;
}
location @handle_redirects {
set $redirect_upstream_http_location $upstream_http_location;
proxy_pass $redirect_upstream_http_location;
}
}
来自nginx文档
此外,可以使用“= response”语法将响应代码更改为另一个,例如:
error_page 404 = 200 /empty.gif; 如果代理服务器或FastCGI / uwsgi / SCGI服务器处理错误响应,并且服务器可能返回不同的响应代码(例如,200,302,401或404),则可以使用它返回的代码进行响应:
error_page 404 = /404.php;