今天上了一个多页应用,发现接口转发后是 401。 最后发现是接口转发出错了。地址里多拼了一个 路径。 以前我一直以为location 字段会替换掉 proxy_pass 里面配置的 url 地址。 今天了解了一下 发现还是有不少细节的。
// node js 文件,打印请求路径。方便查看后端真实接受到的请求地址 const http = require('http'); http.createServer((req, res) => { console.log(req.url); res.end(`您的 请求 地址是${req.url}`); }).listen(3000); proxy_pass 如何转发,首先看 proxy_pass 的url 配置。
proxy_pass 只是HOST proxy_pass 只是HOST,不包含任何路径,比如
* http://host - √ * https://host - √ * http://host:port - √ * https://host:port - √ * http://host/ - x * http://host:port/ - x 这种情况下,会把匹配到的所有路径直接穿透转发。比如以下的配置
location /api/ { proxy_pass http://127.0.0.1:3000; } 访问 http://127.0.0.1:80/api/cc, 后端结果为 您的 请求 地址是/api/cc
proxy_pass 包含路径 这里的路径哪怕只是一个 / 也是存在的,如:
http://host - x https//host/ - √ http://host:port- x https://host:port/ - √ http://host/api - √ http://host/api/ - √ 这种情况下,url 里面会去掉 location 匹配的字符串,拼接到 proxy_pass 再进行转发。
location /api/ { proxy_pass http://127.0.0.1:3000/; } 访问 http://127.0.0.1:81/api/cc, 后端结果为 您的 请求 地址是/cc
重写代理链接 - url rewrite 使用 rewrite 指令并且生效后,proxy_pass url 链接中的路径会被忽略,如:
server { listen 83; location / { rewrite ^/api/(.*) /fixpath=$1 break; proxy_pass http://127.0.0.1:3000/node/; } location ^/api/ { rewrite ^/api/(.*) /fixpath=$1 break; proxy_pass http://127.0.0.1:3000/node/; } } 访问 http://127.0.0.1:83/bb/cc 得到 您的 请求 地址是/node/bb/cc(匹配上 / 了,没有匹配 rewrite) 访问 http://127.0.0.1:83/api/cc 得到 您的 请求 地址是/fixpath=cc (我们写的 proxy_pass http://127.0.0.1:3000/node/ 里面的 node路径丢失了 )
知道了这几点,碰到转发接口也有一点底气啦~
更多 在github上看到的这本小书 ⬇️ (参考资料)[https://xuexb.github.io/learn...]
我的 nginx 配置
events { } http { # proxy_pass url 只是 host # 这时候 location 匹配的完整路径将直接透传给 url ,如: server { listen 80; location / { proxy_pass http://127.0.0.1:3000; }
location /api/ { proxy_pass http://127.0.0.1:3000; } } # url 包含路径 # 当 proxy_pass url 的 url 包含路径时,匹配时会根据 location 的匹配后的链接透传给 url ,注意匹配后就是这样: server { listen 81; location / { proxy_pass http://127.0.0.1:3000/; }
location /api/ { proxy_pass http://127.0.0.1:3000/; } location /bpi/ { proxy_pass http://127.0.0.1:3000/v1; } location /cpi { proxy_pass http://127.0.0.1:3000/v1; } } # 当 location 以正则形式匹配时,proxy_pass 就不能以 / 结束了,也就是不能包含路径了, 会提示配置错误,比如错误的:
server { listen 82; location / { proxy_pass http://127.0.0.1:3000/; } # nginx: [emerg] "proxy_pass" cannot have URI part in location given by regular expression, or inside named location, or inside "if" statement, or inside "limit_except" block in /test.conf:47 # location ~* ^/api/ { # proxy_pass http://127.0.0.1:3000/; # }
# nginx: [emerg] "proxy_pass" cannot have URI part in location given by regular expression, or inside named location, or inside "if" statement, or inside "limit_except" block in /Users/tangdaoyuan/code/anheng/jz-bingjiang/test.conf:52 # location ~* ^/api/ { # proxy_pass http://127.0.0.1:3000/b1; # } }
# 使用 rewrite 指令并且生效后,proxy_pass url 链接中的路径会被忽略,如: server { listen 83; location / { proxy_pass http://127.0.0.1:3000/node/; } location ^/api/ { rewrite ^/api/(.*) /fixpath=$1 break; proxy_pass http://127.0.0.1:3000/node/; } }
} 测试流程 : node 运行 服务, 启动Nginx 转发 , 再用postman 发送请求。