- 工信部备案号 滇ICP备05000110号-1
- 滇公网安备53011102001527号
- 增值电信业务经营许可证 B1.B2-20181647、滇B1.B2-20190004
- 云南互联网协会理事单位
- 安全联盟认证网站身份V标记
- 域名注册服务机构许可:滇D3-20230001
- 代理域名注册服务机构:新网数码
- CN域名投诉举报处理平台:电话:010-58813000、邮箱:service@cnnic.cn
高效统计Nginx访问频繁IP:AWK实战指南
欢迎来到8455线路检测中心技术小课堂,每天分享一个技术小知识。
1. 背景与价值
在日常运维和安全监控中,快速识别高频访问IP是至关重要的。通过分析Nginx访问日志中的频繁IP,我们可以:
发现恶意爬虫或攻击行为
识别热门用户或异常流量
优化CDN和负载均衡策略
辅助排查服务器性能问题
2. 基础统计命令
2.1 简单IP频次统计
# 基础版本:统计所有IP的出现次数
awk '{print $1}' /var/log/nginx/access.log | sort | uniq -c | sort -nr
命令解析:
awk '{print $1}':提取每行第一个字段(默认IP地址位置)
sort:对IP进行排序,为uniq做准备
uniq -c:统计每个唯一IP出现的次数
sort -nr:按次数降序排列
2.2 指定日志格式的精准统计
如果你的Nginx使用自定义日志格式:
# 查看日志格式定义(通常在nginx.conf中)
grep 'log_format' /etc/nginx/nginx.conf
# 例如日志格式包含变量$remote_addr
awk '{print $1}' /path/to/access.log | sort | uniq -c | sort -nr | head -20
3. 进阶统计技巧
3.1 按时间段统计
# 统计最近1小时的IP访问频次
awk -v d1="$(date -d'1 hour ago' +'%d/%b/%Y:%H:%M:%S')" -v d2="$(date +'%d/%b/%Y:%H:%M:%S')" '$4 > "["d1 && $4 < "["d2 {print $1}' access.log | sort | uniq -c | sort -nr
3.2 结合时间窗口和访问量阈值
# 找出5分钟内访问超过100次的IP(CC攻击检测)
awk -v limit=100 '
BEGIN {
# 设置5分钟时间窗口(300秒)
window=300
}
{
# 解析时间戳(需要根据日志格式调整)
gsub(/\\[/, "", $4)
split($4, t, /[/:]/)
timestamp = mktime(t[3] " " t[2] " " t[1] " " t[4] " " t[5] " " t[6])
ip = $1
# 滑动窗口统计
count[ip]++
time[ip][count[ip]] = timestamp
# 清理超过时间窗口的记录
while (count[ip] > 0 && timestamp - time[ip][1] > window) {
for (i=1; i
time[ip][i] = time[ip][i+1]
}
count[ip]--
}
# 输出超过阈值的IP
if (count[ip] > limit) {
print timestamp, ip, count[ip]
}
}' /var/log/nginx/access.log | sort -u
3.3 多维度组合分析
# IP + 状态码组合统计
awk '{print $1, $9}' access.log | sort | uniq -c | sort -nr | head -30
# IP + 请求路径统计
awk '{print $1, $7}' access.log | sort | uniq -c | sort -nr | head -30
4. 生产环境实用脚本
4.1 完整监控脚本
#!/bin/bash# filename: nginx_ip_monitor.sh
LOG_PATH="/var/log/nginx/access.log"
OUTPUT_FILE="/tmp/ip_stat_$(date +%Y%m%d_%H%M%S).txt"
TOP_N=20
THRESHOLD=1000 # 访问次数阈值
echo "=== Nginx访问IP统计报告 ===" > $OUTPUT_FILEecho "生成时间: $(date)" >> $OUTPUT_FILEecho "分析日志: $LOG_PATH" >> $OUTPUT_FILEecho "=============================" >> $OUTPUT_FILE
# 1. 总访问次数TOP IPecho -e "\\n[TOP $TOP_N 访问IP]" >> $OUTPUT_FILE
awk '{ip_count[$1]++} END {for(ip in ip_count) print ip_count[ip], ip}' $LOG_PATH | sort -rn | head -$TOP_N >> $OUTPUT_FILE
# 2. 异常高频IP(超过THRESHOLD次)echo -e "\\n[异常高频IP(>$THRESHOLD 次)]" >> $OUTPUT_FILE
awk -v limit=$THRESHOLD '{count[$1]++} END {for(ip in count) if(count[ip] > limit) print count[ip], ip}' $LOG_PATH | sort -rn >> $OUTPUT_FILE
# 3. 最近10分钟高频IPecho -e "\\n[最近10分钟高频IP]" >> $OUTPUT_FILE
awk -v d1="$(date -d'10 minutes ago' +'%d/%b/%Y:%T')" -v d2="$(date +'%d/%b/%Y:%T')" '$4 > "["d1 && $4 < "["d2 {print $1}' $LOG_PATH | sort | uniq -c | sort -rn | head -10 >> $OUTPUT_FILE
# 输出结果cat $OUTPUT_FILE
4.2 实时监控脚本
#!/bin/bash# 实时监控高频访问IP
tail -f /var/log/nginx/access.log | \\
awk '
{
ip = $1
count[ip]++
time = strftime("%H:%M:%S")
# 每1000行输出一次统计结果
if (NR % 1000 == 0) {
print "\\n=== 实时统计 ==="
print "时间:", time
print "已处理行数:", NR
print "独立IP数:", length(count)
print "TOP 10 IP:"
for (ip in count) {
print count[ip], ip
} | "sort -rn | head -10"
}
}
’
5. 性能优化建议
5.1 处理大日志文件
# 使用LC_ALL=C提升排序性能export LC_ALL=C
# 分块处理大文件split -l 1000000 large_access.log chunk_for file in chunk_*; do
awk '{print $1}' $file | sort | uniq -c >> temp_countsdonesort -rn temp_counts | uniq
5.2 使用AWK内置数组
# 单次AWK处理,避免多次管道
awk '{count[$1]++} END {for(ip in count) print count[ip], ip | "sort -rn"}' access.log
6. 扩展应用
6.1 自动封禁脚本
# 自动封禁访问频率过高的IP
awk -v limit=100 '
{
count[$1]++
if (count[$1] > limit) {
system("iptables -A INPUT -s " $1 " -j DROP")
print "已封禁IP:", $1, "次数:", count[$1]
}
}' /var/log/nginx/access.log
6.2 生成可视化数据
# 生成CSV格式,用于Excel或图表
awk 'BEGIN {print "IP,Count,Time"}
{count[$1]++}
END {for(ip in count) print ip "," count[ip] "," strftime("%Y-%m-%d %H:%M:%S")}' access.log > ip_stats.csv
7. 注意事项
日志格式验证:先确认IP在日志中的位置
head -1 /var/log/nginx/access.log | awk '{print "字段数:", NF; for(i=1;i<=NF;i++) print i": "$i}'
时区处理:确保时间解析正确
内存考虑:超大日志文件可能消耗大量内存
日志轮转:处理压缩的日志文件
zcat access.log.*.gz | awk '{print $1}' | sort | uniq -c | sort -nr
总结
AWK是处理Nginx日志的强大工具,通过简单的命令组合就能实现复杂的IP频率分析。掌握这些技巧后,你可以快速识别异常访问模式,及时响应潜在的安全威胁。建议根据实际业务需求调整阈值和统计维度,构建适合自己业务的监控体系。
8455线路检测中心官网上拥有完善的技术支持库可供参考,大家可自行查阅,更多技术问题,可以直接咨询。同时,8455线路检测中心整理了运维必备的工具包免费分享给大家使用,需要的朋友可以直接咨询。
更多技术知识,8455线路检测中心期待与你一起探索。
售前咨询
售后咨询
备案咨询
二维码

TOP