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