注意Tengine(Nginx) proxy_pass之后的"/"
1, proxy_pass 之 upstream后面是否带"/"
proxy_pass的时候,是否在upstream名称后面添加"/"是有区别的,例如下面两种配置方式:
配置1,upstream后没有/
location /proxy/ {
proxy_pass http://k8s_beta_lb;
}
配置2,upstream后有/
location /proxy/ {
proxy_pass http://k8s_beta_lb/;
}
同样的请求:"GET /proxy/system/checkLogin HTTP/1.1"
- 配置1,upstream后没有
/
;传到后端的请求为"GET /proxy/system/checkLogin HTTP/1.1"
- 配置2,upstream后有
/
,传到后端的请求为"GET /system/checkLogin HTTP/1.1"
即,如果在upstream后有/
的话,location的名称会在转发的时候被忽略掉;在upstream后不配置/
,请求才会被完整的转发到后端。
2, proxy_pass 之 location后面是否带"/" & upstream后面是否带"/"
proxy_pass的时候,是否在location名称后面添加"/"也是有些区别的,如下测试:
# 配置1,location后没有/,upstream后有/
location /test1 {
proxy_pass http://k8s_beta_lb/;
proxy_http_version 1.1;
proxy_set_header Connection "";
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_next_upstream off;
}
# 配置2,location后有/,upstream后有/
location /test2/ {
proxy_pass http://k8s_beta_lb/;
proxy_http_version 1.1;
proxy_set_header Connection "";
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_next_upstream off;
}
# 配置3,location后没有/,upstream后没有/
location /test3 {
proxy_pass http://k8s_beta_lb;
proxy_http_version 1.1;
proxy_set_header Connection "";
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_next_upstream off;
}
# 配置4,location后有/,upstream后有/
location /test4/ {
proxy_pass http://k8s_beta_lb;
proxy_http_version 1.1;
proxy_set_header Connection "";
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_next_upstream off;
}
分别测试以下4个请求,来做下对比:
- http://beta-cms.test.meipu.cn/test1/location_without_slash_upstream_with_slash
- http://beta-cms.test.meipu.cn/test2/location_with_slash_upstream_with_slash
- http://beta-cms.test.meipu.cn/test3/location_without_slash_upstream_without_slash
- http://beta-cms.test.meipu.cn/test4/location_with_slash_upstream_without_slash
测试结果
10.12.1.47 - - [09/Apr/2018:23:22:15 +0800] "GET //location_without_slash_upstream_with_slash HTTP/1.1" 404 660 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36"
10.12.1.47 - - [09/Apr/2018:23:22:25 +0800] "GET /location_with_slash_upstream_with_slash HTTP/1.1" 404 656 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36"
10.12.1.47 - - [09/Apr/2018:23:22:34 +0800] "GET /test3/location_without_slash_upstream_without_slash HTTP/1.1" 404 668 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36"
10.12.1.47 - - [09/Apr/2018:23:22:41 +0800] "GET /test4/location_with_slash_upstream_without_slash HTTP/1.1" 404 665 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36"
总结:
location | url 或 upstream | 结果 |
---|---|---|
结尾没有/ | 结尾有/ | 请求转发异常,出现两个// |
结尾有/ | 结尾有/ | location的内容被忽略,请求正常转发到upstream |
结尾有没有/ | 结尾没有/ | location的内容被完整转发到upstream |
结尾有/ | 结尾没有/ | location的内容被完整转发到upstream |
- 在nginx中配置proxy_pass时,要注意proxy_pass后的url或upstream结尾是否带有
/
;带有/
,相当于是绝对根路径,则nginx不会把location中匹配的路径部分代理走;如果没有/
,则会把匹配的路径部分也会被代理走。
即在proxy_pass后的url或upstream末尾加/
,请求会被自动rewrite。 - 当location结尾不带
/
且upstream的结尾带/
时,请求的转发可能会出现异常。
推荐配置:
- location末尾一定加
/
- upstream末尾根据实际需求决定是否加
/