1、为了提升 GoAccess 分析准确度,建议配置 nginx.conf
的 log_format 项以下:html
log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for" "$request_body"';
2、安装GoAccessnginx
$ wget http://tar.goaccess.io/goaccess-1.2.tar.gz $ tar -xzvf goaccess-1.2.tar.gz #安装依赖 $ yum install GeoIP-devel $ cd goaccess-1.2/ # --with-openssl项开启openssl,HTTPS时须要 $ ./configure --enable-utf8 --enable-geoip=legacy --with-openssl $ make $ make install
安装完成后,默认配置文件goaccess.conf
放置于/usr/local/etc下,为了方便管理,使用
jsonmv /usr/local/etc/goaccess.conf /etc/
命令将其移动到/etc
目录下
对配置文件作一些主要配置:服务器
time-format %H:%M:%S date-format %d/%b/%Y log-format %h %^[%d:%t %^] "%r" %s %b "%R" "%u"
其中,log-format 与 access.log 的 log_format 格式对应,每一个参数以空格或者制表符分割。参数说明以下:dom
%t 匹配time-format格式的时间字段 %d 匹配date-format格式的日期字段 %h host(客户端ip地址,包括ipv4和ipv6) %r 来自客户端的请求行 %m 请求的方法 %U URL路径 %H 请求协议 %s 服务器响应的状态码 %b 服务器返回的内容大小 %R HTTP请求头的referer字段 %u 用户代理的HTTP请求报头 %D 请求所花费的时间,单位微秒 %T 请求所花费的时间,单位秒 %^ 忽略这一字段
查看 GoAccess 命令参数,以下:socket
$ goaccess -h # 经常使用参数 -a --agent-list 启用由主机用户代理的列表。为了更快的解析,不启用该项 -d --with-output-resolver 在HTML/JSON输出中开启IP解析,会使用GeoIP来进行IP解析 -f --log-file 须要分析的日志文件路径 -p --config-file 配置文件路径 -o --output 输出格式,支持html、json、csv -m --with-mouse 控制面板支持鼠标点击 -q --no-query-string 忽略请求的参数部分 --real-time-html 实时生成HTML报告 --daemonize 守护进程模式,--real-time-html时使用
一、控制台模式tcp
goaccess -a /usr/local/nginx/logs/access.log -p /etc/goaccess.conf
控制台下的操做方法:url
F1 主帮助页面 F5 重绘主窗口 q 退出 1-15 跳转到对应编号的模块位置 o 打开当前模块的详细视图 j 当前模块向下滚动 k 当前模块向上滚动 s 对模块排序 / 在全部模块中搜索匹配 n 查找下一个出现的位置 g 移动到第一个模块顶部 G 移动到最后一个模块底部
若是不修改配置文件/etc/goaccess.conf,输入下面指令,而后选择日志文件类型也能够:spa
[root@host1 logs]# goaccess -f access.log
Nginx日志是属于Combined Log Format (XLF/ELF)类型的,因此咱们选择第三个,用上下光标移动,空格选中,回车肯定。代理
二、HTML模式
[root@host1 logs]# goaccess -a /usr/local/nginx/logs/access.log -p /etc/goaccess.conf -o /usr/local/nginx/html/ao-access.html
这个分析报表是经过手动执行命令生成,因此须要实现 GoAccess 自动地建立报表
三、daemonize
GoAccess 已经为咱们考虑到这点了,它能够以 daemonize 模式运行,并提供建立实时 HTML 的功能,只须要在启动命令后追加--real-time-html --daemonize
参数便可。
[root@host1 logs]# goaccess -a /usr/local/nginx/logs/access.log -p /etc/goaccess.conf -o /usr/local/nginx/html/ao-access.html --real-time-html --daemonize Daemonized GoAccess: 13647 [root@host1 logs]# netstat -anptu | grep goaccess tcp 0 0 0.0.0.0:7890 0.0.0.0:* LISTEN 13647/goaccess
以守护进程启动 GoAccess 后,使用 Websocket 创建长链接,它默认监听 7890 端口,能够经过--port
参数指定端口号。
若是站点启用了 HTTPS,GoAccess 须要使用 openssl,在配置文件goaccess.conf
中配置ssl-cert
和ssl-key
项,并确保在安装过程当中 configure 时已添加--with-openssl
项来支持 openssl 。当使用 HTTPS 后 Websocket 通讯时也应该使用 wss 协议,须要将ws-url
项配置为wss://www.domain.com。
四、生成json报告:
[root@host1 logs]# goaccess -f access.log -a -d -o json > report.json
五、生成CSV报表:
[root@host1 logs]# goaccess -f access.log -o csv > report.csv
因为nginx会自动压缩日志,如下命令能够直接分析压缩后的日志:
zcat access.log.*.gz | goaccess #或者 zcat -f access.log* | goaccess
支持管道:
[root@host1 logs]# sed -n '/30\/May\/2018/,$p' access.log | goaccess -a [root@host1 logs]# grep "May" access.log | goaccess -a