首页 > 解决方案 > nginx:if条件中的“allow”指令

问题描述

我的网络应用程序根据子域加载不同的视图,并且子域是动态的,所以我像这样启动虚拟主机配置:

server {
listen                       443 ssl http2 default_server;
server_name                   ~^(?<subdomain>.+)\.test\.com$;
add_header                   Strict-Transport-Security "max-age=31536000; includeSubdomains;";
...
}

稍后在 server 指令中,我指定了访问控制:

  location / {
    root                       /usr/share/webapp/;
    index                      /index.html;
    try_files                  $uri $uri/ =404;
    allow                      my.ip.sub.net/24;
    deny                       all;
  }

现在我想公开某些子域。试试这个:

  location / {
    root                       /usr/share/webapp/;
    index                      /index.html;
    try_files                  $uri $uri/ =404;
    allow                      my.ip.sub.net/24;
    if ($subdomain = publicenv) {
      allow                       all;
    }
    deny                        all;
  }

给我这个错误信息:

nginx: [emerg] "allow" directive is not allowed here in /etc/nginx/conf.d/https.conf:119

甚至可以根据变量动态加载白名单选项吗?

标签: nginxnginx-config

解决方案


推荐阅读