介绍

虽然 Nginx 已经提供 access 日志来查阅,但是不得不说很难看清,常规的正则过滤又显得较为麻烦。于是就找到了这么一款实时网络日志分析器 GoAccessopen in new window

一、安装使用

1. 安装 GoAccess

本例使用源码安装,./configure 如果出错请看问题说明部分:

$ wget https://tar.goaccess.io/goaccess-1.3.tar.gz
$ tar -xzf goaccess-1.3.tar.gz
$ cd goaccess-1.3
$ ./configure --enable-utf8 --enable-geoip=legacy
$ make
# make install
  • 如果有镜像,可通过yum 源直接安装
yum install -y geoip-devel
yum install -y ncurses-devel

2. 修改 Nginx 配置

nginx.conf 文件中新增以下配置:

location /report.html {
        alias /usr/local/nginx/html/report.html;
}

并重新加载配置文件:

$ /usr/local/nginx/sbin/nginx -s reload

3. 启动 GoAccess

在 nginx 安装目录下,执行 goaccess 命令:

# 生成html
$ cd /usr/local/nginx
$ goaccess ./logs/access.log -o ./html/report.html --log-format=COMBINED
WebSocket server ready to accept new client connections

4. 访问服务

在浏览器中打开http://127.0.0.1/report.html

二、问题说明

本处整理了安装 GoAccess 执行 configure 命令报错和解决办法。

1. 如出现以下报错则需安装 GeoIP:

configure: error: 
    *** Missing development files for the GeoIP library

2. 如出现以下报错则需安装 NCurses:

checking for mvaddwstr in -lncursesw... no
configure: error: *** Missing development libraries for ncursesw

3. 解决办法

官方已经给出了各个平台对应依赖包的名字(传送至官网open in new window),只需使用对应平台的包管理工具安装即可 如以上两个报错在 centos 下只需安装 geoip-devel 和 ncurses-devel 即可:

$ yum install geoip-devel ncurses-devel

4. 相关资料

GoAccess 官网:https://goaccess.ioopen in new window

配置选项

GoAccess 拥有多个配置选项。获取完整的最新配置选项列表,请运行:./configure --help

--enable-debug
使用调试标志编译且关闭编译器优化。
--enable-utf8
宽字符支持。依赖 Ncursesw 模块。
--enable-geoip=<legacy|mmdb>
地理位置支持。依赖 MaxMind GeoIP 模块。legacy 将使用原始 GeoIP 数据库。mmdb 将使用增强版 GeoIP2 数据库。
--enable-tcb=<memhash|btree>
Tokyo Cabinet 存储支持。 memhash 将使用 Tokyo Cabinet 的内存哈希数据库。 btree 将使用 Tokyo Cabinet 的磁盘 B+Tree 数据库。
--disable-zlib
禁止在 B+Tree 数据库上使用 zlib 压缩。
--disable-bzip
禁止在 B+Tree 数据库上使用 bzip2 压缩。
--with-getline
使用动态扩展行缓冲区用来解析完整的行请求,否则将使用固定大小(4096)的缓冲区。
--with-openssl
使 GoAccess 与其 WebSocket 服务器之间的通信能够支持 OpenSSL。

使用

1. 最简单的静态使用

DANGER

goaccess -f /var/log/nginx/access.log -o report.html --log-format=COMBINED

  • -f:指定日志文件
  • -o:指定报表路径
  • --log-format:日志格式,不加则会报错

这样就会生成一份 html 文件,可以利用 scp(推荐)、sz、rsync 下载该文件到本地后查看。

2. 配合 Nginx 实时更新

  • 发文日:2022/06/17暂未实现实时更新
  • 和普通的生成就是差别在增加了命令,配置文件未变
  
location /report.html {
   alias /usr/local/nginx/html/report.html;
     
    # 代理,限制访问
    allow xxx.xxx.xxx.xxx;
    deny all;
}

# 接着前往域名提供商添加 DNS 解析

DANGER

goaccess -f /usr/local/nginx/logs/access.log -o /usr/local/nginx/html/report.html --log-format=COMBINED --real-time-html

启动服务后会提示开启了一个 websocket

接着就可以在浏览器访问对应的地址了。