加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 运营中心 > Nginx > 正文

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

发布时间:2020-12-13 21:11:56 所属栏目:Nginx 来源:网络整理
导读:假设我在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} 如果我还想限制对其他几个目录的访问,是否可以这样做而无需创建另一个块并重新列出I

假设我在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
}

奇迹般有效.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读