通过Apache配置限制访问频率,可以使用mod_evasive
模块或mod_security
模块。以下是使用这两个模块的方法:
安装mod_evasive
在Debian/Ubuntu系统上:
sudo apt-get install libapache2-mod-evasive
在CentOS/RHEL系统上:
sudo yum install mod_evasive
启用mod_evasive
在Debian/Ubuntu系统上:
sudo a2enmod evasive
在CentOS/RHEL系统上:
sudo systemctl enable mod_evasive
配置mod_evasive
编辑Apache配置文件(通常是/etc/apache2/apache2.conf
或/etc/httpd/conf/httpd.conf
),添加以下内容:
<IfModule mod_evasive20.c>
DOSHashTableSize 3097
DOSPageCount 2
DOSSiteCount 50
DOSPageInterval 1
DOSSiteInterval 1
DOSBlockingPeriod 10
</IfModule>
解释:
DOSHashTableSize
: 哈希表的大小,用于存储IP地址和请求信息。DOSPageCount
: 在指定时间间隔内允许的最大页面请求次数。DOSSiteCount
: 在指定时间间隔内允许的最大站点请求次数。DOSPageInterval
: 页面请求的时间间隔(秒)。DOSSiteInterval
: 站点请求的时间间隔(秒)。DOSBlockingPeriod
: 被阻止的IP地址将被阻止的时间(秒)。重启Apache
sudo systemctl restart apache2
安装mod_security
在Debian/Ubuntu系统上:
sudo apt-get install libapache2-mod-security2
在CentOS/RHEL系统上:
sudo yum install mod_security
启用mod_security
在Debian/Ubuntu系统上:
sudo a2enmod security2
在CentOS/RHEL系统上:
sudo systemctl enable mod_security
配置mod_security
编辑Apache配置文件(通常是/etc/apache2/apache2.conf
或/etc/httpd/conf/httpd.conf
),添加以下内容:
<IfModule security2_module>
SecAction "id:500001,\
phase:2,\
nolog,\
pass,\
initcol:ip=%{REMOTE_ADDR},\
setvar:ip.request_count=+1,\
expirevar:ip.request_count=60"
SecRule IP:REQUEST_COUNT "@gt 10" \
"id:500002,\
phase:2,\
pass,\
log,\
msg:'Too many requests',\
status:429,\
tag:'application-multi',\
tag:'language-multi',\
tag:'platform-multi',\
tag:'attack-reconnaissance',\
tag:'OWASP_CRS/WEB_ATTACK/REQUEST_RATE_EXCESSIVE',\
tag:'WASCTC/WASC-21',\
tag:'OWASP_TOP_10/A7',\
tag:'OWASP_AppSensor/CIE1',\
tag:'PCI/6.5.10',\
setvar:ip.request_count=0"
</IfModule>
解释:
SecAction
: 初始化IP地址的请求计数器,并设置过期时间为60秒。SecRule
: 如果IP地址的请求计数超过10次,则返回429状态码(Too Many Requests),并重置计数器。重启Apache
sudo systemctl restart apache2
通过以上步骤,你可以使用mod_evasive
或mod_security
模块来限制Apache服务器的访问频率。选择哪个模块取决于你的具体需求和偏好。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
推荐阅读:Apache配置中如何限制访问频率