Nginx中CC攻擊與DDoS防御的高級(jí)策略指南
一、分層防御體系架構(gòu)

二、CC 攻擊防御策略
1. 請(qǐng)求頻率限制(核心防御)
# 在 http 塊中定義限制區(qū)域
http {
# 定義請(qǐng)求限制區(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. 關(guān)鍵位置應(yīng)用限流
server {
# 登錄接口嚴(yán)格限制
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. 人機(jī)驗(yàn)證挑戰(zhàn)
location / {
# 當(dāng)請(qǐng)求超過(guò)閾值時(shí)重定向到驗(yàn)證頁(yè)面
error_page 429 = @verify;
limit_req zone=req_perip burst=15 nodelay;
# 正常請(qǐng)求處理
proxy_pass http://backend;
}
location @verify {
# 返回驗(yàn)證碼挑戰(zhàn)
add_header Content-Type text/html;
return 200 '<html><body>
<h1>請(qǐng)驗(yàn)證</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; # 根據(jù)服務(wù)器性能調(diào)整
}
http {
# 限制單個(gè)IP的連接數(shù)
limit_conn_zone $binary_remote_addr zone=conn_limit_perip:10m;
limit_conn conn_limit_perip 50;
# 限制每個(gè)連接的速率
limit_rate 500k; # 全局默認(rèn)限速
# 限制請(qǐng)求體大小
client_max_body_size 10m;
}
2. 慢連接防護(hù)
http {
# 防止慢速攻擊
client_body_timeout 10s; # 請(qǐng)求體超時(shí)
client_header_timeout 10s; # 請(qǐng)求頭超時(shí)
keepalive_timeout 15s; # 保持連接超時(shí)
send_timeout 10s; # 發(fā)送響應(yīng)超時(shí)
# 關(guān)閉不必要的HTTP方法
if ($request_method !~ ^(GET|HEAD|POST)$ ) {
return 444;
}
}
3. 高級(jí)防護(hù)模塊
# 啟用Nginx Plus或第三方模塊
load_module modules/ngx_http_modsecurity_module.so;
load_module modules/ngx_http_geoip2_module.so;
http {
# 使用GeoIP限制地區(qū)訪問(wèn)
geoip2 /usr/share/GeoIP/GeoLite2-Country.mmdb {
$geoip2_country_code country iso_code;
}
map $geoip2_country_code $allowed_country {
default 0;
CN 1; # 只允許中國(guó)IP
US 1;
JP 1;
}
server {
# 應(yīng)用地區(qū)限制
if ($allowed_country = 0) {
return 403;
}
# 啟用WAF
modsecurity on;
modsecurity_rules_file /etc/nginx/modsec/main.conf;
}
}
四、Nginx 調(diào)優(yōu)增強(qiáng)防御
1. 內(nèi)核級(jí)優(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 工作進(jìn)程優(yōu)化
# 調(diào)整工作進(jìn)程
worker_processes auto; # 自動(dòng)匹配CPU核心
worker_rlimit_nofile 65535; # 每個(gè)進(jìn)程最大文件描述符
# 使用多核處理連接
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;
}
}
五、云平臺(tái)集成防御
1. 阿里云/騰訊云集成
# 通過(guò)HTTP頭傳遞真實(shí)客戶端IP
real_ip_header X-Forwarded-For;
set_real_ip_from 100.64.0.0/10; # 云平臺(tái)內(nèi)網(wǎng)段
# 啟用云平臺(tái)WAF
location / {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
# 云平臺(tái)特定頭
proxy_set_header Ali-CDN-Real-IP $remote_addr;
proxy_set_header Qcloud-Real-IP $remote_addr;
}
2. Cloudflare 集成
# 只接受來(lái)自Cloudflare IP的請(qǐng)求
include /etc/nginx/cloudflare-ips.conf;
server {
listen 80;
server_name yourdomain.com;
# 僅允許Cloudflare IP訪問(wèn)
allow 103.21.244.0/22;
allow 103.22.200.0/22;
# ... 其他Cloudflare IP段
deny all;
location / {
proxy_pass http://backend;
}
}
六、監(jiān)控與自動(dòng)化響應(yīng)
1. 實(shí)時(shí)監(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. 自動(dòng)封禁腳本
#!/bin/bash
# 自動(dòng)封禁高頻IP
LOG_PATH="/var/log/nginx/access.log"
THRESHOLD=100 # 每分鐘請(qǐng)求閾值
BAN_TIME=3600 # 封禁時(shí)間(秒)
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
# 定時(shí)解封
(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)控指標(biāo):
- 請(qǐng)求率
- 活動(dòng)連接數(shù)
- 錯(cuò)誤率
- 帶寬使用
- 上游響應(yīng)時(shí)間
七、應(yīng)急響應(yīng)計(jì)劃
攻擊發(fā)生時(shí)的處理流程:
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)維護(hù)頁(yè)面:
location / {
root /var/www/emergency;
try_files $uri /maintenance.html;
}
3.啟用云平臺(tái)DDoS防護(hù):
- 阿里云:?jiǎn)⒂肈DoS高防IP
- 騰訊云:?jiǎn)⒂么笥鞡GP高防
- AWS:?jiǎn)⒂肧hield Advanced
4.分析攻擊模式:
# 分析訪問(wèn)日志
awk '{print $1}' access.log | sort | uniq -c | sort -nr | head -20
# 實(shí)時(shí)監(jiān)控
tail -f access.log | grep -E ' (429|444|503) '
最佳實(shí)踐總結(jié)
- 分層防御:網(wǎng)絡(luò)層 + Nginx層 + 應(yīng)用層
- 速率限制:基于IP和關(guān)鍵端點(diǎn)
- 連接管理:限制并發(fā)和超時(shí)
- 自動(dòng)封禁:實(shí)時(shí)監(jiān)控 + 自動(dòng)響應(yīng)
- 云平臺(tái)集成:利用云WAF和DDoS防護(hù)
- 定期演練:每季度進(jìn)行防御壓力測(cè)試
關(guān)鍵建議:對(duì)于大規(guī)模DDoS攻擊,建議結(jié)合云服務(wù)商的DDoS防護(hù)服務(wù)(如阿里云高防IP、Cloudflare Pro),這些服務(wù)提供TB級(jí)的清洗能力,遠(yuǎn)超單臺(tái)Nginx服務(wù)器的防御能力。
最終部署前,務(wù)必進(jìn)行壓力測(cè)試:
# 使用wrk進(jìn)行壓力測(cè)試 wrk -t12 -c400 -d30s --timeout 10s https://yourdomain.com
根據(jù)測(cè)試結(jié)果調(diào)整限流閾值和防護(hù)策略,確保在防護(hù)攻擊的同時(shí)不影響正常用戶訪問(wèn)。
到此這篇關(guān)于Nginx中CC攻擊與DDoS防御的高級(jí)策略指南的文章就介紹到這了,更多相關(guān)Nginx防御CC攻擊與DDoS內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
nginx設(shè)置超時(shí)時(shí)間的問(wèn)題及解決方案
程序在處理大量數(shù)據(jù),接口超過(guò)1分鐘(默認(rèn)的)未返回?cái)?shù)據(jù),導(dǎo)致等待超時(shí),出現(xiàn)這種情況,我們可以先優(yōu)化程序,縮短執(zhí)行時(shí)間,可以調(diào)大nginx超時(shí)限制的參數(shù),使程序可以正常執(zhí)行,本文介紹nginx設(shè)置超時(shí)時(shí)間及504 Gateway Time-out的問(wèn)題解決方案,一起看看吧2024-02-02
Nginx內(nèi)置變量應(yīng)用場(chǎng)景分析
Nginx內(nèi)置變量速查表,涵蓋請(qǐng)求URI、客戶端信息、服務(wù)器信息、文件路徑、響應(yīng)與性能等類別,這篇文章給大家介紹Nginx內(nèi)置變量應(yīng)用場(chǎng)景分析,感興趣的朋友跟隨小編一起看看吧2025-11-11
詳解Nginx中常見(jiàn)負(fù)載均衡策略配置與使用場(chǎng)景
負(fù)載均衡(Load Balancing)成為確保系統(tǒng)高可用和高性能的關(guān)鍵技術(shù),本文將詳細(xì)講解 Nginx 的常見(jiàn)負(fù)載均衡策略,并附帶示例配置與適用場(chǎng)景,需要的小伙伴可以了解下2025-09-09
Nginx配置Prometheus監(jiān)控的實(shí)現(xiàn)
本文主要介紹了Nginx配置Prometheus監(jiān)控的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2025-02-02

