在 CentOS + Apache 环境下,实现“限流”常见有几种方式,取决于你想限的是什么:
下面按常用方案说明。
适合防止慢速攻击、限制请求建立速度。
httpd -M | grep reqtimeout
<IfModule mod_reqtimeout.c>
RequestReadTimeout header=10-20,MinRate=500
RequestReadTimeout body=10,MinRate=500
</IfModule>
含义:
重启:
systemctl restart httpd
yum install epel-release -y
yum install mod_evasive -y
vi /etc/httpd/conf.d/mod_evasive.conf
示例:
<IfModule mod_evasive20.c>
DOSHashTableSize 3097
DOSPageCount 10
DOSSiteCount 50
DOSPageInterval 1
DOSSiteInterval 1
DOSBlockingPeriod 60
</IfModule>
含义:
重启:
systemctl restart httpd
✅ 适合 防刷接口、防 CC
yum install mod_qos -y
<IfModule mod_qos.c>
QS_LocRequestLimitMatch "^/api/" 5
QS_CondLocRequestLimitMatch "IP" "^/api/" 2
</IfModule>
含义:
/api/ 每秒最多 5 次重启:
systemctl restart httpd
✅ 适合 API 限流
<IfModule mpm_prefork_module>
StartServers 5
MinSpareServers 5
MaxSpareServers 10
MaxClients 150
MaxRequestsPerChild 3000
</IfModule>
或限制单 IP:
<Directory "/var/www/html">
Order Allow,Deny
Allow from all
</Directory>
yum install mod_ratelimit -y
<IfModule mod_ratelimit.c>
<Location "/download">
SetOutputFilter RATE_LIMIT
SetEnv rate-limit 100
</Location>
</IfModule>
含义:
/download 限速 100 KB/s| 场景 | 推荐 |
|---|---|
| 防 CC / 刷接口 | mod_evasive |
| API 限流 | mod_qos |
| 下载限速 | mod_ratelimit |
| 慢速攻击 | mod_reqtimeout |
apachectl configtest
systemctl status httpd
如果你能说明:
我可以给你一套可直接用的配置。