NGINX 在 if 块中使用 Custom/Map 变量

NGINX Use Custom/Map variable in if block

NGINX 1.9x / RHEL

如果未设置 cookie,我正在尝试 return 为特定请求返回 204。我无法让 nginx.conf 文件通过配置测试或重新启动。尝试针对创建的 MAP 变量进行测试时,第一个 IF 块失败。

    http block...
    map $http_cookie $my_login_cookie {
      default 0;
      "~hello_logged_in" 1;
    }


    Server Block....

    location ~ /$ {

      if ($my_login_cookie = 0) { <<<<<< Statement is not working
          if ($args ~ "^Blah=(.*)") {
            return 204;
          }
       }
    }

我发现很多代码示例表明这种事情应该是可行的。我错过了什么?!?!

<<<<<< 更新的最终工作代码 >>>>>>

    http block...
    map $http_cookie $my_login_cookie {
      default 0;
      "~hello_logged_in" 1;
    }

    Server Block....

    location ~ /$ {

      set $my_redirect y;

      if ($my_login_cookie = 0) {
         set $my_redirect "${my_redirect}e";
      }

      if ($args ~ "^blah=(.*)") {
         set $my_redirect "${my_redirect}s";
      }

      if ($my_redirect = "yes") {
          return 204;
      }

   }

nginx 不支持嵌套 ifs 和多条件 ifs。

您在 if ($my_login_cookie = 0) 之后缺少 { 否则您会在 if ($args ~ "^Blah=(.*)") { 行收到以下警告 "if" directive is not allowed here

您的配置的可能解决方案:

# test whether my_login_cookie is set
if ($my_login_cookie = 0) {
  set $test C;
}

# test
if ($args ~ "^Blah=(.*)") {
  set $test "${test}A";
}

# if both of the above tests are true return
if ($test = CA) {
  return 204;
}