【问题标题】:Nginx - Rewrite rule under proxy failedNginx - 代理下的重写规则失败
【发布时间】:2019-11-02 08:43:33
【问题描述】:

我对 proxy_pass 位置类型下的“基本”重写规则有一些问题:

location ~* /test1/network/v1/operator/ke3/dataUp {
              rewrite ^(?<begin>/test1/network/v1/operator/ke3/dataUp)(?<parametersPart>.*)(?<mustDie>/dataUp)$ $parametersPart break;
              proxy_pass http://server_preproduction;
              proxy_set_header X-Real-IP  $remote_addr;
              proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
              proxy_set_header X-Forwarded-Proto https;
              proxy_set_header X-Forwarded-Port 443;
              proxy_set_header Host $host;
       }

我希望任何电话都发送到:.../test1/network/v1/operator/ke3/dataUp?param1=GHJ&amp;param2=865/dataUp

等于:.../test1/network/v1/operator/ke3/dataUp?param1=GHJ&amp;param2=865

所以我只想解析参数部分,以便从原始请求中删除任何额外的 /dataUp。 但是当我尝试使用任何类型的正则表达式来执行此操作时,nginx 似乎会返回位置 / 并使用默认请求...

我确定使用了正确的位置,因为当我使用如下重写时:rewrite ^(?&lt;begin&gt;/test1/network/v1/operator/ke3/dataUp)(?&lt;parametersPart&gt;.*)$ TEST$parametersPart break; 代理服务器上收到的日志:TEST?param1=GHJ&param2=865/dataUp

我没有在 proxy_pass 末尾添加 / 因为我想替换所有 url。(但这不是强制性的!我尝试了很多组合...)

如果有人可以拯救我的一天:p

谢谢!!

【问题讨论】:

  • 您的rewrite 语句将不起作用。查询字符串不是用于匹配 rewritelocation 指令的 normalized URI 的一部分。
  • @RichardSmith 嘿,感谢您的回复,但显然我需要更精确我阅读了文档并尝试了另一种类型或 URI,我捕获了请求,但我可以在重写期间“完全”解析它。所以重写做了一些事情,例如我可以解析URI / test1/network/v1/operator/ke3/dataUp 的第一部分和带有参数的第二部分,但我无法清楚地解析参数......跨度>

标签: nginx nginx-location nginx-config


【解决方案1】:

我找到了一种使用简单 if 语句来操纵参数的方法...

    if ($query_string ~ "^(?<argsok>/dataUp.*)(?<argsko>/dataUp)$") {
        proxy_pass http://server_preproduction/$argsok;
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-11-12
    • 2010-11-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-05
    • 1970-01-01
    相关资源
    最近更新 更多