Nginx – How to make Nginx try_files ignore the port the server listens

nginxopenvpnport

I have a server running both OpenVPN and Nginx on port 443: I configured OpenVPN to run on 443 with the port-share option while Nginx runs on 4545.

All of this seems to work except the try_files directive.

try_files $uri $uri/ =404;

When I access a directory https://mydomain.tld/directory, instead of being redirected to https://mydomain.tld/directory/, I get https://mydomain.tld:4545/directory/. Of course, the port 4545 is blocked by my firewall and the connection fails.

How could I avoid Nginx to redirect to the port it listens using try_files?

Best Answer

Set up port_in_redirect off; in your server configuration.