问题描述
我们的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
标头,并且客户端应向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:
这篇关于拦截后端301/302重定向(proxy_pass)并重写到另一个位置块可能吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!