【问题标题】:Nginx: Use different backend based on value of argumentNginx:根据参数的值使用不同的后端
【发布时间】:2014-07-20 20:38:29
【问题描述】:

我们有一个用 php 编写的遗留应用程序,现在我们正在迁移到 java。 应用程序很大,我们正在尝试部分迁移功能。 记住这种情况,我需要根据查询字符串参数的值在 php-fpm 后端和 java 应用程序之间拆分流量

例如 如果 $format="csv",使用 fast-cgi 并使用 php 处理请求 如果 $format="xml",使用 proxy_pass 指令连接到 java 后端。

不幸的是,我发现在 nginx 上很难做到这一点。

我尝试了以下

if ($args_format ="csv")
 include php;
if ($args_format ="xml")
 include proxy;

这里的php和proxy是包含proxy_pass和fast-cgi相关语句的文件

不幸的是,这会引发语法错误

然后我使用类似的东西创建地图

map $args_output $provider {
  default "proxy";
  csv      "php";
}

然后做了一个 包括 $provider;

这也失败了,因为 nginx 似乎在开始时而不是在每次调用执行期间加载包含。

关于如何以优雅的方式实现这一点的任何建议。

【问题讨论】:

    标签: nginx fastcgi proxypass


    【解决方案1】:

    变量名是$arg_format http://nginx.org/en/docs/http/ngx_http_core_module.html#var_arg_

    您必须在 if 语句之后使用 { }。 http://nginx.org/en/docs/http/ngx_http_rewrite_module.html#if

    尝试类似...

    if ($arg_format ="csv") {
     include php;
    }
    if ($arg_format ="xml") {
     include proxy;
    }
    

    【讨论】:

    • 我们不能在 if 条件中包含包含。如果在运行时执行,而在解析配置文件时似乎会发生包含。
    猜你喜欢
    • 1970-01-01
    • 2021-11-30
    • 1970-01-01
    • 1970-01-01
    • 2021-10-25
    • 2015-07-24
    • 2017-11-30
    • 2021-03-18
    • 2019-10-25
    相关资源
    最近更新 更多