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

前端之家收集整理的这篇文章主要介绍了nginx – 将多个目录限制为相同的IP范围前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

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

  1. location ^~ /foo/ {
  2. allow 1.2.3.4;
  3. allow 5.6.7.8;
  4. allow 9.10.11.12;
  5. allow 99.100.101.102;
  6. deny all;
  7. # rest of directives
  8. }

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

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

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

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

我把它设置成这样:

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

  1. allow 1.2.3.4/32;
  2. allow 1.2.3.5/32;

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

  1. allow 10.0.0.0/8;
  2. allow 172.16.0.0/12;
  3. allow 192.168.0.0/16;

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

  1. allow 4.5.6.7;
  2. allow 8.9.10.11;

/etc/Nginx/conf.d/server.conf

  1. location ^~ /admin/ {
  2. include includes/admin-ips;
  3. deny all;
  4. # rest of directives
  5. }
  6. location ^~ /tools/ {
  7. include includes/admin-ips;
  8. include includes/testing-ips;
  9. include includes/private-ips;
  10. deny all;
  11. # rest of directives
  12. }
  13. location ^~ /tests/ {
  14. include includes/admin-ips;
  15. include includes/testing-ips;
  16. deny all;
  17. # rest of directives
  18. }

奇迹般有效.

猜你在找的Nginx相关文章