一文说清楚nginx中pass_proxy的使用
概述: 说到关于nginx中pass_proxy的使用,其实很多场景下我们都会遇到一些不同的问题,比如说:访问路径多了一层目录、多了一个/斜杆、出现访问404等等,其原因网上一搜遍地都是,但大多都不完整,今天我们来详解一下pass_proxy的使用,尽量做到一文讲清楚。
重要说明: 以下是模拟后端为静态固定路径, 如果是java或其它项目,请先了解对应后端项目的实际访问路径,再做转发,避免盲目尝试pass_proxy从而时间浪费! 例如后端项目是IP:8001 , 接口是IP:8001/pay/ 那么就要注意了,本身的入口就带有/pay/ 这层路径了,做pass_proxy的时候就要看实际需求而定。
nginx测试版本: tengine V2.3
本次测试nginx的默认配置文件内容如下:
server {
listen 80;
server_name test.a.cn;
access_log /var/log/nginx/access_passproxy.log;
location /testproxypass/ {
proxy_pass http://127.0.0.1:8777;
}
location /
return 401;
}
server {
listen 8777;
server_name 127.0.0.1;
index index.html index.htm;
root /var/www/s2;
access_log /var/log/nginx/access_s2.log;
location / {
expires 1d;
}
}
说明: 其中80 server中是前置代理,并且没有指定root; 8777 server是模拟后端服务,指定了root , /var/www/s2下有一个index.html 文件,文件内容 test s2 index.html 。
模拟请求情况一(按照默认配置文件,不改配置):
当请求 http://test.a.cn/testproxypass/index.html 时,出现以下情况:
1、页面显示404 ,无法找到。
2、文件 /var/log/nginx/access_passproxy.log 日志中出现 “ - - [20/Jul/2022:14:13:25 +0800] "GET /testproxypass/index.html HTTP/1.1" 404 582 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101 Firefox/102.0" ”
3、文件/var/log/nginx/access_s2.log 日志中出现 “ 127.0.0.1 - - [20/Jul/2022:14:13:25 +0800] "GET /testproxypass/index.html HTTP/1.0" 404 582 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101 Firefox/102.0" “
分析: 请求去往前置代理域名的时候,将请求完全抛给了后端服务,来到后端服务后,去找root , /var/www/s2目录下,/testproxypass/index.html这样一个文件,发现没有找到,抛404。
然而,当请求 http://test.a.cn/testproxypass-test 的时候,就不一样了,情况如下:
1、页面显示401 ,我们之前定义的错误,显然进入了 location / 中。
2、文件 /var/log/nginx/access_passproxy.log 日志中出现 “ - - [20/Jul/2022:17:07:00 +0800] "GET /testproxypass-test HTTP/1.1" 401 600 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101 Firefox/102.0" ”
3、文件/var/log/nginx/access_s2.log 日志中没有任何请求。
分析:很显然,请求既到了前置代理并没有进入后端服务,说明没有匹配到 location /testproxypass/ 规则, 进入location /。
模拟请求情况二(按照默认配置文件),并且将location /testproxypass/ 替换为location /testproxypass,少了后面的/斜杆.
当请求 http://test.a.cn/testproxypass/index.html ,出现以下情况:
1、页面显示404 ,无法找到。
2、文件 /var/log/nginx/access_passproxy.log 日志中出现 “ - - [20/Jul/2022:14:13:25 +0800] "GET /testproxypass/index.html HTTP/1.1" 404 582 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101 Firefox/102.0" ”
3、文件/var/log/nginx/access_s2.log 日志中出现 “ 127.0.0.1 - - [20/Jul/2022:14:13:25 +0800] "GET /testproxypass/index.html HTTP/1.0" 404 582 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101 Firefox/102.0" “
我们会发现以上请求的路径,与情况一种的结果没有任何区别;
然而,当请求 http://test.a.cn/testproxypass-test 的时候,就不一样了,情况如下:
1、页面显示404 ,无法找到。
2、文件 /var/log/nginx/access_passproxy.log 日志中出现 “ - - [20/Jul/2022:17:06:01 +0800] "GET /testproxypass-test HTTP/1.1" 404 576 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101 Firefox/102.0" ”
3、文件/var/log/nginx/access_s2.log 日志中出现 “ 127.0.0.1 - - [20/Jul/2022:17:06:01 +0800] "GET /testproxypass-test HTTP/1.0" 404 576 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101 Firefox/102.0" “
分析:很显然,请求既到了前置代理也进入了后端服务,说明匹配到了 location /testproxypass 规则。
模拟请求情况三:
当请求 http://test.ff56.cn/testproxypass?=888888 ,出现以下情况:
server {
listen 80;
server_name test.ff56.cn;
access_log /var/log/nginx/access_passproxy.log;
location /testproxypass {
proxy_pass http://127.0.0.1:8777/oldstring;
}
location /
{
return 401;
}
}
server {
listen 8777;
server_name 127.0.0.1;
index index.html index.htm;
root /var/www/s2;
access_log /var/log/nginx/access_s2.log;
location / {
expires 1d;
}
}
1、404,匹配location成功。
2、文件 /var/log/nginx/access_passproxy.log 日志中出现 - - [20/Jul/2022:19:29:29 +0800] "GET /testproxypass?=888888 HTTP/1.1" 404 573 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.84 Safari/537.36"
3、文件/var/log/nginx/access_s2.log 日志中出现 127.0.0.1 - - [20/Jul/2022:19:29:29 +0800] "GET /oldstring?=888888 HTTP/1.0" 404 573 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.84 Safari/537.36"
4、如果pass_proxy后还带了其它路径,只会将/testproxypass 之后的字符传递给后端。
5、适合于通过pass_proxy将参数传递给其它后端服务的情形。
模拟请求情况四:
当请求 http://test.ff56.cn/testproxypass?=888888 ,出现以下情况:
server {
listen 80;
server_name test.ff56.cn;
access_log /var/log/nginx/access_passproxy.log;
location /testproxypass {
proxy_pass http://127.0.0.1:8777;
}
location /
{
return 401;
}
}
server {
listen 8777;
server_name 127.0.0.1;
index index.html index.htm;
root /var/www/s2;
access_log /var/log/nginx/access_s2.log;
location / {
expires 1d;
}
}
1、404,匹配location成功。
2、文件 /var/log/nginx/access_passproxy.log 日志中出现 - - [20/Jul/2022:19:29:29 +0800] "GET /testproxypass?=888888 HTTP/1.1" 404 573 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.84 Safari/537.36"
3、文件/var/log/nginx/access_s2.log 日志中出现 127.0.0.1 - - [20/Jul/2022:19:29:29 +0800] "GET /testproxypass?=888888 HTTP/1.0" 404 573 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.84 Safari/537.36"
4、原封不动的将 /testproxypass888888 所有的内容转给了后端。
模拟请求情况五:
当请求 http://test.ff56.cn/testproxypass/index.html ,出现以下情况:
server {
listen 80;
server_name test.ff56.cn;
access_log /var/log/nginx/access_passproxy.log;
location /testproxypass/ {
proxy_pass http://127.0.0.1:8777/;
}
location /
{
return 401;
}
}
server {
listen 8777;
server_name 127.0.0.1;
index index.html index.htm;
root /var/www/s2;
access_log /var/log/nginx/access_s2.log;
location / {
expires 1d;
}
}
1、正常显示我们的index.html内容 ”test s2 index.html“
2、文件 /var/log/nginx/access_passproxy.log 日志中出现 - - [20/Jul/2022:19:52:59 +0800] "GET /testproxypass/index.html HTTP/1.1" 200 19 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.84 Safari/537.36"
3、文件/var/log/nginx/access_s2.log 日志中出现 127.0.0.1 - - [20/Jul/2022:19:52:59 +0800] "GET /index.html HTTP/1.0" 200 19 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.84 Safari/537.36"
4、从后端的日志上可以看出:后端会接收到除去location /testproxypass/ 之后的部分,然后返回我们原先放在服务器上的正常内容。
模拟请求情况六
当请求: http://test.ff56.cn/testproxypass/index.html ,出现以下情况:
server {
listen 80;
server_name test.ff56.cn;
access_log /var/log/nginx/access_passproxy.log;
location /testproxypass/ {
proxy_pass http://127.0.0.1:8777/oldstring/;
}
location /
{
return 401;
}
}
server {
listen 8777;
server_name 127.0.0.1;
index index.html index.htm;
root /var/www/s2;
access_log /var/log/nginx/access_s2.log;
location / {
expires 1d;
}
}
1、404,匹配location成功。
2、文件 /var/log/nginx/access_passproxy.log 日志中出现 - - [20/Jul/2022:20:01:19 +0800] "GET /testproxypass/index.html HTTP/1.1" 404 578 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.84 Safari/537.36"
3、文件/var/log/nginx/access_s2.log 日志中出现 127.0.0.1 - - [20/Jul/2022:20:01:19 +0800] "GET /oldstring/index.html HTTP/1.0" 404 578 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.84 Safari/537.36"
4、从后端的日志上可以看出:后端会接收到除去location /testproxypass/ 之后的部分,然后之后的其它部分都会传递给后端。这个情况和前一个几乎相同。
模拟请求情况七
当请求:http://test.ff56.cn/testproxypass/index.html,出现以下情况:
server {
listen 80;
server_name test.ff56.cn;
access_log /var/log/nginx/access_passproxy.log;
location /testproxypass/ {
proxy_pass http://127.0.0.1:8777/oldstring;
}
location /
{
return 401;
}
}
server {
listen 8777;
server_name 127.0.0.1;
index index.html index.htm;
root /var/www/s2;
access_log /var/log/nginx/access_s2.log;
location / {
expires 1d;
}
}
1、404,匹配location成功。
2、文件 /var/log/nginx/access_passproxy.log 日志中出现 - - [20/Jul/2022:20:24:03 +0800] "GET /testproxypass/index.html HTTP/1.1" 404 577 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.84 Safari/537.36"
3、文件/var/log/nginx/access_s2.log 日志中出现 127.0.0.1 - - [20/Jul/2022:20:24:03 +0800] "GET /oldstringindex.html HTTP/1.0" 404 577 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.84 Safari/537.36"
4、我们发现,location /testproxypass/ 之后的部分传递给了后端,但是传递过来并没有斜杆/, 而是直接加到了pass_proxy的路径后。
5、这种情况使用并不多,可用于api传参数。
模拟请求情况八
当请求:http://test.ff56.cn/testproxypass/index.html 与 http://test.ff56.cn/testproxypass?uid=999999 ,出现以下情况:
server {
listen 80;
server_name test.ff56.cn;
access_log /var/log/nginx/access_passproxy.log;
location /testproxypass {
proxy_pass http://127.0.0.1:8777/oldstring/;
}
location /
{
return 401;
}
}
server {
listen 8777;
server_name 127.0.0.1;
index index.html index.htm;
root /var/www/s2;
access_log /var/log/nginx/access_s2.log;
location / {
expires 1d;
}
}
1、404 ,匹配location成功。
2、文件 /var/log/nginx/access_passproxy.log 日志中出现 - - [20/Jul/2022:20:32:26 +0800] "GET /testproxypass/index.html HTTP/1.1" 404 577 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.84 Safari/537.36"
3、文件/var/log/nginx/access_s2.log 日志中出现 127.0.0.1 - - [20/Jul/2022:20:32:26 +0800] "GET /oldstringindex.html HTTP/1.0" 404 577 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.84 Safari/537.36"
4、我们发现,location /testproxypass/ 之后的部分传递给了后端,但是传递过来并没有斜杆/, 而是本身的斜杆/也去掉了,直接加到了pass_proxy的路径后。
5、 文件 /var/log/nginx/access_passproxy.log 日志中出现 - - [20/Jul/2022:20:45:54 +0800] "GET /testproxypass?uid=999999 HTTP/1.1" 301 239 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.84 Safari/537.36"
前置代理会跳转一次301到了- - [20/Jul/2022:20:45:54 +0800] "GET /testproxypass/?uid=999999 HTTP/1.1" 404 583 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.84 Safari/537.36"
6、文件/var/log/nginx/access_s2.log 日志中出现 127.0.0.1 - - [20/Jul/2022:20:45:54 +0800] "GET /oldstring?uid=999999 HTTP/1.0" 404 583 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.84 Safari/537.36"
总结,nginx的pass_proxy应用还是挺广泛的,比如经常用到的先重新路径,再进行pass_proxy代理、pass_proxy负载均衡等等,后续再补充其它的用途吧。今天介绍的关于pass_proxy路径的匹配,有正好需要用到的朋友,直接拿去套用自己的配置即可。
本文标签: nginx pass_proxy的使
内容版权声明:【蓝色网居】部分资源来源于网络,如有侵犯您的所有权,请随时告知我们,我们将立即删除!感谢配合!
【手机扫一扫查看文本】
tcp代理 404 日志分析 请求频率 pass_proxy的使 referer限制 404自定义 ngx_cache_pu 定义404跳转 防IP 防IP恶意 ip白名单 lua try_files 与(and) 模糊匹配 $http_host nginx插件 强匹配 扫描访问 四层 参数重写 案例分析 错误页 白名单 geoIP模块 访问控制 跳转rewrite 运算 接口请求

