如何使用apache禁止请求特定目录的IP地址

How to ban IP addresses requesting specific directory using apache

您好,我想永久禁止任何请求目录“/backups/”的内容,现在我该如何使用PHP脚本或[=16] =]?

如果可能的话,我想以与 iptables 类似的方式执行此操作。

我自己想出了如何做到这一点。

在 /bin 中创建一个名为“blockip”的脚本,其中包含以下内容

if [ -z "" ]
  then
    echo "No argument supplied"
    exit 1
fi
echo "Blocking IP address "
iptables -A INPUT -s  -j DROP

然后

chmod +x /bin/blockip

现在 运行

sudo visudo

添加这个:

nobody ALL = NOPASSWD: /your/script

确保它是 chmod 755

并将此代码添加到目录中的 index.php。

exec("sudo blockip ".escapeshellcmd($_SERVER['REMOTE_ADDR']));