Hugo博客公告弹窗

nginx日志分析工具

转载:https://ros6.com/?action=show&id=264

预览

NA6qyNDJprerTLCXGAQhgZMxVOAp5nqC.avif

防火墙

确保876端口开放

# 如果使用ufw
ufw allow 876

# 如果使用iptables
iptables -A INPUT -p tcp --dport 876 -j ACCEPT

日志格式

首先查询 nginx.conf 所在目录

find /etc/nginx /www/server/nginx/conf /usr/local/nginx* /usr/local/etc/nginx -name nginx.conf 2>/dev/null

日志格式

	# 新增JSON日志格式(用于nginx_alog程序)
	log_format jsonlog escape=json '{'
		'"time_local":"$time_iso8601",'
		'"timestamp":"$msec",'
		'"remote_addr":"$remote_addr",'
		'"host":"$host:$server_port",'
		'"request":"$request",'
		'"status": "$status",'
		'"body_bytes_sent":"$body_bytes_sent",'
		'"http_referer":"$http_referer",'
		'"http_user_agent":"$http_user_agent",'
		'"request_time":"$request_time",'
		'"cache_status":"$upstream_cache_status",'
		'"gzip_ratio":"$gzip_ratio"'
	'}';

站点日志更改

access_log /home/wwwroot/xxx.access.log jsonlog buffer=32k flush=5s;

重载nginx

nginx -s reload

启动程序

创建工作目录并进入

mkdir -p /opt/nginx_alog
cd /opt/nginx_alog

下载主程序

wget https://github.com/jimugou/jimugou.github.io/releases/download/v1.0.0/nginx_alog
chmod +x nginx_alog

下载IP库

wget https://github.com/jimugou/jimugou.github.io/releases/download/v1.0.0/ipv4.bin
wget https://github.com/jimugou/jimugou.github.io/releases/download/v1.0.0/ipv6.bin

启动服务

./nginx_alog log=/var/log/nginx/access.log logkeep=375 token=123 conf=/etc/nginx/nginx.conf

参数说明:

  • log:站点日志路径

  • conf:nginx.conf 路径

  • token:访问认证密钥

  • logkeep:日志保存时长(小时)

后台运行方式

方法1:使用nohup

nohup ./nginx_alog log=/var/log/nginx/access.log logkeep=375 token=123 conf=/etc/nginx/nginx.conf > nginx_alog.log 2>&1 &

方法2:使用screen(推荐)

screen -S nginx_alog
./nginx_alog log=/var/log/nginx/access.log logkeep=375 token=123 conf=/etc/nginx/nginx.conf

按 Ctrl+A 然后按 D 退出screen会话

重新进入screen会话查看

screen -r nginx_alog

访问

打开浏览器,访问:http://服务器IP:876/?mytoken=123

tabiew

json日志数据可视化

项目:https://github.com/shshemi/tabiew/tree/main

wget https://github.com/shshemi/tabiew/releases/download/v0.11.1/tabiew-x86_64-unknown-linux-gnu.deb
sudo dpkg -i tabiew-x86_64-unknown-linux-gnu.deb

运行

tw -f jsonl /home/wwwroot/logs/xxxx.access.log

用法:/搜索、Enter展开、e适配终端大小、q退出、导航h j k l← ↓ ↑ →

rhit

项目:https://github.com/Canop/rhit

wget https://dystroy.org/rhit/download/x86_64-unknown-linux-gnu/rhit
mv rhit /usr/local/bin/
chmod +x /usr/local/bin/rhit

转换日志格式

jq -R 'fromjson? | select(.) |
  "\(.remote_addr) - \(.remote_user // "-") [\(.time_local)] \"\(.request)\" \(.status) \(.body_bytes_sent) \"\(.http_referer)\" \"\(.http_user_agent)\""' \
  /home/wwwroot/logs/xxx-access.log \
  > /home/wwwroot/logs/xxx-access-rhit.log

一键分析报告

curl -sS -O https://raw.githubusercontent.com/woniu336/open_shell/main/rhit_report.sh && chmod +x rhit_report.sh

执行

./rhit_report.sh /path/to/access-rhit.log

1️⃣ 基础总览

rhit /home/wwwroot/lnmp01/logs/xxx-access-rhit.log

👉 显示 日期、状态、referer、路径 的总体情况。

2️⃣ 查看访问量最高的 IP

rhit -f i /home/wwwroot/lnmp01/logs/xxx-access-rhit.log

👉 输出访问次数最多的客户端 IP。

3️⃣ 查看最消耗带宽的 IP

rhit -k bytes -f i /home/wwwroot/lnmp01/logs/xxx-access-rhit.log

👉 按 传输字节数 排序,找出大流量 IP。

4️⃣ 查看访问最多的页面路径

rhit -f p /home/wwwroot/lnmp01/logs/xxx-access-rhit.log

👉 显示最热门的 URL 路径。

5️⃣ 查看消耗流量最大的页面

rhit -k bytes -f p /home/wwwroot/lnmp01/logs/xxx-access-rhit.log

👉 找出占带宽最多的 URL。

6️⃣ 查看状态码分布

rhit -f s /home/wwwroot/lnmp01/logs/xxx-access-rhit.log

👉 检查 200404500 等比例。

7️⃣ 查看 referer 来源

rhit -f r /home/wwwroot/lnmp01/logs/xxx-access-rhit.log

👉 知道访问从哪些网站引流过来。

8️⃣ 查看 HTTP 方法

rhit -f m /home/wwwroot/lnmp01/logs/xxx-access-rhit.log

👉 查看 GETPOSTHEAD 的分布。

9️⃣ 时间维度分析

  • 按日期:

    rhit -f d /home/wwwroot/lnmp01/logs/xxx-access-rhit.log
    
  • 按小时:

    rhit -f t /home/wwwroot/lnmp01/logs/xxx-access-rhit.log
    
CC BY-NC-SA 4.0 转载请注明
最后更新于 2025-10-04 15:57
clarity统计