且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

用作代理(proxy_pass)时,如何避免nginx用空格替换%20?

更新时间:2023-02-23 10:13:56

我能够解决类似的问题-我们有一个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).