Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

301 Redirect on nginx machine running non-standard port behind a proxy

I have an nginx server processing PHP requests, but it's configured to listen on a non-standard port (port 12345 or something). I can't change the listen port because corporate IT says, "No."

There is a proxy in the data center that forwards requests from www.domain.com:80 to the nginx box on port 12345.

I have some static 301 redirects that I need to put in place, but I'm getting unexpected behavior.

Sample redirects in site.conf "server { }" block:

rewrite ^/foo$ /bar/foo/ permanent;

When I attempt to go to www.domain.com/foo, the redirect happens, but it tries to forward the browser to www.domain.com:12345/bar/foo/

My question is, how can I get nginx to redirect the user to the correct port (www.domain.com/bar/foo/)?

Maybe a better question is, what is the correct way to do what I'm asking? There are 50+ redirects that need to go in, and I'd rather not create a "location" section for each of those redirects.

like image 751
Jason La Avatar asked Dec 17 '12 19:12

Jason La


People also ask

Why is Nginx returning 301?

Temporary and Permanent Nginx Redirect Explained On the other hand, a permanent Nginx redirect informs the web browser that it should permanently link the old page or domain to a new location or domain. To map this change, the redirects response code 301 is used for designating the permanent movement of a page.

Why is 301 redirect not working?

The reasons for 301 redirect not working are much more well-defined among WordPress sites. One of the main causes is because you have added the rewrite rules on both the cPanel “Redirects” tool and from your WordPress plugin.


1 Answers

You can provide a more explicit rewrite. Try the following:

rewrite ^/foo/ $scheme://www.domain.com:80/bar$request_uri permanent;

I have assumed that you meant to use ^/foo/ instead of ^/foo$, since ^/foo$ is a very specific case. Just revise as needed.

like image 91
Kevin A. Naudé Avatar answered Sep 30 '22 01:09

Kevin A. Naudé