从Nginx日志中发现DDoS攻击,可以通过以下几个步骤进行:
grep
、awk
等工具来提取和分析这些字段。awk
或cut
命令提取IP地址,并统计每个IP的请求次数。awk '{print $1}' access.log | sort | uniq -c | sort -nr
grep
命令查找特定的请求模式。grep 'GET /path/to/resource' access.log
awk
或grep
命令统计不同状态码的数量。awk '{print $9}' access.log | cut -d ' ' -f 1 | sort | uniq -c | sort -nr
logrotate
工具来管理日志文件的轮转。以下是一个简单的Bash脚本示例,用于统计每个IP地址的请求次数并找出异常高的IP:
#!/bin/bash
LOG_FILE="/var/log/nginx/access.log"
THRESHOLD=1000
# 统计每个IP的请求次数
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攻击,并采取相应的防护措施。