GoAccess – 实时 Apache 和 Nginx 日志分析工具

一、使用yum方式安装goaccess1.2

操作系统环境

[root@k8s-etcd-2 test]# uname -r 3.10.0-693.el7.x86_64

1.1、安装使用yum源

首先安装epel的yum源,否则系统中没有goaccess的包

[root@k8s-etcd-2 test]# wget http://dl.fedoraproject.org/pub/epel/7/x86_64/Packages/e/epel-release-7-12.noarch.rpm
[root@k8s-etcd-2 test]# rpm -ivh epel-release-7-12.noarch.rpm
[root@k8s-etcd-2 test]# yum makecache		#建立数据缓存

1.2、安装依赖包

[root@k8s-etcd-2 test]# yum -y install glib2 glib2-devel GeoIP-devel  ncurses-devel zlib zlib-devel gcc
[root@k8s-etcd-2 test]# yum -y install GeoIP-update	    #安装GeoIP-update地理位置数据库

1.3、安装goaccess

[root@k8s-etcd-2 test]# yum -y install goaccess
[root@k8s-etcd-2 test]# echo $?		     #查看安装命令是否执行成功,0为成功
0

1.4、调整goaccess配置

使用yum安装的goaccess,默认配置文件是在/etc/goaccess.conf

[root@k8s-etcd-2 test]# cat << EOF >> /etc/goaccess.conf
time-format %T
date-format %d/%b/%Y
log-format %h - %^ [%d:%t %^] requesthost:"%v"; "%r" requesttime:"%T"; %s %b "%R" - %^"%u"
EOF

1.5、调整nginx.conf配置

因为goaccess解析日志对log_format信息有要求,所以也需要调整nginx的配置

[root@k8s-etcd-2 test]# vim /opt/nginx/nginx.conf
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"';

(截取一张刚刚用yum安装的nginx配置,j见下图,以供参考!主要看log_format配置)

1.6、重启nginx

      这里需要注意 如果nginx之前已经在服务器上运行,并且之前已经有access访问日志,那么因为之前的日志文件是根据之前的log_format配置生成的,goaccess配置log_format不会识别,所以这次就需要先将以往的access.log删除或者重命名备份,然后等待重新生成的access.log文件。

[root@k8s-etcd-2 test]# cd /var/log/nginx/
[root@k8s-etcd-2 nginx]# mv access.log access.log_bak
[root@k8s-etcd-2 nginx]# systemctl restart nginx

3.2、访问测试nginx默认页面,生成access日志

1.7、使用goaccess进行日志解析

[root@k8s-etcd-2 nginx]# goaccess -a -c -d -f /var/log/nginx/access.log -p /etc/goaccess.conf > /home/test/test.html

goaccess使用参数详解

  • -a 开启 UserAgent 列表。开启后会降低解析速度
  • -c 在程序开始运行时显示 日志/日期 配置窗口
  • -d 输出 HTML 或者 JSON 报告时开启 IP 解析
  • -f 指定输入日志文件的路径
  • -p 指定使用自定义配置文件

1.8、配置访问方式

可以通过nginx的虚拟主机去访问,或者就干脆在Windows中用浏览器打开html文件就可以了

我这里简单配置一下nginx的虚拟主机去访问

[root@k8s-etcd-2 nginx]# vim /etc/nginx/nginx.conf
    server {
        listen     8080;
        server_name localhost;
            root    /home/test;
            index   test.html *.html;
        }
[root@k8s-etcd-2 nginx]# nginx -s reload

1.9、访问测试

访问我上边配置的地址 http://192.168.99.183:8080

如果可以访问到这个页面,恭喜简单使用成功!

版权声明:
作者:郭靖
链接:https://www.sxszhian.com/archives/9654
来源:上海永驰网络科技有限公司
文章版权归作者所有,未经允许请勿转载。

THE END
分享
二维码
打赏
< <上一篇
下一篇>>