centos7.9 使用 GoAccess 分析 nginx 访问日志
1.安装
以下内容可能已过期,只用作参考,请根据最新 官方安装说明 进行安装
$ sudo yum install -y epel-release
$ sudo yum install -y goaccess
2. 修改 nginx
日志格式
使用 goaccess
分析 nginx
日志,需要特定格式,修改nginx.conf log_format
#vim /etc/nginx/nginx.conf
# log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
log_format main '$remote_addr - $remote_user [$time_local] requesthost:"$http_host"; "$request" requesttime:"$request_time"; '
'$status $body_bytes_sent "$http_referer" - $request_body'
'"$http_user_agent" "$http_x_forwarded_for"';
3. 修改 goaccess
读取日志的格式,对应上面的 nginx
日志格式
# vim /etc/goaccess/goaccess.conf
time-format %T
date-format %d/%b/%Y
log-format %h - %^ [%d:%t %^] requesthost:"%v"; "%r" requesttime:"%T"; %s %b "%R" - %^"%u"
3. 先删除旧的 nginx
日志,然后重启 nginx
systemctl stop nginx
rm -rf /var/log/nginx/access.log
systemctl restart nginx
4. 启动 goaccess
进行日志解析
实时生成
LANG="zh_CN.UTF-8"
bash -c "goaccess -a -d -f /var/log/nginx/access.log -p /etc/goaccess/goaccess.conf -o /usr/share/nginx/html/goaccess.html --real-time-html --daemonize"
定时生成
$ crontab -e
# 每 5 分钟执行
*/5 * * * * LANG="zh_CN.UTF-8" bash -c "goaccess -a -d -f /var/log/nginx/access.log -p /etc/goaccess/goaccess.conf -o /usr/share/nginx/html/goaccess.html"
备注:
- 加上
--hour-spec=min
可以让小时报表按分钟显示 LANG="zh_CN.UTF-8"
用于设置系统语言,goaccess
的语言读的是系统语言
5. 查看 goaccess
的监听端口 7890,判断是否启动成功
$ netstat -tunpl | grep "goaccess"
tcp 0 0 0.0.0.0:7890 0.0.0.0:* LISTEN 18013/goaccess
6. 访问
访问上面输出的 /usr/share/nginx/html/goaccess.html
文件即可