Nginx中CC攻擊與DDoS防御的高級策略指南
更新時間:2025年07月06日 08:59:48 作者:碼上有潛
這篇文章主要為大家詳細介紹了Nginx中CC攻擊與DDoS防御的高級策略,文中的示例代碼講解詳細,感興趣的小伙伴可以跟隨小編一起學習一下
一、分層防御體系架構

二、CC 攻擊防御策略
1. 請求頻率限制(核心防御)
# 在 http 塊中定義限制區(qū)域
http {
# 定義請求限制區(qū)域
limit_req_zone $binary_remote_addr zone=req_perip:10m rate=10r/s;
limit_req_status 429; # 自定義限流狀態(tài)碼
# 定義并發(fā)連接限制
limit_conn_zone $binary_remote_addr zone=conn_perip:10m;
}
2. 關鍵位置應用限流
server {
# 登錄接口嚴格限制
location = /login {
limit_req zone=req_perip burst=5 nodelay;
limit_conn conn_perip 3;
proxy_pass http://backend;
}
# API 接口限制
location /api/ {
limit_req zone=req_perip burst=10;
limit_conn conn_perip 5;
proxy_pass http://backend;
}
# 靜態(tài)資源寬松限制
location ~* \.(js|css|png|jpg)$ {
limit_req zone=req_perip burst=20;
access_log off; # 減少日志壓力
}
}
3. 人機驗證挑戰(zhàn)
location / {
# 當請求超過閾值時重定向到驗證頁面
error_page 429 = @verify;
limit_req zone=req_perip burst=15 nodelay;
# 正常請求處理
proxy_pass http://backend;
}
location @verify {
# 返回驗證碼挑戰(zhàn)
add_header Content-Type text/html;
return 200 '<html><body>
<h1>請驗證</h1>
<script src="https://challenges.cloudflare.com/turnstile/v0/api.js"></script>
<div class="cf-turnstile" data-sitekey="YOUR_SITE_KEY"></div>
</body></html>';
}
三、DDoS 防御策略
1. 連接限制配置
# 全局連接限制
events {
worker_connections 4096; # 根據服務器性能調整
}
http {
# 限制單個IP的連接數
limit_conn_zone $binary_remote_addr zone=conn_limit_perip:10m;
limit_conn conn_limit_perip 50;
# 限制每個連接的速率
limit_rate 500k; # 全局默認限速
# 限制請求體大小
client_max_body_size 10m;
}
2. 慢連接防護
http {
# 防止慢速攻擊
client_body_timeout 10s; # 請求體超時
client_header_timeout 10s; # 請求頭超時
keepalive_timeout 15s; # 保持連接超時
send_timeout 10s; # 發(fā)送響應超時
# 關閉不必要的HTTP方法
if ($request_method !~ ^(GET|HEAD|POST)$ ) {
return 444;
}
}
3. 高級防護模塊
# 啟用Nginx Plus或第三方模塊
load_module modules/ngx_http_modsecurity_module.so;
load_module modules/ngx_http_geoip2_module.so;
http {
# 使用GeoIP限制地區(qū)訪問
geoip2 /usr/share/GeoIP/GeoLite2-Country.mmdb {
$geoip2_country_code country iso_code;
}
map $geoip2_country_code $allowed_country {
default 0;
CN 1; # 只允許中國IP
US 1;
JP 1;
}
server {
# 應用地區(qū)限制
if ($allowed_country = 0) {
return 403;
}
# 啟用WAF
modsecurity on;
modsecurity_rules_file /etc/nginx/modsec/main.conf;
}
}
四、Nginx 調優(yōu)增強防御
1. 內核級優(yōu)化 (sysctl.conf)
# 防止SYN洪水攻擊 net.ipv4.tcp_syncookies = 1 net.ipv4.tcp_max_syn_backlog = 4096 # 加快TIME-WAIT回收 net.ipv4.tcp_tw_reuse = 1 net.ipv4.tcp_fin_timeout = 30 # 連接追蹤優(yōu)化 net.netfilter.nf_conntrack_max = 1000000 net.netfilter.nf_conntrack_tcp_timeout_established = 1200
2. Nginx 工作進程優(yōu)化
# 調整工作進程
worker_processes auto; # 自動匹配CPU核心
worker_rlimit_nofile 65535; # 每個進程最大文件描述符
# 使用多核處理連接
events {
use epoll;
worker_connections 4096;
multi_accept on;
}
3. 緩存優(yōu)化減少后端壓力
proxy_cache_path /var/cache/nginx levels=1:2 keys_zone=my_cache:10m max_size=1g inactive=60m;
server {
location / {
proxy_cache my_cache;
proxy_cache_valid 200 302 10m;
proxy_cache_valid 404 1m;
proxy_cache_use_stale error timeout updating;
# 緩存鎖定防止緩存擊穿
proxy_cache_lock on;
proxy_cache_lock_timeout 5s;
}
}
五、云平臺集成防御
1. 阿里云/騰訊云集成
# 通過HTTP頭傳遞真實客戶端IP
real_ip_header X-Forwarded-For;
set_real_ip_from 100.64.0.0/10; # 云平臺內網段
# 啟用云平臺WAF
location / {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
# 云平臺特定頭
proxy_set_header Ali-CDN-Real-IP $remote_addr;
proxy_set_header Qcloud-Real-IP $remote_addr;
}
2. Cloudflare 集成
# 只接受來自Cloudflare IP的請求
include /etc/nginx/cloudflare-ips.conf;
server {
listen 80;
server_name yourdomain.com;
# 僅允許Cloudflare IP訪問
allow 103.21.244.0/22;
allow 103.22.200.0/22;
# ... 其他Cloudflare IP段
deny all;
location / {
proxy_pass http://backend;
}
}
六、監(jiān)控與自動化響應
1. 實時監(jiān)控配置
# 啟用狀態(tài)監(jiān)控
server {
listen 127.0.0.1:8080;
location /nginx_status {
stub_status on;
access_log off;
allow 127.0.0.1;
deny all;
}
}
2. 自動封禁腳本
#!/bin/bash
# 自動封禁高頻IP
LOG_PATH="/var/log/nginx/access.log"
THRESHOLD=100 # 每分鐘請求閾值
BAN_TIME=3600 # 封禁時間(秒)
tail -F $LOG_PATH | while read LINE
do
IP=$(echo $LINE | awk '{print $1}')
COUNT=$(grep $IP $LOG_PATH | wc -l)
if [ $COUNT -gt $THRESHOLD ]; then
# 添加到防火墻
iptables -A INPUT -s $IP -j DROP
# 定時解封
(sleep $BAN_TIME && iptables -D INPUT -s $IP -j DROP) &
fi
done
3. Prometheus + Grafana 監(jiān)控面板
# nginx-prometheus-exporter 配置
scrape_configs:
- job_name: 'nginx'
static_configs:
- targets: ['nginx-host:9113']
metrics_path: /metrics
監(jiān)控指標:
- 請求率
- 活動連接數
- 錯誤率
- 帶寬使用
- 上游響應時間
七、應急響應計劃
攻擊發(fā)生時的處理流程:
1.啟用緊急模式:
# 在http塊添加
limit_req_zone $binary_remote_addr zone=emergency:10m rate=5r/s;
server {
location / {
limit_req zone=emergency burst=10 nodelay;
}
}
2.切換至靜態(tài)維護頁面:
location / {
root /var/www/emergency;
try_files $uri /maintenance.html;
}
3.啟用云平臺DDoS防護:
- 阿里云:啟用DDoS高防IP
- 騰訊云:啟用大禹BGP高防
- AWS:啟用Shield Advanced
4.分析攻擊模式:
# 分析訪問日志
awk '{print $1}' access.log | sort | uniq -c | sort -nr | head -20
# 實時監(jiān)控
tail -f access.log | grep -E ' (429|444|503) '
最佳實踐總結
- 分層防御:網絡層 + Nginx層 + 應用層
- 速率限制:基于IP和關鍵端點
- 連接管理:限制并發(fā)和超時
- 自動封禁:實時監(jiān)控 + 自動響應
- 云平臺集成:利用云WAF和DDoS防護
- 定期演練:每季度進行防御壓力測試
關鍵建議:對于大規(guī)模DDoS攻擊,建議結合云服務商的DDoS防護服務(如阿里云高防IP、Cloudflare Pro),這些服務提供TB級的清洗能力,遠超單臺Nginx服務器的防御能力。
最終部署前,務必進行壓力測試:
# 使用wrk進行壓力測試 wrk -t12 -c400 -d30s --timeout 10s https://yourdomain.com
根據測試結果調整限流閾值和防護策略,確保在防護攻擊的同時不影響正常用戶訪問。
到此這篇關于Nginx中CC攻擊與DDoS防御的高級策略指南的文章就介紹到這了,更多相關Nginx防御CC攻擊與DDoS內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

