Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

nginx proxy_pass to a directory

How can I setup nginx to reverse proxy a single folder to one server and the rest of root to a different server?

The root "/" is managed by CMS while the "other" is my own programs (independent of the CMS).

Here is my current configuration

server {
    listen  80;
    server_name www.example.com;
    rewrite     ^   https://$server_name$request_uri? permanent;
}

server {
    listen   443;
    ... <ssl stuff> ...

    server_name www.example.com;

    location /other {
        proxy_pass http://192.168.2.2/other ;
    }

    location / {
        proxy_pass http://192.168.1.1;
    }
}
like image 470
user3160945 Avatar asked Oct 19 '14 10:10

user3160945


1 Answers

reference to nginx docs:

http://wiki.nginx.org/HttpCoreModule#location

http://wiki.nginx.org/HttpProxyModule#proxy_pass

You should change the other location to this:

location /other/ {
    proxy_pass http://192.168.2.2/other/ ;
}

Note the trailing /. It actually make a difference since it gets proxy_pass to normalize the request. I quote:

when a request is passed to the server, the part of a normalized request URI matching the location is replaced by a URI specified in the [proxy_pass] directive.

This should help those that find this page.

like image 95
iansheridan Avatar answered Oct 22 '22 16:10

iansheridan