Nginx proxy_pass轉發/路徑小記
按照^~匹配路徑時,要注意proxy_pass後的url最後的/
加上/,至關因而絕對根路徑,則nginx不會把location中匹配的路徑部分代理傳到後端upstream
不加/,則會把匹配的路徑部分也代理傳到後端upstream
upstream apache {
server localhost:8080 weight=1 max_fails=2 fail_timeout=30s;
}
server {
location ^~ /test/
{
#proxy_pass http://localhost:8080/;
proxy_pass http://apache/;
}
}
如上配置,若是請求的url是http://servername/test/test.html
會被代理成http://apache
/test.html
而若是這麼配置
upstream apache {
server localhost:8080 weight=1 max_fails=2 fail_timeout=30s;
}
server {
location ^~ /test/
{
#proxy_pass http://localhost:8080;
proxy_pass http://apache;
}
}
則會被代理到http://apache
/test/test.html
還能夠用rewrite來實現
server {
rewrite ^/test/(.*)$ http://192.168.192.20:8080/$1 break;
}