Nginx Location和proxy_pass结尾带不带/的区别
location匹配结尾带不带/
location /a/b
location /a/b/
第一种不带/, 可以匹配/a/b、/a/b/c、/a/bc
第二种带/, 只可以匹配/a/b/、/a/b/c
proxy_pass结尾带不带/
存在以下四种情况
location /a/ {
proxy_pass http://target.com:8080;
}
location /a/ {
proxy_pass http://target.com:8080/;
}
location /a/ {
proxy_pass http://target.com:8080/api;
}
location /a/ {
proxy_pass http://target.com:8080/api/;
}
针对以上四种情况,当请求 http://example.com/a/wocao/ 进入nginx后,proxy_pass结果分别是:
- http://target.com:8080/a/wocao/
- http://target.com:8080/wocao/
- http://target.com:8080/apia/wocao/
- http://target.com:8080/api/wocao/
总结
对于proxy_pass结尾的/,当结尾带/,不会将localtion匹配的内容加入到proxy_pass地址结尾,反之会将localtion匹配内容添加到proxy_pass地址结尾