Nginx:基于多个参数的重定向位置

问题描述 投票:0回答:1

我设置了nginx位置重定向,以将/my_route重定向到即将发布的页面,但允许/my_route?db=preview传递到代理服务器。

location /my_route {
  if ($arg_db != "preview") {
    rewrite ^ /coming-soon/ last;
  }
  <other location config for when db == preview>
}

我想增加一层复杂性,以支持即将推出的页面的多种语言。

破坏nginx的配置,但给你一个主意:

location /my_route {
  if ($arg_db != "preview") {
    if ($arg_lang == "es") {
      rewrite ^ /coming-soon/index_es.html last;
    }
    rewrite ^ /coming-soon/ last;
  }
  <other location config for when db == preview>
}

我知道if is evil,所以很高兴不再使用if,如果需要的话,但是我不知道看哪个方向。我只知道nginx不支持&&语句或嵌套的if语句。

nginx nginx-location
1个回答
0
投票

您可以使用map消除内部的if块。

例如:

map $arg_lang $mylang {
    default   /;
    es        /index_es.html;
}
server {
    ...
    rewrite ^ /coming_soon$mylang last;
    ...
}

有关详细信息,请参见this document

© www.soinside.com 2019 - 2024. All rights reserved.