Ubuntu上配置Apache防盗刷的实用方案
一 核心思路与适用场景
二 快速落地步骤
启用速率限制模块并配置全局速率
<IfModule mod_ratelimit.c>
# 每个客户端IP每分钟最多 60 次请求
SetEnv rate-limit 60/minute
</IfModule>
安装并配置 mod_evasive 防刷与防DoS
<IfModule mod_evasive20.c>
DOSHashTableSize 3097
DOSPageCount 2 # 同一页面在 DOSPageInterval 内的请求上限
DOSSiteCount 50 # 同一站点在 DOSSiteInterval 内的请求上限
DOSPageInterval 1 # 单位:秒
DOSSiteInterval 1 # 单位:秒
DOSBlockingPeriod 600 # 封禁时长:单位秒(建议至少 10 分钟)
</IfModule>
部署 Fail2Ban 自动封禁恶意来源
[DEFAULT]
bantime = 600
findtime = 600
maxretry = 3
[apache-auth]
enabled = true
filter = apache-auth
action = iptables-multiport[name=Apache, port="http,https", protocol=tcp]
logpath = /var/log/apache2/access.log
保护敏感接口与后台
<Directory /var/www/html/protected>
AuthType Basic
AuthName "Restricted Area"
AuthUserFile /etc/apache2/.htpasswd
Require valid-user
</Directory>
创建用户:sudo htpasswd -c /etc/apache2/.htpasswd username<div class="g-recaptcha" data-sitekey="YOUR_SITE_KEY"></div>
<script src="https://www.google.com/recaptcha/api.js" async defer></script>
$response = file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=YOUR_SECRET_KEY&response=".$_POST['g-recaptcha-response']);
$data = json_decode($response);
if ($data->success) { /* 处理业务 */ } else { /* 拒绝请求 */ }
资源防盗链(保护图片、视频、下载)
RewriteEngine On
RewriteCond %{HTTP_REFERER} !^https?://(www\.)?yourdomain\.com [NC]
RewriteCond %{HTTP_REFERER} !^https?://(www\.)?cdn\.yourdomain\.com [NC]
RewriteRule \.(jpg|jpeg|png|gif|ico|css|js)$ - [F,L]
三 进阶可选方案
四 调参与运维建议