避免Nginx在proxy_pass上转义查询参数 [英] Avoid nginx escaping query parameters on proxy_pass

查看:1013
本文介绍了避免Nginx在proxy_pass上转义查询参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要代理域上的/api,我有此位置限制.

location ^~ /api/ {
    rewrite_log on;
    rewrite ^/api/(.*) /$1$is_args$args break;
    proxy_pass http://127.0.0.1:1337;
}

只要URL没有查询参数,它就可以正常工作,但是只要有,我就会在上游服务器上像这些Could not find path: /records%3fname=hoegh.io

一样出现错误

这里讨论的%3f是URL编码的?,并且由于它是URL编码,因此上游服务器无法识别它.这可能会有所延迟,但我希望可以让nginx正确处理此问题(即在将URL传递给代理之前不要转义URL).

有什么想法吗?

解决方案

您无需执行任何操作. $ args会自动传递.

如果要修改传递的$ args,则必须覆盖.

set $args "foo=bar";

例如.

工作解决方案应如下:

location ^~ /api/ {
    rewrite_log on;
    rewrite ^/api/(.*) /$1 break;
    proxy_pass http://127.0.0.1:1337;
}

I to proxy /api on a domain, I have this location block.

location ^~ /api/ {
    rewrite_log on;
    rewrite ^/api/(.*) /$1$is_args$args break;
    proxy_pass http://127.0.0.1:1337;
}

It works fine as long as the URLs do not have query parameters, but as soon as they do, I get errors on the upstream server like these Could not find path: /records%3fname=hoegh.io

The %3f in question here is an URL encoded ?, and since it's URL encoded, the upstream server does not recognize it. That might be retarded, but I was hoping it was possible to get nginx to handle this correctly (ie. not escape the URL before passing it to the proxy).

Any ideas?

解决方案

You don't need to do anything. $args is passed along automatically.

If you wish to modify the $args that are passed you have to override.

set $args "foo=bar";

for instance.

Working solution should go as follows:

location ^~ /api/ {
    rewrite_log on;
    rewrite ^/api/(.*) /$1 break;
    proxy_pass http://127.0.0.1:1337;
}

这篇关于避免Nginx在proxy_pass上转义查询参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆