Jason La
Jason La

Reputation: 1081

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.

Upvotes: 6

Views: 2045

Answers (1)

Kevin A. Naudé
Kevin A. Naudé

Reputation: 4060

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.

Upvotes: 2

Related Questions