301 Redirecting on a nginx machine with a non-standard port behind a proxy server

I have a nginx server that processes PHP requests, but it is configured to listen on a non-standard port (port 12345 or something else). I can’t change the listening port because corporate IT professionals say no.

In the data center there is a proxy server that sends requests from www.domain.com:80 to the nginx field on port 12345.

I have some static 301 redirects that I need to install, but I get unexpected behavior.

Example redirect in site.conf file "server {}":

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

When I try to go to www.domain.com/foo, a redirect occurs, but it tries to redirect 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/)?

Perhaps the best question is: what is the right way to do what I ask? There are 50+ redirects you need to log in, and I would prefer not to create a “location” section for each of these redirects.

+6
source share
1 answer

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

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

I assumed that you used ^/foo/ instead of ^/foo$ , since ^/foo$ is a very specific case. Just review as needed.

+2
source

All Articles