关于proxy_pass的参数路径问题

由于工作需要,开始分析nginx的proxy模块,在分析之前,当然要先会用了。于是开始熟悉该模块的一些指令,其中最基本的指令要属proxy_pass了。nginx的英文文档总是看着感觉有些别扭,于是按惯例先google了一些文章。

这一搜,就掉进坑里了。

这些文章里都把proxy_pass的目标地址是形如“127.0.0.1:8090”和“127.0.0.1:8090/”分开讨论,认为后者“/"的作用是删除url中匹配的部分,然后再讨论目标地址中带了uri的情况。

其实根本没这么复杂,只有两种情况:

(1)目标地址中不带uri。即proxy_pass的参数形如"http://127.0.0.1:8090"。 此时新的目标url中,匹配的uri部分不做修改,原来是什么样就是什么样。

(2)目标地址中带uri。即proxy_pass的参数形如“http://127.0.0.1:8090/dir1/dir2"

此时新的目标url中,匹配的uri部分将会被修改为该参数中的uri,如"http://127.0.0.1:8888/dir1/dir2."

有人说,你没有讨论ip和端口后带不带”/“的区别。其实是不需要的,因为"/"本身就是一种uri,很明显属于上面的第二种情况,只不过是把原来的uri修改为了现在的uri(”/"),看上去,像是删除了原url中匹配的部分。如果不理解这一点,就会总想着去牢记、区分结尾带不带"/"的情况。 官方文档也是这么叙述的,根本没有提及半句“/":

A request URI is passed to the server as follows:

If the proxy_pass directive is specified with a URI, then when a request is passed to the server, the part of a normalized request URI matching the location is replaced by a URI specified in the directive: location /name/ { proxy_pass http://127.0.0.1/remote/; } If proxy_pass is specified without a URI, the request URI is passed to the server in the same form as sent by a client when the original request is processed, or the full normalized request URI is passed when processing the changed URI: location /some/path/ { proxy_pass http://127.0.0.1; }

测试部分如下。

如果配置为:

server {
                listen 9090;
                access\_log /home/strider/project/nginx/nginx-1.4.2/log/access\_9090.log;
                location /test1/test2/{
                    proxy\_pass http://127.0.0.1:8090;
                }   
        }

则有如下对应关系:

127.0.0.1:9090/test1/test2/echo1----->127.0.0.1:8090/test1/test2/echo1
127.0.0.1:9090/test1/test2/---->127.0.0.1:8090/test1/test2

如果配置为:

server {
                listen 9090;
                access\_log /home/strider/project/nginx/nginx-1.4.2/log/access\_9090.log;
                location /test1/test2/{
                    proxy\_pass http://127.0.0.1:8090/;
                }   
}

则有如下对应关系:

127.0.0.1:9090/test1/test2/echo1----->127.0.0.1:8090/echo1
127.0.0.1:9090/test1/test2/---->127.0.0.1:8090/

如果配置为:

server {
                listen 9090;
                access\_log /home/strider/project/nginx/nginx-1.4.2/log/access\_9090.log;
                location /test1/test2/{
                        proxy\_pass http://127.0.0.1:8090/test1;
                }   
}

则有如下对应关系:

127.0.0.1:9090/test1/test2/echo1----->127.0.0.1:8090/test1echo1
127.0.0.1:9090/test1/test2/---->127.0.0.1:8090/test1

如果配置为:

server {
                listen 9090;
                access\_log /home/strider/project/nginx/nginx-1.4.2/log/access\_9090.log;
                location /test1/test2/{
                        proxy\_pass http://127.0.0.1:8090/test3/test4/test5;
                }   
}

则有如下对应关系:

127.0.0.1:9090/test1/test2/echo1----->127.0.0.1:8090/test3/test4/test5echo1
127.0.0.1:9090/test1/test2/---->127.0.0.1:80990/test3/test4/test5

转载于:https://my.oschina.net/sukai/blog/863932

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值