配置如下
location /dev-api/wms-order/api/screen/byq/ {
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Credentials' 'true';
add_header 'Access-Control-Allow-Methods' 'OPTIONS, GET, POST';
proxy_pass http://182.44.16.68:8099/wms-order/api/screen/;
proxy_connect_timeout 300s;
proxy_send_timeout 300s;
proxy_read_timeout 300s;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
转发过程:
当我们请求的地址为:http://182.44.16.68:8088/dev-api/wms-order/api/screen/byq/yearOutbound
的时候,因为端口8088后面的路径,匹配到上面的location,所以匹配的路径会替换为proxy_pass的内容,匹配路径后面的路径yearOutbound会加在proxy_pass后,实际上nginx会把这个地址转发为:
http://182.44.16.68:8099/wms-order/api/screen/yearOutbound
示意图更好理解:
总结:请求nginx http://ip:port/路径1/路径2 通过location路径1匹配,相当于请求:proxy_pass/路径2