nginx的转发之proxy_pass研究

技术 · 2021-04-15

nginx中配置proxy_pass一般是形如这样:
location /proxy {

proxy_pass http://xxxx.com/;

}

这里面有几种情况,比如location后面是否带 / ,或者proxy_pass后面是否带 / ,下面分情况试验下:
第一种:
location /proxy {

proxy_pass http://xxxx.com;

}
跳转到:http://xxxx.com/index.html;

第二种:
location /proxy {

proxy_pass http://xxxx.com/;

}
跳转到:http://xxxx.com/index.html;

第三种:
location /proxy/ {

proxy_pass http://xxxx.com;

}
跳转到:http://xxxx.com/proxy/index.html;

第四种:
location /proxy/ {

proxy_pass http://xxxx.com/;

}
跳转到:http://xxxx.com/index.html;

第五种:
location /proxy/ {

proxy_pass http://xxxx.com/aaa/;

}
跳转到:http://xxxx.com/aaa/index.html;

第六种:
location /proxy/ {

proxy_pass http://xxxx.com/aaa;

}
跳转到:http://xxxx.com/aaaindex.html;

所以综上所述:
一般建议location出后面一定要加 / ,目的是为了准确识别,比如:location /m 会把 /m /mm /ma 等等请求都拦截到的。
而proxy_pass后面,也要根据情况加 / ,(如 第一种 和 第五种)目的是防止目录不正确。

Theme Jasmine by Kent Liao