參考: https://serverfault.com/questions/379675/nginx-reverse-proxy-url-rewrite
有個需求
http://myserver:80/foo/bar 反向代理到后台端 http://localhost:3200/bar
兩種方法,第二種是最正確的
第一種
location /foo {
rewrite /foo/(.*) /$1 break;
proxy_pass http://localhost:3200;
proxy_redirect off;
proxy_set_header Host $host;
}
第二種
location /foo {
proxy_pass http://localhost:3200/;
}
upstream后面是否帶反斜線很重要,
請注意proxy_pass指令末尾的附加/。NGINX將去掉匹配的前綴/foo,並將剩余的前綴傳遞給后端服務器的/路徑。
因此,http://myserver:80/foo/bar 將發布到后端http://localhost:3200/bar.
