2

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?

  • 1
    Most likely your application is doing this. – Michael Hampton Sep 9 '14 at 17:19
  • Probably you are right (I tried with a different app and didn't experience this issue). Feel free to add your comment as an answer and I will gladly accept it. – jarandaf Sep 9 '14 at 21:14
  • Why do you have proxy_redirect off? – Alexey Ten Sep 10 '14 at 3:39
  • Also it's pretty bad idea to have location with trailing slash and proxy_pass without one. – Alexey Ten Sep 10 '14 at 3:40
  • Just tried different configurations until I got something working. Why is it a bad idea? – jarandaf Sep 10 '14 at 4:31
4

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;
}
1

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.

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.