nginx中很少有条件可以满足

问题描述

我目前正在尝试允许满足以下任何条件的任何人访问nginx中的文件夹:

  • 从10.0.0.0/24子网访问。
  • 从192.168.1.0/24子网访问并进行身份验证。

我在该位置有以下代码:

location /private {
        satisfy any;

        allow 10.0.0.0/24;
        deny all;

        auth_basic      "Admin access";
        auth_basic_user_file    /etc/nginx/.htpasswd;
}

但是这不起作用,因为我希望允许来自10.0.0.0/24(无身份验证)的任何人或来自192.168.1.0/24并进行身份验证的任何人的访问。

上面的代码可以让未经身份验证的10.0.0.0/24人和所有进行身份验证的人进入,因此我只允许192.168.1.0/24中的人进行身份验证,在其他情况下,它将拒绝访问。 / p>

解决方法

一旦我尝试执行相同操作,仅凭allow / deny / auth规则就无法做到。您可以尝试做的是

map $remote_addr $disallow {
    ~^10\.0\.0\.     "";
    ~^192\.168\.1\.  "";
    default          1;
}
server {
    ...
    location /private {
        if ($disallow) { return 403; }
        satisfy any;

        allow 10.0.0.0/24;
        deny all;

        auth_basic      "Admin access";
        auth_basic_user_file    /etc/nginx/.htpasswd;
   }
   ...
}

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...