Think before you speak, read before you think.

nginx location proxy_pass URI问题

对于配置 nginx proxy_pass,对于请求的 URI 最后面的 slah 即 / 有无需要注意。

例子

请求 jpuyy.com/compare/mid/222.do
proxy_pass 目标是 http://192.168.2.2:8082/compare/mid/222.do

测试一:proxy_pass 最后面有 /

location ^~ /compare/ {
    proxy_pass http://192.168.2.2:8082/;
    # 有 /,将从 /compare/ 右做为请求与 proxy_pass 进行拼接, request to http://192.168.2.2:8082/mid/222.do
}

测试二:proxy_pass 最后无 /

location ^~ /compare/ {
    proxy_pass http://192.168.2.2:8082;
    # 无 /,将从/compare/左侧最开始做为请求与 proxy_pass 进行拼接, request to http://192.168.2.2:8082/compare/mid/222.do
}

这样测试一会报 404 错,证明要采用测试二。

例子

请求 jpuyy.com/news/abc.xml
proxy_pass 目标为 http://192.168.2.2:80/news/abc.xml

location /news/abc.xml {
    proxy_pass http://192.168.2.2:80/news/abc.xml;
}

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *