GoAccess分析Nginx日志access.log以html方式实时预览

如果您想要对nginx的日志进行一波简单又快速的分析,可以采用GoAccess;

前提

1、安装GoAccess

[root@Centos ~]# yum install -y libmaxminddb-devel libncursesw5-dev ncurses-libs ncurses-devel openssl openssl-devel [root@Centos ~]# yum install -y GeoIP GeoIP-devel GeoIP-data [root@Centos ~]# wget http://tar.goaccess.io/goaccess-1.5.2.tar.gz --no-check-certificate [root@Centos ~]# tar -xzvf goaccess-1.5.2.tar.gz [root@Centos ~]# cd goaccess-1.5.2 # --with-openssl项开启openssl;使 GoAccess 与其 WebSocket 服务器之间的通信能够支持 OpenSSL # --enable-geoip地理位置支持;依赖 MaxMind GeoIP 模块。 legacy 将使用原始 GeoIP 数据库 # --enable-utf8 宽字符支持;依赖 Ncursesw 模块; [root@Centos goaccess-1.5.2]# ./configure --enable-utf8 --enable-geoip=legacy --with-openssl [root@Centos goaccess-1.5.2]# make && make install [root@Centos goaccess-1.5.2]# goaccess --version GoAccess - 1.5.2. For more details visit: https://goaccess.io/ Copyright (C) 2009-2020 by Gerardo Orellana Build configure arguments: --enable-utf8 --enable-geoip=legacy --with-openssl

2、配置Nginx的日志格式

# 定位到Nginx的主配置文件 在http里面定义一个日志格式 [root@Centos goaccess-1.5.2]# vim /etc/nginx/nginx.conf log_format main_goaccess '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for" "$request_body"'; # 业务域名的Server配置里面使用对应的日志格式 假设我这里的 域名为 www.tacks.com tacks.conf部分配置如下 listen 443 ssl; server_name www.tacks.com; access_log /home/s/logs/nginx/www.tacks.com-access.log main_goaccess;

3、配置GoAccess的日志解析格式

  • 编辑配置文件 追加下面配置 日志/日期/时间 格式
    [root@Centos goaccess-1.5.2]# vim config/goaccess.conf time-format %H:%M:%S date-format %d/%b/%Y log-format %h %^[%d:%t %^] "%r" %s %b "%R" "%u"

4、Https网站需要ssl支持

  • 需要配置下面两个参数(如果不需要则跳过)
--ssl-cert=/etc/nginx/certs/_.www.tacks.com.crt --ssl-key=/etc/nginx/certs/_.www.tacks.com.key

5、配置ws获取实时数据

  • 让网站实时获取数据,需要设置--ws-url参数
  • wss协议实际是websocket+SSL,就是在websocket协议上加入SSL层,类似https(http+SSL)
  • 这里可以采用nginx反向代理到内部的goaccess的端口上
# 在业务域名中,配置反向代理到内部的端口7890上 location /goaccess { proxy_redirect off; proxy_pass https://127.0.0.1:7890; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection "upgrade"; proxy_set_header Host $http_host; }

6、开启中文支持,编写启动脚本

 #!/bin/bash LANG="zh_CN.UTF-8" goaccess /home/s/logs/nginx/www.tacks.com-access.log -o /home/s/apps/tacks/public/report.html --real-time-html --ssl-cert=/etc/nginx/certs/_.www.tacks.com.crt --ssl-key=/etc/nginx/certs/_.www.tacks.com.key --ws-url=wss://www.tacks.com/goaccess --port=7890 --daemonize 

7、执行脚本

# 重启nginx [root@Centos ~]# systemctl restart nginx # 启动脚本 [root@Centos ~]# sh goaccess.sh zh_CN.UTF-8 Daemonized GoAccess: 18860 # 查看端口服务 [root@Centos ~]# netstat -ntlp | grep go tcp 0 0 0.0.0.0:7890 0.0.0.0:* LISTEN 18860/goaccess 

8、HTML实时预览

9、命令行窗口执行

goaccess -a /home/s/logs/nginx/www.tacks.com-access.log
本作品采用《CC 协议》,转载必须注明作者和本文链接
明天我们吃什么 悲哀藏在现实中 Tacks
讨论数量: 0
(= ̄ω ̄=)··· 暂无内容!