131

What I want to do is: When someone visits http://localhost/route/abc the server responds exactly the same as http://localhost:9000/abc

Now I configure my Nginx server like this:

location /route {
    proxy_pass  http://127.0.0.1:9000;
}

The HTTP request is dispatched to port 9000 correctly, but the path it receives is http://localhost:9000/route/abc not http://localhost:9000/abc.

Any suggestions?

1
  • It would help to know if "/route/*" was moved to "/*" or if the intention was to re-write "/route/(.*)" from the very beginning? Because for me the answer would change to a 301 redirect or a URL rewrite.
    – b01
    Oct 24, 2022 at 8:28

4 Answers 4

134

I hate the subtlety here, but try adding a / at the end of 9000 like below. It will no longer append "route" to the forwarded request now.

location /route {
    proxy_pass  http://127.0.0.1:9000/;
}
3
  • 9
    This should be the accepted answer. Simply adding / is well documented as the way to remove the prefix listed in the location.
    – Bernard
    Sep 12, 2016 at 12:39
  • it should be location /route/
    – vakarami
    Jun 7, 2021 at 7:24
  • More details for newcomers: The location directive goes under the server directive, and the server directive goes under the http directive. To reload nginx, run "nginx -s reload". Here's an example server directive: redswitches.com/blog/nginx-location/… Jan 21 at 19:57
58

I believe you can use rewrite to remove the extra part of the URL. In your case I think you could use:

location /route/ {
    rewrite ^/route/?(.*)$ /$1 break;    
    proxy_pass  http://127.0.0.1:9000;
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}

However if your app has internal links in it, they may still point to /abc/foo , and if you do this they instead need to point to /route/abc/foo so that the raw request comes in correctly. You may be better off leaving the nginx config as it is and instead configuring your app to be aware it lives at a subdirectory, if you can.

I know this is an old question, but it was the top google hit for me when I was trying to solve the same issue!

2
28

Try the following

location /route/ {
        proxy_pass  http://127.0.0.1:9000;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
2
  • This solution worked best for me, as it preserves the host name. Sep 29, 2018 at 20:18
  • Clean and simple. Mar 15, 2019 at 11:24
13

vim nginx.conf

http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;

  server {
    listen 8080;

    location / {
      proxy_pass http://compute-1-36:8787;
      proxy_redirect http://compute-1-36:8787/ $scheme://$host:8080/;
    }
  }
}

This code listens on 8080 and redirects to port 8787 on compute-1-36. You can select other path in location /

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .