将多个目录限制在相同的IP范围

假设我在nginxconfiguration文件中有以下内容:

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 } 

如果我还想限制访问其他几个目录,是否可以这样做,而不必创build另一个块并重新列出IP? 我担心在未来添加和删除IP时会发生变化 – 我不想确保每个块都被更新。

更好的办法是基本上允许我以某种方式“包括”每个块下的IP列表。

只要我在上面的问题中input“包含”这个词,轮子就开始旋转在我的头上。

原来,你可以绝对只是把allowdeny指令到一个包含文件,他们将工作正如所料。 最重要的是,这意味着我可以将IP列表合并,以便某些服务器组可以访问某些目录,而其他服务器则不能访问这些目录。

我有这样的设置:

的/ etc / nginx的/包括/pipe理-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的/包括/testing-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 } 

奇迹般有效。