nginx日志监控之goaccess

介绍

GoAccess 是一个开源的实时 Web 日志分析器和交互式 在 *nix 系统上的终端中或通过浏览器运行的查看器。它为系统提供快速而有价值的HTTP统计信息 需要动态可视化服务器的管理员 。

官网: https://goaccess.io

github:https://github.com/allinurl/goaccess

安装

源码安装

1
2
3
4
5
6
$ wget https://tar.goaccess.io/goaccess-1.7.2.tar.gz
$ tar -xzvf goaccess-1.7.2.tar.gz
$ cd goaccess-1.7.2/
$ ./configure --enable-utf8 --enable-geoip=mmdb
$ make
# make install

docker安装

1
tail -F access.log | docker run -p 7890:7890 --rm -i -e LANG=$LANG allinurl/goaccess -a -o html --log-format COMBINED --real-time-html - > report.html

yum安装

1
yum install goaccess

监控nginx

修改nginx日志配置

1
2
3
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"';

运行goaccess,访问:http://test.cn/report.html

1
goaccess /var/log/nginx/access.log -o /data/html/test.cn/report.html --real-time-html --time-format %T --date-format %d/%b/%Y --log-format '%h - %^ [%d:%t %^] requesthost:"%v"; "%r" requesttime:"%T"; %s %b "%R" - %^"%u"' --daemon

docker运行

1
nohup tail -f /var/log/nginx/access.log | docker run -m 500M -p 7890:7890 --rm -i -e LANG="zh_CN.UTF-8" allinurl/goaccess -a -o html --time-format '%T' --date-format '%d/%b/%Y' --log-format '%h - %^ [%d:%t %^] requesthost:"%v"; "%r" requesttime:"%T"; %s %b "%R" - %^"%u"' --real-time-html - > /usr/share/nginx/html/report.html &

参数说明

1
2
3
4
5
--real-time-html 	#实时监听,默认ws端口为9870
LANG="zh_CN.UTF-8" #中文字符集,web界面显示为中文
--time-format #时间格式
--date-format #日期格式
--log-format #日志格式

问题

  1. 生成的report.html需要通过web服务器访问
  2. report.html如果没有实时更新,检查9870端口是否开放

本博客所有文章除特别声明外,均采用 CC BY-SA 4.0 协议 ,转载请注明出处!