拦截后端 301/302 重定向(proxy_pass)并重写到另一个位置块可能吗? [英] Intercepting backend 301/302 redirects (proxy_pass) and rewriting to another location block possible?
问题描述
我们的 nginx 前端后面有几个后端.
We have a couple of backends sitting behind our nginx front ends.
是否可以拦截这些后端发送的 301/302 重定向并让 nginx 处理它们?
Is it possible to intercept 301 / 302 redirects sent by these backends and have nginx handle them?
我们单独处理一些事情:
We were thinging something alone the lines of:
error_page 302 = @target;
但我怀疑 301/302 重定向是否可以像 404 等一样处理...我的意思是,error_page 可能不适用于 200 等错误代码?
But I doubt 301/302 redirects can be handled the same as 404's etc etc... I mean, error_page probably doesnt apply to 200, etc error codes?
总结一下:
我们的后端偶尔会发回 301/302.我们希望 nginx 拦截这些,并将它们重写到另一个位置块,在那里我们可以用它们做任何数量的其他事情.
Our backends send back 301/302s once in a while. We would like to have nginx intercept these, and rewrite them to another location block, where we could do any number of other things with them.
可能吗?
谢谢!
推荐答案
您可以使用 proxy_redirect
指令:
http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_redirect
Nginx 仍会向客户端返回 301/302,但 proxy_redirect
将修改 Location
标头,客户端应向 中给出的 URL 发出新请求位置
标题.
Nginx will still return 301/302 to the client but proxy_redirect
will modify Location
header and the client should make a new request to the URL given in the Location
header.
这样的事情应该使后续请求返回到 nginx:
Something like this should make the subsequent request back to nginx:
proxy_redirect http://upstream:port/http://$http_host/;
这篇关于拦截后端 301/302 重定向(proxy_pass)并重写到另一个位置块可能吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!