nginx – 将多个目录限制为相同的IP范围

假设我在nginx配置文件中有以下内容:

location ^~ /foo/ {
    allow 1.2.3.4;
    allow 5.6.7.8;
    allow 9.10.11.12;
    …
    allow 99.100.101.102;
    deny all;
    # rest of directives
}

如果我还想限制对其他几个目录的访问,是否可以这样做而无需创建另一个块并重新列出IP?我担心的是在将来添加和删除IP时进行更改 – 我不希望确保每个块都已更新.

更好的是一个指令,它基本上允许我以某种方式“包含”每个块下的IP列表.

最佳答案
一旦我在上面的问题中输入“包含”这个词,轮子开始在我脑海中旋转.

事实证明,您绝对可以将allow和deny指令放入包含文件中,它们将按预期工作.最重要的是,这意味着我可以组合IP列表,以便某些服务器组可以访问某些目录而其他服务器则无法访问.

我把它设置成这样:

的/ etc / nginx的/包括/管理-IPS

allow 1.2.3.4/32;
allow 1.2.3.5/32;

的/ etc / nginx的/包括/私有-IPS

allow 10.0.0.0/8;
allow 172.16.0.0/12;
allow 192.168.0.0/16;

的/ etc / nginx的/包括/测试-IPS

allow 4.5.6.7;
allow 8.9.10.11;

/etc/nginx/conf.d/server.conf

location ^~ /admin/ {
    include includes/admin-ips;
    deny all;
    # rest of directives
}

location ^~ /tools/ {
    include includes/admin-ips;
    include includes/testing-ips;
    include includes/private-ips;
    deny all;
    # rest of directives
}

location ^~ /tests/ {
    include includes/admin-ips;
    include includes/testing-ips;
    deny all;
    # rest of directives
}

奇迹般有效.

dawei

【声明】:唐山站长网内容转载自互联网,其相关言论仅代表作者个人观点绝非权威,不代表本站立场。如您发现内容存在版权问题,请提交相关链接至邮箱:bqsm@foxmail.com,我们将及时予以处理。