通过Nginx日志排查DDoS攻击是一个系统性的过程,涉及多个步骤和工具。以下是一个详细的指南,帮助你有效地识别和应对DDoS攻击:
http {
limit_req_zone $binary_remote_addr zone=one:10m rate=1r/s;
server {
listen 80;
server_name example.com;
location / {
limit_req zone=one burst=5 nodelay;
# 其他配置...
}
}
}
server {
listen 80;
server_name example.com;
location / {
if ($remote_addr ~* (192\.168\.1\.|10\.0\.0\.) ) {
return 403;
}
# 其他配置...
}
}
http {
geoip_country GeoIP.dat;
server {
listen 80;
server_name example.com;
location / {
# GeoIP过滤指令...
}
}
}
以下是一个简单的Bash脚本示例,用于统计每个IP地址的请求次数并找出异常高的IP:
#!/bin/bash
LOG_FILE="/var/log/nginx/access.log"
THRESHOLD=1000
awk '{print $1}' $LOG_FILE | sort | uniq -c | sort -nr | while read count ip; do
if [ "$count" -gt "$THRESHOLD" ]; then
echo "Possible DDoS attack detected from IP: $ip with $count requests"
fi
done
通过以上步骤,你可以有效地从Nginx日志中发现潜在的DDoS攻击,并采取相应的防护措施,确保网络服务的稳定性和安全性。