I have nginx backend that's used to serve static files. Nginx is running on a non-standard port (e.g. 8080). It is publicly accessible via a reverse proxy on port 80.

The setup works fine for the most part. However, if a request is made to a directory, nginx returns a 301 redirect with the port set to 8080.

All the questions I could find were about nginx being a reverse proxy rather than the actual server. Any help would be very much appreciated!

As an illestration of this problem, the URL http://example.com/dir/ works fine. But leaving the trailing slash http://example.com/dir causes the browser to redirect to http://example.com:8080/dir.

  • nginx.org/r/port_in_redirect – Alexey Ten Jul 8 '14 at 8:24
  • Hi Alexey Ten, I've tried adding this to the configuration file, but it doesn't seem to have had any affect. (ofc, after restarting the server) – Sam Jul 9 '14 at 8:17
  • So show your configuration – Alexey Ten Jul 9 '14 at 8:53
  • sorry for the late response. I've added the nginx configuration for that server. – Sam Jul 9 '14 at 11:30
  • I think I found the root of the problem: chrome. Chrome seems to be aggressively caching 301 redirects and not clearing them with Ctrl-R or F5. Your proposed solution seems to have done the trick. Thank you! (PS: you should add the the directive as the solution to this question) – Sam Jul 9 '14 at 11:42

There is port_in_redirect directive for this. You should turn it off.

server {
    port_in_redirect off;

Also, it's better to test redirects (especially permanent redirects) with console tools like curl or wget, because your browser is likely already cached redirect and will just use it without actual hit to the server (that's the main reason of permanent redirect).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.