Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Intercepting backend 301/302 redirects (proxy_pass) and rewriting to another location block possible?

Tags:

We have a couple of backends sitting behind our nginx front ends.

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; 

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?

So to summarize:

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.

Possible?

Thanks!

like image 221
anonymous-one Avatar asked Nov 27 '13 22:11

anonymous-one


People also ask

What is the difference between 301 and 302 redirect?

The user's search experience may be the same as both options land the user on the appropriate webpage. However, search engines handle these types of URL redirects differently - the 302 redirect means that the page has been moved temporarily and other, 301, means that a new page has taken over permanently.

Does a 302 redirect change the URL?

What is a 302 redirect? Whereas a 301 redirect is a permanent relocation of your URL, a 302 redirect is a temporary change that redirects both users and search engines to the desired new location for a limited amount of time, until the redirect is removed.

What does a 302 redirect do?

What Is A 302 Redirect? A 302 redirect lets search engines know that a website or page has been moved temporarily. When Should You Use 302 Redirects? Use this type of redirect if you want to send users to a new site or page for a short period of time, such as when you're redesigning or updating your website.


1 Answers

You could use proxy_redirect directive:

http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_redirect

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.

Something like this should make the subsequent request back to nginx:

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

like image 111
Tuomo Kestilä Avatar answered Oct 26 '22 23:10

Tuomo Kestilä