查看网站每天访问记录,对应ip、地址区域、访问网址等
我的网站示例获取到的结果展示:Server Statistics
Nginx日志分析完整手册(Ubuntu服务器专用版)
所有输出目录:/root/web/games/logs/
日志路径:/var/log/nginx/access.log
一、环境准备
1. 安装必备工具
# 安装jq(JSON解析)
sudo apt update && sudo apt install -y jq
# 安装GoAccess(日志分析),生成可视化界面
sudo apt install -y goaccess
# 安装实时监控工具
sudo apt install -y ngxtop lnav
2. 创建输出目录
mkdir -p /root/web/games/logs/
chmod 755 /root/web/games/logs/
二、基础日志分析
1. 高频访问统计
功能 |
命令 |
TOP 10 IP |
awk '{print $1}' /var/log/nginx/access.log | sort | uniq -c | sort -nr | head -10 > /root/web/games/logs/top_ips.txt |
TOP 20 URL |
awk '{print $7}' /var/log/nginx/access.log | sort | uniq -c | sort -nr | head -20 > /root/web/games/logs/top_urls.txt |
状态码统计 |
awk '{print $9}' /var/log/nginx/access.log | sort | uniq -c > /root/web/games/logs/status_codes.txt |
2. 时间范围分析
# 分析2023年10月1日全天日志
awk '$4 >= "[01/Oct/2023:00:00:00" && $4 <= "[01/Oct/2023:23:59:59"' /var/log/nginx/access.log > /root/web/games/logs/20231001.log
# 分析最近1小时(动态时间)
awk -v d1="$(date --date='-1 hour' +'%d/%b/%Y:%H:%M:%S')" -v d2="$(date +'%d/%b/%Y:%H:%M:%S')" '$4 >= "["d1 && $4 <= "["d2' /var/log/nginx/access.log > /root/web/games/logs/last_hour.log
三、高级分析工具
1. GoAccess可视化报告
sudo goaccess /var/log/nginx/access.log \
--log-format=COMBINED \
--time-format='%H:%M:%S' \
--date-format='%d/%b/%Y' \
--ignore-crawlers \
-o /root/web/games/logs/report.html
参数说明:
- --ignore-crawlers:过滤爬虫访问
- --real-time-html:如需实时更新添加此参数
核心参数
参数 |
作用 |
示例 |
-f |
指定日志文件 |
goaccess -f /var/log/nginx/access.log |
-o |
输出报告文件 |
-o /root/web/games/logs/report.html |
--log-format |
定义日志格式 |
--log-format=COMBINED |
--time-format |
时间格式 |
--time-format='%H:%M:%S' |
--date-format |
日期格式 |
--date-format='%d/%b/%Y' |
--real-time-html |
实时更新HTML报告 |
需配合 -o 输出HTML |
--daemonize |
后台运行 |
实时监控时使用 |
控制台实时展示,F5刷新
goaccess -f /var/log/nginx/access.log --log-format=COMBINED
其他命令
# 分析指定时间段的日志
goaccess -f <(awk '$4 >= "[01/Jul/2023:00:00:00" && $4 <= "[01/Jul/2023:23:59:59"' access.log) --log-format=COMBINED
# 忽略爬虫或静态文件
--exclude-ip=1.2.3.4 \
--ignore-panel=STATIC_FILES \
--exclude-referer="bot|spider"
# 实时html报告
goaccess -f /var/log/nginx/access.log \
--log-format=COMBINED \
--real-time-html \
--port=7890 \
-o /root/web/games/logs/realtime.html
# 生成带地址信息的报告
goaccess -f /var/log/nginx/access.log \
--log-format=COMBINED \
--time-format='%H:%M:%S' \
--date-format='%d/%b/%Y' \
--geoip-database=/usr/share/GeoIP/GeoLite2-City.mmdb \
--ignore-crawlers \
--exclude-ip=192.168.1.100 \
-o /root/web/games/logs/full_report.html
# 检测异常状态码(触发后发送邮件)
goaccess -f access.log --no-progress --404-not-found | grep "404" | while read line; do
echo "发现404异常: $line" | mail -s "Nginx异常告警" admin@example.com
done
2. IP归属地分析
awk '{print $1}' /var/log/nginx/access.log | sort | uniq | while read ip; do
echo -n "$ip: " >> /root/web/games/logs/ip_locations.txt
curl -s "https://ipinfo.io/$ip?fields=country,city,org" | jq -r '.country + ", " + .city + " (" + .org + ")"' >> /root/web/games/logs/ip_locations.txt
done
四、安全监控
1. 异常请求检测
检测类型 |
命令 |
恶意扫描 |
grep -E '(wp-admin|phpmyadmin|.env)' /var/log/nginx/access.log > /root/web/games/logs/scan_attempts.txt |
暴力破解 |
grep 'POST /login' /var/log/nginx/access.log | awk '{print $1}' | sort | uniq -c | sort -nr > /root/web/games/logs/brute_force.txt |
敏感路径访问 |
grep -E '(\.php|\.asp|\.sh)\ HTTP' /var/log/nginx/access.log > /root/web/games/logs/sensitive_access.txt |
2. Fail2Ban防护
# 安装配置
sudo apt install -y fail2ban
sudo cp /etc/fail2ban/jail.{conf,local}
# 添加Nginx防护规则
sudo tee /etc/fail2ban/filter.d/nginx-probing.conf <<EOF
[Definition]
failregex = ^<HOST>.*"(GET|POST).*(wp-admin|phpmyadmin|\.env).*HTTP.*"
ignoreregex =
EOF
# 重启服务
sudo systemctl restart fail2ban
五、日志管理
1. 日志切割与归档
# 手动切割日志
sudo mv /var/log/nginx/access.log /var/log/nginx/access_$(date +%Y%m%d).log
sudo systemctl reload nginx
# 自动清理30天前日志
find /var/log/nginx/ -name "*.log" -mtime +30 -exec gzip {} \;
2. 敏感信息过滤(Nginx配置)
http {
log_format filtered '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log filtered;
# 屏蔽密码字段记录
set $redacted_pass $arg_password;
if ($redacted_pass != "") {
set $redacted_pass "***";
}
}
六、自动化脚本
1. 每日统计报告脚本
#!/bin/bash
LOG="/var/log/nginx/access.log"
OUTDIR="/root/web/games/logs"
REPORT="$OUTDIR/daily_report_$(date +%Y%m%d).txt"
echo "===== Nginx访问日报 ($(date +%F)) =====" > $REPORT
echo "■ 总访问量: $(wc -l < $LOG)" >> $REPORT
echo "■ 独立IP数: $(awk '{print $1}' $LOG | sort | uniq | wc -l)" >> $REPORT
echo "■ TOP 10 IP:" >> $REPORT
awk '{print $1}' $LOG | sort | uniq -c | sort -nr | head -10 >> $REPORT
echo "■ 异常请求(4xx/5xx):" >> $REPORT
awk '$9 >= 400 {print $1,$7,$9}' $LOG | sort | uniq -c | sort -nr >> $REPORT
2. 定时任务设置
# 每天0点生成报告
(crontab -l 2>/dev/null; echo "0 0 * * * /bin/bash /root/web/games/logs/daily_report.sh") | crontab -
七、注意事项
- 权限问题
-
- 确保脚本可执行:chmod +x /root/web/games/logs/*.sh
- 若使用sudo运行,需检查输出目录权限
- 日志格式验证
# 检查日志实际格式 head -n 1 /var/log/nginx/access.log
-
- 如格式非标准,需调整awk和goaccess的解析参数
- API调用限制
-
- IP归属地查询若使用免费API(如ipinfo.io),注意每分钟请求限制(建议搭配本地IP库)
- 存储监控
# 监控日志目录大小 du -sh /var/log/nginx/ /root/web/games/logs/
附录:常用命令速查表
场景 |
命令 |
实时监控 |
tail -f /var/log/nginx/access.log |
生成可视化报告 |
goaccess /var/log/nginx/access.log -o /root/web/games/logs/report.html |
IP归属地查询 |
curl -s "8.8.8.8 IP Address Details - IPinfo.io" | jq |
安全分析 |
grep ' 404 ' /var/log/nginx/access.log | awk '{print $7}' | sort | uniq -c |
本指南已整合全部需求,包含:
✅ 软件安装(jq/goaccess)
✅ 路径修改(所有输出到/root/web/games/logs/)
✅ 完整分析命令(基础/高级/安全)
✅ 自动化脚本与定时任务
✅ 权限与格式注意事项