容易晕的proxy_pass 后面 url带不带”/“的问题
(1)配置 proxy_pass 时,当在后面的 url 加上了 /,相当于是绝对路径,则 Nginx 不会把 location 中匹配的路径部分加入代理 uri,即url直接替换为代理的地址。
(2)如果配置 proxy_pass 时,后面没有 /,Nginx 则会把匹配的路径部分加入代理 uri,即匹配的路径部分替换后代理的地址后,再加上请求的地址。
代理配置举例
下面几种情况的代理配置
1.请求地址:http://127.0.0.1/test -> http://xiaofei.site/dir/test
#代理配置
location /test {
proxy_pass http://xiaofei.site/dir ;
}
2.请求地址:http://127.0.0.1/test/ -> http://xiaofei.site/dir
location /test {
proxy_pass http://xiaofei.site/dir/ ;
}
3.请求地址:http://127.0.0.1/test/ -> http://127.0.0.1/dir/test/
location /test/ {
proxy_pass http://xiaofei.site/dir;
}
4.请求地址:http://127.0.0.1/test/ ->