Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

nginx proxy_pass to localhost

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).

like image 949
Greg Avatar asked Sep 08 '13 21:09

Greg


People also ask

What is proxy_pass in Nginx?

The proxy_pass setting makes the Nginx reverse proxy setup work. The proxy_pass is configured in the location section of any virtual host configuration file. To set up an Nginx proxy_pass globally, edit the default file in Nginx's sites-available folder.

How do I redirect a URL in Nginx?

To redirect a URL in a website running on an Nginx server, you must manually create a redirects. conf file. In this file will then add your redirect code.


1 Answers

This worked:

location /blah {
    rewrite ^/blah/(.*) /$1 break;
    proxy_pass http://$server_addr:8080;
}
like image 180
Greg Avatar answered Sep 28 '22 14:09

Greg