最初我有一个这样的conf:
location /some/path/ {
proxy_pass http://other.host/foo/;
}
Run Code Online (Sandbox Code Playgroud)
并且请求http://my.domain/some/path/bar将被代理到http://other.host/foo/bar
我开始在 proxy_pass 中使用变量来强制 nginx 重新解析 DNS:
location /some/path/ {
resolver 1.2.3.4;
set $proxy_root "other.host/foo"
proxy_pass http://$proxy_root/;
}
Run Code Online (Sandbox Code Playgroud)
但是我发现 uri 路径的其余部分不再被附加,所以现在请求http://my.domain/some/path/bar将被代理到简单的http://other.host/foo/.
所以我把它改成了正则表达式
location ~ ^/some/path/(.*) {
resolver 1.2.3.4;
set $proxy_root "other.host/foo"
proxy_pass http://$proxy_root/$1;
}
Run Code Online (Sandbox Code Playgroud)
但这不包括任何查询参数,所以我再次更新
location ~ ^/some/path/(.*) {
resolver 1.2.3.4;
set $proxy_root "other.host/foo"
proxy_pass http://$proxy_root/$1?$args;
}
Run Code Online (Sandbox Code Playgroud)
这有点工作,但这意味着有一个 ? 在每个目标地址中,当只有一些传入请求实际上具有 ?query 部分时...
我想我可以做一些进一步的字符串操作,但这感觉有点多。有没有像我最初做的那样更简单的 proxy_pass 方法,但是将代理目标作为变量来强制重新解析?