概念前提
在proxy_pass中的代理url后加上/,代理转发的url中就不会带上location中匹配路径;
在proxy_pass中的代理url后面没有/,代理转发的url中就会带上location中的匹配路径
例如
server {
listen 8080;
server_name localhost;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
try_files $uri $uri/ /index.html;
add_header Access-Control-Allow-Origin *;
}
location /nest/ {
proxy_pass http://portal.com/nest/api/;
client_max_body_size 1024m;
}
location /api/ {
proxy_pass http://portal.com/mall/api/;
client_max_body_size 1024m;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}
请求地址:
https://tmall.com/nest/open/topics
代理后真正的地址
http://portal.com/nest/api/open/topics
说明:/nest被替换成/nest/api
请求地址:
https://tmall.com/api/item/mall/search
代理地址:
http://portal.com/mall/api/item/mall/search
说明:/api被替换成/mall/api
相当于
server {
listen 8080;
server_name localhost;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
try_files $uri $uri/ /index.html;
add_header Access-Control-Allow-Origin *;
}
location /nest {
proxy_pass http://portal.com;
client_max_body_size 1024m;
}
location /mall {
proxy_pass http://portal.com;
client_max_body_size 1024m;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}
https://tmall-rn-test.geega.com/nest/api/open/topics =>
http://portal-refit.cloud-dev.geega.com/nest/api/open/topics
https://tmall-rn-test.geega.com/mall/api/item/mall/search =>
http://portal-refit.cloud-dev.geega.com/mall/api/item/mall/search