网站被DDoS攻击或CC攻击是站长最头疼的问题之一——攻击来了网站直接瘫痪,正常用户无法访问,搜索引擎抓取失败影响收录。对于预算有限的中小站长来说,不可能购买昂贵的高防IP。本文将系统讲解低成本的防御方案,从Nginx限流到Cloudflare防护到iptables封禁。
一、攻击类型识别
1. DDoS流量攻击

大量垃圾流量塞满带宽,导致正常请求无法到达服务器。特征:带宽使用率突然达到上限。
2. CC攻击(HTTP Flood)
大量真实HTTP请求耗尽服务器资源(CPU、内存、数据库连接)。特征:CPU和内存使用率飙升,PHP-FPM进程耗尽。
3. 如何判断当前正在被攻击
# 查看连接数
netstat -an | grep :80 | wc -l
# 正常值:<1000;攻击时可能>10000
# 查看单IP连接数
netstat -an | grep :80 | awk '{print $5}' | cut -d: -f1 | sort | uniq -c | sort -rn | head -20
# 查看PHP-FPM进程数
ps aux | grep php-fpm | wc -l
# 如果远超pm.max_children,说明被CC攻击
# 查看Nginx日志QPS
tail -10000 /www/wwwlogs/yoursite.log | awk '{print $4}' | cut -d: -f2 | sort | uniq -c
二、Nginx限流配置
1. 基于IP的请求限流
# nginx.conf
# 定义限流区域(10MB可存储16万个IP状态)
limit_req_zone $binary_remote_addr zone=api:10m rate=30r/s;
limit_req_zone $binary_remote_addr zone=general:10m rate=10r/s;
server {
# 普通页面:每秒10个请求,突发允许20个
limit_req zone=general burst=20 nodelay;
# API接口:更严格
location /api/ {
limit_req zone=api burst=10 nodelay;
}
# 返回429而非503
limit_req_status 429;
}
2. 基于IP的连接数限制
limit_conn_zone $binary_remote_addr zone=conn_limit:10m;
server {
# 单IP最大并发连接数
limit_conn conn_limit 30;
}
3. 针对特定URI的防护
# 保护登录接口
location = /zb_system/cmd.php {
if ($args ~* "act=verify") {
limit_req zone=api burst=5 nodelay;
}
}
# 保护搜索接口
location /search {
limit_req zone=general burst=3 nodelay;
}
三、Cloudflare免费防护
Cloudflare免费版提供基础的DDoS和CC防护,对中小站长来说性价比极高:
1. Under Attack模式
# 攻击时立即开启 # Cloudflare Dashboard → Security → Security Level → "I'm Under Attack" # 所有访问者需要通过JS Challenge验证(5秒等待页面) # 正常浏览器自动通过,攻击脚本无法通过
2. WAF规则
# 自定义防火墙规则(免费版支持5条)
# 规则1:屏蔽已知攻击IP段
IP Source Address is in {攻击IP段} → Block
# 规则2:限制特定国家(如只服务中国)
IP Source Address is not in {China} → Challenge
# 规则3:屏蔽无User-Agent的请求
HTTP Header "User-Agent" equals "" → Block
# 规则4:屏蔽可疑的请求路径
URI Path contains "wp-admin" and not IP Source Address is in {你的IP} → Block
3. Rate Limiting
# Cloudflare Rate Limiting # 免费版:10万请求/月 # 规则:同IP 10秒内超过50次请求 → Challenge # 路径:*yoursite.com/*
4. 验证码挑战
# Managed Challenge(推荐) # Cloudflare自动判断是否需要验证码 # 可疑流量显示Turnstile验证码 # 正常流量直接通过 # 配置: # Security → Bots → Bot Fight Mode → 开启 # Super Bot Fight Mode → 开启
四、iptables防火墙规则
1. 紧急封IP
# 攻击时快速封禁
iptables -I INPUT -s 攻击IP -j DROP
# 批量封禁
for ip in $(cat attack_ips.txt); do
iptables -I INPUT -s $ip -j DROP
done
# 查看封禁列表
iptables -L INPUT -n --line-numbers
# 解封
iptables -D INPUT 序号
2. 自动封禁脚本
#!/bin/bash
# /root/auto_ban.sh
LOG=/www/wwwlogs/yoursite.log
BANNED=/root/banned_ips.txt
MAX_CONN=100 # 单IP最大并发
# 统计当前连接数
netstat -an | grep :80 | awk '{print $5}' | cut -d: -f1 | sort | uniq -c | sort -rn | while read count ip; do
if [ $count -gt $MAX_CONN ] && [ "$ip" != "127.0.0.1" ] && [ "$ip" != "你的IP" ]; then
if ! grep -q "$ip" $BANNED 2>/dev/null; then
iptables -I INPUT -s $ip -j DROP
echo "$ip $(date) conn=$count" >> $BANNED
echo "Banned $ip (connections: $count)"
fi
fi
done
3. 地理IP封锁
# 如果只服务国内用户
# 安装ipset
yum install ipset -y
# 下载中国IP列表
curl -s https://raw.githubusercontent.com/17mon/china_ip_list/master/china_ip_list.txt > /root/cn_ips.txt
# 创建白名单
ipset create cn_ips hash:net
while read ip; do
ipset add cn_ips $ip 2>/dev/null
done < /root/cn_ips.txt
# 只允许中国IP访问HTTP/HTTPS
iptables -A INPUT -m set --match-set cn_ips src -p tcp --dport 80 -j ACCEPT
iptables -A INPUT -m set --match-set cn_ips src -p tcp --dport 443 -j ACCEPT
iptables -A INPUT -p tcp --dport 80 -j DROP
iptables -A INPUT -p tcp --dport 443 -j DROP
五、宝塔面板防火墙
宝塔面板的Nginx防火墙插件提供了可视化的防护配置:
# 安装:软件商店 → Nginx防火墙 → 安装 # 核心配置: # - CC防护:开启,周期1秒,频率60次 # - GET过滤:开启 # - POST过滤:开启 # - URL黑名单:添加攻击URL模式 # - IP黑名单:添加攻击IP # - User-Agent过滤:屏蔽可疑UA # - 恶意文件上传拦截:开启
宝塔防火墙的优势是界面直观,但精细控制不如手动配置iptables/Cloudflare。
六、攻击后的恢复
1. 搜索引擎抓取恢复
# 攻击期间搜索引擎可能抓取到503/502错误 # 恢复后主动推送关键页面给百度 curl -H 'Content-Type:text/plain' --data @urls.txt \ "http://data.zz.baidu.com/urls?site=www.yoursite.com&token=YOUR_TOKEN" # 在百度搜索资源平台查看抓取异常 # 如果有抓取失败记录,提交重新抓取请求
2. 日志分析与防御优化
# 分析攻击特征,针对性加强防御 # 常见攻击特征: # - 固定User-Agent # - 固定Referer # - 无Cookie # - 访问特定URL模式 # - 来自特定IP段
七、防御方案总结
按成本从低到高的防御体系:
- 零成本:Nginx限流 + Cloudflare免费防护(Under Attack模式)
- 低成本:上述 + iptables自动封禁 + 宝塔防火墙
- 中成本:上述 + Cloudflare Pro(20$/月,更智能的WAF)
- 高成本:高防IP(500-5000元/月)
对99%的中小站长来说,Cloudflare免费版 + Nginx限流 + iptables自动封禁这套组合拳已经足够应对绝大多数攻击场景。
关注西数资源网,获取更多DDoS防护、网站安全和站长资源实战干货!
最后修改时间:
2026年虚拟主机与VPS选择完全指南:配置对比适用场景选购策略与迁移方案
上一篇
2026年05月12日 04:10
2026年网站流量统计深度分析指南:百度统计GA4高级用法热力图与数据驱动运营
下一篇
2026年05月12日 04:18
相关文章
发表评论
评论列表