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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM