Hide port in Nginx reverse proxy redirection

My set up is as follows:

  • Webserver: app listening on port 8529.
  • Reverse proxy: Nginx listening on port 80.

The problem is that my Nginx redirects requests to port 8529 and this appears in the URL like so http://some.domain.com:8529/foo, which isn't what I want to accomplish.

My current nginx sites-available related file is as follows:

server {

  listen 80;
  server_name some.domain.com;

  location / {
    proxy_pass http://localhost:8529/foo;
    proxy_redirect off;
    proxy_set_header        Host            $host;
    proxy_set_header        X-Real-IP       $remote_addr;
    proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header        X-NginX-Proxy   true;
  }

}

Where some.domain.com is within /etc/hosts pointing to 127.0.0.1.

Any hints?


Hiding the port during proxying needs these two lines in server body:

server_name_in_redirect off;
proxy_set_header Host $host:$server_port;

The conf is like:

server
{
listen 80;
server_name example.com;
server_name_in_redirect off;
proxy_set_header Host $host:$server_port;
location / {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://localhost:8080;
}
access_log off;
}

Either your upstream server issued a redirect and you forbid nginx to rewrite it with proxy_redirect off; and by missing a trailing slash in proxy_pass directive or your application is building these URLs.

Note : as Alexey said, a trailing slash is important in this case as nginx will remove the part of the normalized URI matching the location prefix , i.e. / before appending it to the proxy_pass URI. So your backend server will receive a request on /foobar while your are most likely waiting for /foo/bar.