I currently have nginx setup to serve content through Varnish. Nginx listens on port 8000 and varnish connects users' requests from 80 to 8000.
The problem is, on some occasions, particularly when trying to hit a directory, like site.com/2010
, nginx is redirecting the request to site.com:8000/2010/
.
How can I prevent this?
I my case nginx listens to port
80
inside a docker container but it's mapped to port8080
(or any random port) outside the container. There is no reverse proxy in-between that can add proper headers for port and also don't want to hardcode it in the nginx configuration.Example of wrong redirect:
http://localhost:8080/directory -> http://localhost/directory/
I tried:
But didn't work. The only thing that worked well was:
Manual entry for
absolute_redirect
says:I find this to be more flexible and doesn't require you to have the server name and port hardcoded anywhere.
If you are worried about redirects with relative URLs check this comment.
I found the answer to this question by more carefully reading the HttpCoreModule docs.
port_in_redirect off;
This retains the port used by the client during redirects. Closely related is
server_name_in_redirect
which uses the first hostname for redirects. As I didn't want sitename.v.myserver.com to redirect to sitename.com,The code behind NGINX probably uses the FASTCGI variable SERVER_PORT to determine where to direct the user. SERVER_PORT will contain the port nginx listens on, so that would be 8000 in your case.
You can try something like this for testing purpose:
adapted to your configuration. That's a dirty hack but can help you diagnose the problem.
For those who have
The fastcgi_param SERVER_PORT should be same as the frontend.
So, I set
to redirect to appropriate URL from nginx.
I have the same problem with Nginx+PHP-FPM in docker on 80:8080 (port mapping). port_in_redirect could not help. with absolute_redirect the problem was periodically.
So my solution: create file f.e. schema.conf:
And then just use need port: