概述
Nginx 配置中
location
和
proxy_pass
指令的不同组合方式及其对请求转发路径的影响。
配置效果
1.
location
和
proxy_pass
都带斜杠
/
location /api/ {
proxy_pass http://127.0.0.1:8080/;
}
- 访问地址:
www.hw.com/api/upload
- 转发地址:
http://127.0.0.1:8080/upload
转发地址不带 location 匹配目录 /api/
2.
location
不带斜杠,
proxy_pass
带斜杠
/
location /api {
proxy_pass http://127.0.0.1:8080/;
}
- 访问地址:
www.hw.com/api/upload
- 转发地址:
http://127.0.0.1:8080//upload
转发地址会多带 /
3.
location
带斜杠,
proxy_pass
不带斜杠
location /api/ {
proxy_pass http://127.0.0.1:8080;
}
- 访问地址:
www.hw.com/api/upload
- 转发地址:
http://127.0.0.1:8080/api/upload
转发地址会带 location 匹配目录 /api/
4.
location
和
proxy_pass
都不带斜杠
location /api {
proxy_pass http://127.0.0.1:8080;
}
- 访问地址:
www.hw.com/api/upload
- 转发地址:
http://127.0.0.1:8080/api/upload
转发地址会带 location 匹配目录 /api/
5.
location
和
proxy_pass
都带斜杠
/
,但
proxy_pass
带地址
location /api/ {
proxy_pass http://127.0.0.1:8080/server/;
}
- 访问地址:
www.hw.com/api/upload
- 转发地址:
http://127.0.0.1:8080/server/upload
转发地址不带 location 匹配目录 /api/
6.
location
不带斜杠,
proxy_pass
带斜杠
/
,但
proxy_pass
带地址
location /api {
proxy_pass http://127.0.0.1:8080/server/;
}
- 访问地址:
www.hw.com/api/upload
- 转发地址:
http://127.0.0.1:8080/server//upload
转发地址不带 location 匹配目录 /api/ ,会多带 /
7.
location
带斜杠,
proxy_pass
不带斜杠,但
proxy_pass
带地址
location /api/ {
proxy_pass http://127.0.0.1:8080/server;
}
- 访问地址:
www.hw.com/api/upload
- 转发地址:
http://127.0.0.1:8080/serverupload
转发地址不带 location 匹配目录 /api/ 直接进行了替换
8.
location
和
proxy_pass
都不带斜杠,但
proxy_pass
带地址
location /api {
proxy_pass http://127.0.0.1:8080/server;
}
- 访问地址:
www.hw.com/api/upload
- 转发地址:
http://127.0.0.1:8080/server/upload
转发地址不带 location 匹配目录 /api
总结
- 当
proxy_pass
代理地址端口后有目录(包括/
),转发后地址为:代理地址 + 访问 URL 目录部分去除location
匹配目录。 - 当
proxy_pass
代理地址端口后无任何内容,转发后地址为:代理地址 + 访问 URL 目录部分(包括location
地址)。
场景示例
upstream backend_name_hw {
server 10.10.10.10:32323 max_fails=2 fail_timeout=2;
}
server {
listen 80;
server_name hw.test.com;
client_max_body_size 1024m;
client_body_timeout 12;
client_header_timeout 12;
keepalive_timeout 15;
send_timeout 10;
location / {
proxy_pass http://backend_name_hw;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
location /hello {
proxy_pass http://backend_name_hw/hello;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
location /hw/ {
proxy_pass http://hw-nginx/index.html;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
location /hwhw/ {
proxy_pass http://hw-nginx/;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
location /hw/hi/ {
proxy_pass http://hw-nginx/hello/index.html;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
location /hello/index.html {
proxy_pass http://hw-nginx;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
版权归原作者 _Johnny_ 所有, 如有侵权,请联系我们删除。