proxy_pass 覆盖由 rewrite 指令更改的 URL [英] proxy_pass overwrites the URL changed by rewrite directive
问题描述
从下面引用的 nginx 文档 中,我明白proxy_pass
指令不会更改 URI,只会将重写的 URI 传递给服务器.
From the nginx documentation as quoted below, I understand that proxy_pass
directive will not the change the URI and will just pass the rewritten URI to the server.
当使用 rewrite 指令在代理位置内更改 URI 时,将使用相同的配置来处理请求(中断):
When the URI is changed inside a proxied location using the rewrite directive, and this same configuration will be used to process a request (break):
location /name/ {
rewrite /name/([^/]+) /users?name=$1 break;
proxy_pass http://127.0.0.1/randomstring;
}
在这种情况下,指令中指定的 URI 将被忽略,并将完整更改的请求 URI 传递给服务器.
In this case, the URI specified in the directive is ignored and the full changed request URI is passed to the server.
但是当我将 proxy_pass
设置为一个变量时,URI 会被覆盖.
But when I set the proxy_pass
to a variable, the URI is being overwritten.
示例:
location /test_rewrite {
set $ups_addr http://upstream_server;
rewrite /test_rewrite /test_rewrite_upstream break;
proxy_pass $ups_addr/random_string;
}
URI /test_rewrite
正在被重写为 /random_string
.
The URI /test_rewrite
is being rewritten to /random_string
.
谁能帮我理解这种行为的原因?
Can anyone help me understand the reason for this behavior?
推荐答案
如以下文档所述:
proxy_pass 中使用变量时:
When variables are used in proxy_pass:
location /name/ {
proxy_pass http://127.0.0.1$request_uri;
}
在这种情况下,如果在指令中指定了 URI,它将按原样传递给服务器,替换原始请求 URI.
In this case, if URI is specified in the directive, it is passed to the server as is, replacing the original request URI.
仅当结果字符串包含 URI 部分时才为真.一些例子:
This is true only if resulting string contains an URI part. Some examples:
location /test_rewrite {
set $ups_addr http://upstream_server;
rewrite /test_rewrite /test_rewrite_upstream break;
proxy_pass $ups_addr;
}
结果字符串不包含 URI 部分,传递给上游服务器的 URI 是 /test_rewrite_upstream
.
Resulting string does not contain an URI part, URI passed to upstream server is /test_rewrite_upstream
.
location /test_rewrite {
set $ups_addr http://upstream_server/;
rewrite /test_rewrite /test_rewrite_upstream break;
proxy_pass $ups_addr;
}
结果字符串包含一个URI部分(注意$ups_addr
变量末尾的斜杠),传递给上游服务器的URI是/
.
Resulting string contains an URI part (note the trailing slash at the end of $ups_addr
variable), URI passed to upstream server is /
.
您可以使用 $uri
变量将重写规则与 proxy_pass
指令中的自定义 URI 部分结合起来:
You can use an $uri
variable to combine rewrite rules with the custom URI parts in a proxy_pass
directive:
location /test_rewrite {
set $ups_addr http://upstream_server;
rewrite /test_rewrite /test_rewrite_upstream break;
proxy_pass $ups_addr/random_string$uri;
}
在这种情况下,传递给上游服务器的 URI 是 /random_string/test_rewrite_upstream
.
In this case URI passed to upstream server is /random_string/test_rewrite_upstream
.
要保留请求参数,请使用 $is_args
和 $args
变量:
To preserve request arguments, use the $is_args
and $args
variables:
location /test_rewrite {
set $ups_addr http://upstream_server;
rewrite /test_rewrite /test_rewrite_upstream break;
proxy_pass $ups_addr$uri$is_args$args;
}
这篇关于proxy_pass 覆盖由 rewrite 指令更改的 URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!