Nginx: How to forward an HTTP request to another port

nginx

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?

Best Answer

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/;
}
Related Topic