按 ip 范围访问文件

2022-08-30 12:01:19

如何仅允许IP地址范围内的IP用户访问文件?

例如,文件管理员.php。范围从 0.0.0.0 到 1.2.3.4。

我只需要配置对一个文件的访问权限,而不是目录。


答案 1

只需添加 FilesMatchFiles 指令即可将其限制为特定脚本。

以下内容将阻止访问所有以“admin.php”结尾的脚本:

<FilesMatch "admin\.php$">
    Order deny,allow
    Deny from all
    Allow from 10.0.0.0/24
</FilesMatch>

以下内容只会阻止管理员.php:

<Files "admin.php">
    Order deny,allow
    Deny from all
    Allow from 10.0.0.0/24
</Files>

有关更多信息,请参阅配置节上的 apache 文档。


答案 2

检查允许指令的手册页

Order Deny,Allow
Deny from all
Allow from 10.1.0.0/255.255.0.0

部分 IP 地址

例:

Allow from 10.1
Allow from 10 172.20 192.168.2

IP 地址的前 1 到 3 个字节,用于子网限制。

网络/网络掩码对

例:

Allow from 10.1.0.0/255.255.0.0

一个网络 a.b.c.d 和一个网络掩码 w.x.y.z。用于更细粒度的子网限制。

网络/神经网络网段规范

例:

Allow from 10.1.0.0/16

与前一种情况类似,除了网络掩码由nnn高阶1位组成。


推荐