官方文档 http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_pass
proxy_pass 末尾是否加 /
的区别
1. 末尾有’/’
location /proxy/ {
proxy_pass http://127.0.0.1:8080/; #末尾有'/'
}
proxy_pass 地址末尾加 /
,则代理路径将不包含location匹配的部分
- 访问 localhost:80/proxy/index.html 时,代理到 127.0.0.1:8080/index.html
2. 末尾无’/’
location /proxy/ {
proxy_pass http://127.0.0.1:8080; #末尾无'/'
}
proxy_pass 地址末尾不加 /
,则代理路径将包含location匹配的部分
- 访问 localhost:80/proxy/index.html 时,代理到 127.0.0.1:8080/proxy/index.html
扩展:通过 rewrite 配置实现末尾不加 /
和 加 /
的代理路径一致
location /proxy/ {
proxy_pass http://127.0.0.1:8080; #末尾无'/'
rewrite '^/proxy(.*)$' $1 break;
}
- 访问 localhost:80/proxy/index.html 时,代理到 127.0.0.1:8080/index.html
end