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

Nginx: redirect location based on multiple args

我设置了一个 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 语句。

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

例如:

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

详情见this document