nginx proxy_pass url 与有条件的 GET 参数

标签 nginx proxypass

我有一个网址http://foo.com/banana,我还有另一个网址http://foo.com/banana?a=1&b=2

我喜欢所有 /banana 路由都由本地 nginx 处理,但我希望任何带有 GET 参数的香蕉路由都代理到 http://bar.com

所以:

http://foo.com/banana -> http://foo.com/banana

http://foo.com/banana?a=1 -> (代理) -> http://bar.com/banana? a=1

我应该注意,这用于生产。我正在尝试重定向 api 调用以在开发过程中重定向到另一台服务器。

我尝试执行“if args” block ,但无法在 if block 中执行 proxy_pass。 我考虑重写: http://foo.com/banana?a=1 -> http://foo.com/proxy?a=1

location /proxy {
   proxy_pass http://bar.com;
}

但是我没有上面的正确逻辑,因为 bar.com 期望 /banana 路由。

有什么想法吗?

最佳答案

由于这不适用于生产,因此您可以坚持使用原来的“if”解决方案。您只需要逃离“if” block 就可以使用proxy_pass,这可以使用传统技巧轻松完成:

location /banana {
    error_page 418 = @good_old_fallback;

    if ($args) {
        return 418;
    }
}

location @good_old_fallback {
    proxy_pass http://bar.com;
}

您使用其他位置的想法也可行,因此,如果您更喜欢它,您可以采用如下方式:

location /banana {
    if ($args) {
        rewrite ^/banana(.*)$ /proxy$1 last;
    }
}

location /proxy {
    internal;
    rewrite ^/proxy(.*)$ /banana$1 break;
    proxy_pass http://bar.com;
}

关于nginx proxy_pass url 与有条件的 GET 参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30063744/

相关文章:

redirect - 如何重定向并删除 URL 中的 .html 扩展名?

mysql - PHP session 不会转移到其他页面

docker - 对Docker容器应用程序使用Docker Nginx反向代理

nginx - 使用 proxy_pass 配置 Nginx

apache - centOS htaccess 文件上的 Proxypass 和 ProxyPassReverse 复制

nginx - proxy_pass后如何将HTTPS协议(protocol)保存为HTTP协议(protocol)?

php - nginx:连接到上游时连接()失败(111:连接被拒绝)

PHP file_exists 或 is_file 在 NFS 文件 (EC2) 上无法正确回答 10-20s

apache - 设置 HTTP 代理以插入 header

Apache AJP,ProxyPass : How to set the Host header