nginx proxy_pass 指令

nginx proxy_pass 指令

文档

Nginx 官方文档
https://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_pass

Nginx 服务器的反向代理 proxy_pass 配置方法讲解
https://www.cnblogs.com/lianxuan1768/p/8383804.html 
Syntax: proxy_pass URL;
Default:    —
Context:    location, if in location, limit_except

Sets the protocol and address of a proxied server and an optional URI to which a location should be mapped. As a protocol, “http” or “https” can be specified. The address can be specified as a domain name or IP address, and an optional port:
A request URI is passed to the server as follows:

# 如果 proxy_pass 指令指定了 URI ,则传给服务器的请求的 URI 中匹配 location 指令的部分将被去除掉。
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/;
}

# 如果 proxy_pass 指令没有指定 URI ,则传给服务器的请求的 URI 被原样传递。
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;
}

测试应用的目录结构

[root@localhost ~]# tree ROOT
ROOT
├── proxy
│   ├── mozq
│   │   └── test.html
│   ├── mozqtest.html
│   └── test.html
└── test.html

请求的 url 都是 www.mozq.com/proxy/test.html

所有请求的 uri 是 /proxy/test.html

upstream tomcatserver {  
    server  172.17.0.3:8080;
}   
server {  
    listen       80;  
    server_name  www.mozq.com;  
    location /proxy/ {  
        proxy_pass   http://tomcatserver/;  
        index  index.html index.htm;  
    }  
} 
#请求
www.mozq.com/proxy/test.html
# 实际访问
/test.html
server {  
    listen       80;  
    server_name  www.mozq.com;  
    location /proxy/ {  
        proxy_pass   http://tomcatserver/mozq/;  
        index  index.html index.htm;  
    }  
} 
#请求
www.mozq.com/proxy/test.html
# 实际访问
/mozq/test.html
server {  
    listen       80;  
    server_name  www.mozq.com;  
    location /proxy/ {  
        proxy_pass   http://tomcatserver;  
        index  index.html index.htm;  
    }  
} 
#请求
www.mozq.com/proxy/test.html
# 实际访问
/proxy/test.html
server {  
    listen       80;  
    server_name  www.mozq.com;  
    location /proxy/ {  
        proxy_pass   http://tomcatserver/mozq;  
        index  index.html index.htm;  
    }  
} 
#请求
www.mozq.com/proxy/test.html
# 实际访问
/mozqtest.html

总结

请求: www.mozq.com/proxy/test.html
请求的 URI 为: /proxy/test.html
location: /proxy/

proxy_pass: 带 URI 的情况。请求的 URI 中匹配 location 指定的部分被去除掉。请求的 URI 去除掉被 location 指令匹配的部分后为 test.html 。拼接。
    http://tomcatserver/        结果: http://tomcatserver/test.html
    http://tomcatserver/mozq    结果: http://tomcatserver/mozqtest.html
    http://tomcatserver/mozq/   结果: http://tomcatserver/mozq/test.html
    
proxy_pass: 不带 URI 的情况。使用原请求的 URI 。拼接。
    http://tomcatserver         结果: http://tomcatserver/proxy/test.html

转载于:https://www.cnblogs.com/mozq/p/11323192.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值