用作代理(proxy_pass)时,如何避免nginx用空格替换%20? [英] how to avoid nginx to replace %20 by whitespace when using as a proxy (proxy_pass) ?
问题描述
我正在使用nginx作为apache服务器的代理.
I am using a nginx as a proxy for an apache server.
这是我的配置:
location ~ ^/subsite/(.*)$ {
proxy_pass http://127.0.0.1/subsite/$1?$query_string;
}
问题是,如果我发送%20的请求,例如mywebsite.com/subsite/variable/value/title/Access%20denied/another/example
the problem is that if I send a request with %20 like mywebsite.com/subsite/variable/value/title/Access%20denied/another/example
%20替换为空格,并且在访问/title/Access之后,apache并不关心请求的所有结尾
the %20 is replaced by a whitespace, and apache don't care about all the end of the request after Access /title/Access
有什么想法吗?
推荐答案
我能够解决类似的问题-我们有一个api,要求将搜索词作为URL路径的一部分.将输出直接传递到proxy_pass指令会导致即使请求经过正确的url编码,也会抛出502.
I was able to solve a similar issue -- we have an api that requires the search terms to be part of the URL path. Passing the output directly to the proxy_pass directive caused it to throw a 502 even though the request was properly url encoded.
这是我们想出的解决方案:
Here's the solution we came up with:
location ~ /api/search(/.*) {
set $query $1;
proxy_pass http://127.0.0.1:3003$query;
}
"set"指令似乎使url编码保持完整(或从$ 1的正则表达式传回的内容中重新编码).
The "set" directive seems to keep the url encoding intact (or re-encodes from what the regex is passing back in $1).
这篇关于用作代理(proxy_pass)时,如何避免nginx用空格替换%20?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!