首页 > 技术文章 > nginx 之proxy_pass

fanggege 2020-03-01 19:19 原文

在nginx中配置proxy_pass代理转发时,如果在proxy_pass后面加不加路径是有很大区别的,具体情况我们来做几个测试

 

proxy_pass 后面不带路径

    location /test {
         proxy_pass http://192.168.1.8;
    }
View Code

访问http://www.kzf.com/test/... 代理转发后URL为http://192.168.1.8/test/..。实际转发后用代理的地址+客户端的uri 来转发的。

 

proxy_pass 后面加“/”

    location /test {
         proxy_pass http://192.168.1.8/;
    }
View Code

访问http://www.kzf.com/test/a.html 代理转发后URL为http://192.168.1.8//a.html。多了一个/,是因为去掉了location中的路径/test,然奇一proxy中的/作为根路径+客户端URI中去掉/test的部分。去掉的是location 中的uri 而不是客户端请求中的uri,例如客户端请求为http://www.kzf.com/test/asdf/a.html 那么转发后的请求的uri 为//asdf/a.html 而不是/a.html。

 

proxy_pass 后面是一个非根路径

    location  /test {
         proxy_pass http://192.168.1.8/asdf;
    }
View Code

此情况与后面是“/”访问效果一样,实际就是proxy 后面的uri 替换location中的uri再加上客户端uri去掉localtionuri 的部分。另外proxy_pass 后面的uri 例如/asdf 后面加不加"/"都是一样的,但是有些文章却说不加/ 就是另一种情况,例如访问/http://www.kzf.com/test/a.html

转发后为/http://www.kzf.com/asdfa.html 但是实际测试中与加/一样都为/http://www.kzf.com/asdf/.a.html

 

注意:

当location为正则表达式时,proxy_pass 不能包含URI部分。否则检查配置文件的时候会报错。

 

推荐阅读