nginx 反向代理 proxy_pass详解

在nginx中配置proxy_pass代理转发时,如果在proxy_pass后面的url加/,表示绝对根路径;如果没有/,表示相对路径,把匹配的路径部分也给代理走。

假设下面四种情况分别用 http://192.168.1.1/proxy/test.html 进行访问。

第一种情况,代理到URL:http://192.168.4.173:8084/test.html

location /proxy/ {
  proxy_pass http://192.168.4.173:8084/;
}

注意:proxy后面以及 192.168.4.173:8084后面都有/

第二种(相对于第一种,最后少一个 / ),代理到URL:http://192.168.4.173:8084/proxy/test.html

location /proxy/ {
  proxy_pass http://192.168.4.173:8084;
}

注意:192.168.4.173:8084后面是没有/的

第三种,代理到URL:http://192.168.4.173:8084/gateway/test.html

location /proxy/ {
  proxy_pass http://192.168.4.173:8084/gateway/;
}

注意:192.168.4.173:8084/gateway后面是有/的

第四种(相对于第三种,最后少一个 / ),代理到URL:http://192.168.4.173:8084/gatewaytest.html

location /proxy/ {
  proxy_pass http://192.168.4.173:8084/gateway;
}

注意:192.168.4.173:8084/gateway后面是没有/的