nginx proxy_pass to a directory

…衆ロ難τιáo~ 提交于 2019-12-22 04:51:10

问题


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;
    }
}

回答1:


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.




回答2:


You needs to use this code , i think it work

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 https://192.168.2.2/other ;
    }

    location / {
        proxy_pass https://192.168.1.1;
    }
}


来源:https://stackoverflow.com/questions/26449466/nginx-proxy-pass-to-a-directory

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!