LOADING

加载过慢请开启缓存 浏览器默认开启

Nginx中location与proxy_pass是否携带斜杠的分析

本文主要探讨一下使用Nginx进行反代时,location中的location以及proxy_pass带斜杠的分析。

四种情况

location结尾不带斜杠,proxy_pass结尾不带斜杠

location /test-locaion-and-p {
    proxy_pass http://127.0.0.1:5000;
}

请求/test-locaion-and-p被转发到http://127.0.0.1:5000/test-locaion-and-p
请求/test-locaion-and-p/sub被转发到http://127.0.0.1:5000/test-locaion-and-p/sub

location结尾带斜杠,proxy_pass结尾不带斜杠

location /test-p/ {
    proxy_pass http://127.0.0.1:5000;
}

请求/test-p/被转发到http://127.0.0.1:5000/test-p/
请求/test-p/sub被转发到http://127.0.0.1:5000/test-p/sub

location结尾不带斜杠,proxy_pass结尾带斜杠

location /test-location {
    proxy_pass http://127.0.0.1:5000/;
}

请求/test-location被转发到http://127.0.0.1:5000/
请求/test-location/sub被转发到http://127.0.0.1:5000//sub

location结尾带斜杠,proxy_pass结尾带斜杠

location /test/ {
    proxy_pass http://127.0.0.1:5000/;
}

请求/test被转发到http://127.0.0.1:5000/
请求/test/sub被转发到http://127.0.0.1:5000/sub

总结

由以上的结果能够看到

location后是否带斜杠影响不大,而proxy_pass的url后面是否带/将影响反代到的实际路径。