Question

I'm trying to use proxy_pass in nginx to forward requests to another port on localhost like this:

location /foo {
    rewrite ^/foo/(.*) /$1 break;
    proxy_pass http://127.0.0.1:8080/;
}
location /bar {
    rewrite ^/bar/(.*) /$1 break;
    proxy_pass http://localhost:8080/;
}
location /blah {
    rewrite ^/blah/(.*) /$1 break;
    proxy_pass http://192.168.77.56:8080/;
}

So only the last one works. The first two give me a page-unavailable error. I know the endpoint is working as I can go directly to localhost:8080 and see output I expected.

Any idea what I'm doing wrong?

[Edit]: Further enlightenment... It seems the rewrite line has something to do with it. Using it like I have here seems to work on non-localhost IPs, i.e. it removes /blah from the path and keeps the rest as it sends it to its final destination. If I remove the rewrite line I can proxy to localhost (of course losing my indented other stuff on the url).

Was it helpful?

Solution

This worked:

location /blah {
    rewrite ^/blah/(.*) /$1 break;
    proxy_pass http://$server_addr:8080;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top