拦截后端301/302重定向(proxy_pass)并重写到另一个位置块可能吗? [英] Intercepting backend 301/302 redirects (proxy_pass) and rewriting to another location block possible?

查看:648
本文介绍了拦截后端301/302重定向(proxy_pass)并重写到另一个位置块可能吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们的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:

proxy_redirect http://upstream:port/ http://$http_host/;

这篇关于拦截后端301/302重定向(proxy_pass)并重写到另一个位置块可能吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆