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

Jor*_*ter 4 configuration 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
}
Run Code Online (Sandbox Code Playgroud)

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

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

Jor*_*ter 8

当我在上面的问题中输入“包含”这个词时,轮子开始在我脑海中旋转。

原来,你完全可以只把allowdeny指令到一个包含文件和预期,他们将只是工作。最重要的是,这意味着我可以组合 IP 列表,以便某些服务器组可以访问某些目录,而另一些则不能。

我是这样设置的:

/etc/nginx/includes/admin-ips

allow 1.2.3.4/32;
allow 1.2.3.5/32;
Run Code Online (Sandbox Code Playgroud)

/etc/nginx/includes/private-ips

allow 10.0.0.0/8;
allow 172.16.0.0/12;
allow 192.168.0.0/16;
Run Code Online (Sandbox Code Playgroud)

/etc/nginx/includes/testing-ips

allow 4.5.6.7;
allow 8.9.10.11;
Run Code Online (Sandbox Code Playgroud)

/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
}
Run Code Online (Sandbox Code Playgroud)

奇迹般有效。