理解Nginx的proxy_pass和url路径转换

1,668 阅读1分钟

思考以下两个nginx配置块:

location / {
    proxy_pass http://127.0.0.1:8080;
}
location / {
    proxy_pass http://127.0.0.1:8080/;
}

它们的代理规则其实是一样的,当请求的url为/abc/def,都会将请求代理转发到http://127.0.0.1:8080/abc/def

但是在匹配规则为非根路径的location块中,比如以下配置:

location /app {
    proxy_pass http://127.0.0.1:8080;
}

它和下面这个配置所对应的代理规则却不一样:

location /app/ {
    proxy_pass http://127.0.0.1:8080/;
}

下表列举了locationproxy_pass url 的不同组合所对应的最终请求路径:

CaseNginx locationproxy_pass URLTest URLPath received
1/testhttp://127.0.0.1:8080/test1/abc/test/test1/abc/test
2/test2http://127.0.0.1:8080//test2/abc/test//abc/test
3/test3/http://127.0.0.1:8080/test3/abc/test/test3/abc/test
4/test4/http://127.0.0.1:8080//test4/abc/test/abc/test
5/test5http://127.0.0.1:8080/app1/test5/abc/test/app1/abc/test
6/test6http://127.0.0.1:8080/app1//test6/abc/test/app1//abc/test
7/test7/http://127.0.0.1:8080/app1/test7/abc/test/app1abc/test
8/test8/http://127.0.0.1:8080/app1//test8/abc/test/app1/abc/test
9/http://127.0.0.1:8080/test9/abc/test/test9/abc/test
10/http://127.0.0.1:8080//test10/abc/test/test10/abc/test
11/http://127.0.0.1:8080/app1/test11/abc/test/app1test11/abc/test
12/http://127.0.0.1:8080/app2//test12/abc/test/app2/test12/abc/test

结论:在nginx中配置proxy_pass代理转发时,如果在proxy_pass后面的url加/,表示绝对根路径,nginx不会把location中匹配的路径部分代理走,比如case 4;如果没有/,表示相对路径,则会把匹配的路径部分也给代理走,比如case 3

翻译自:Understanding Nginx proxy_pass and url path translations

参考:Nginx location 和 proxy_pass路徑配置詳解

补充:在location的匹配规则中,/test//test 有什么区别?

# 只会匹配 /test/xxx,不会匹配 /testabc
location /test/ {
  # ...
}

# 可以匹配/test/xxx,也可以匹配/testabc
location /test {
  # ...
}