原文地址:https://blog.csdn.net/yown/article/details/56027112
需求:及时得到线上用户访问日志分析统计结果,以便给开发、测试、运维、运营人员提供决策!
找了各种工具,最终还是觉得goaccess不仅图文并茂,而且速度快,每秒8W 的日志记录解析速度,websocket10秒刷新统计数据,站在巨人肩膀上你也会看得更远…先上图:
具体方案如下步骤:
一、linux上安装goaccess(版本 1.1.1,一般安装在nginx所在机器上的/opt目录)
a.先安装依赖包
yum install ncurses-devel
wget http://geolite.maxmind.com/download/geoip/api/c/GeoIP.tar.gz
tar -zxvf GeoIP.tar.gz
cd GeoIP-1.4.8/
./configure
make && make install
b.安装goaccess
wget http://tar.goaccess.io/goaccess-1.1.1.tar.gz
tar -xzvf goaccess-1.3.tar.gz
cd goaccess-1.3/
./configure
make
make install
二、校对nginx的配置的日志格式(nginx.conf中log_format 使用以下自定义格式)
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for" '
'$connection $upstream_addr '
'$upstream_response_time $request_time';
修改后重启:
nginx -s stop
nginx
三、生成配置文件goaccess_log_conf_nginx.conf
vi /opt/goaccess/goaccess_log_conf_nginx.conf
time-format %T
date-format %d/%b/%Y
log_format %h - %^ [%d:%t %^] "%r" %s %b "%R" "%u" "%^" %^ %^ %^ %T
四、生成统计页面
手工生成当日统计页面:
goaccess -f /opt/nginx/logs/access.log -p /opt/goaccess/goaccess_log_conf_nginx.conf -o /opt/www/day-report.html
生成实时统计页面:
nohup goaccess -f /opt/nginx/logs/access.log -p /opt/goaccess/goaccess_log_conf_nginx.conf -o /opt/www/real-time-yong-report.html --real-time-html --ws-url=report.xxx.com &
检查是否存在进程: ps -ef|grep goaccess
五、开通对外访问地址
a.安装新的tomcat(假设在/opt/report-tomcat目录) 端口:7891 修改conf/server.xml中的端口,并增加访问目录:
<Host name="localhost" appBase="webapps"
unpackWARs="true" autoDeploy="false">
<Context path="/" docBase="/opt/www" />
</Host>
然后增加权限与角色( conf/tomcat-users.xml) :
<role rolename="report"/>
<user username="report" password="reportxxx" roles="report"/>
最后在webapps/ROOT/WEB-INFO/web.xml的web-app中增加:
<security-constraint>
<web-resource-collection>
<web-resource-name>
Restricted Area
</web-resource-name>
<url-pattern>/*</url-pattern>
</web-resource-collection>
<auth-constraint>
<role-name>report</role-name>
</auth-constraint>
</security-constraint>
<login-config>
<auth-method>BASIC</auth-method>
<realm-name>Authenticate yourself</realm-name>
</login-config>
b.确认统计端口:7890、7891对外开放
c.访问页面是否ok
手工生成当日统计页面:http://report.xxx.com:7891/day-report.html
实时统计页面: http://report.xxx.com:7891/real-time-yong-report.html
附:
参考链接:
各种日志格式转换工具:https://github.com/stockrt/nginx2goaccess
goaccess官网:https://goaccess.io
goaccess文件的配置说明:
输入命令: whereis goaccess 找到goaccess.conf配置文件
- port,websocket 的端口号(找起来比较费时,在输入框中输入:The port to which the connection is being attempted to connect. 可以快速定位),端口添加后重启或者关闭防火墙
- ws-url ,websocket 监听地址,可以是域名也可以是 ip
- real-time-html true ,取消前面的注释,开启实时功能
- output-format /data/logs/html/index.html,统计文件输出位置,具体位置可以自行定义
启动 goaccess,控制台输入命令:
goaccess -f /usr/local/openresty/nginx/logs/access.log -p /opt/goaccess/goaccess_log_conf_nginx.conf -o /data/logs/html/index.html --real-time-html
如果要在后台运行,可以用tmux 命令,如果本机没有安装tmux,yum install tmux 安装
控制台输入:tmux,按回车
[root@localhost ~]# tmux
输入 goaccess 启动命令,按回车
goaccess -f /usr/local/openresty/nginx/logs/access.log -p /opt/goaccess/goaccess_log_conf_nginx.conf -o /data/logs/html/index.html --real-time-html
中文界面
LANG="zh_CN.UTF-8" bash -c " goaccess -f /usr/local/openresty/nginx/logs/access.log -p /opt/goaccess/goaccess_log_conf_nginx.conf -o /data/logs/html/index.html --real-time-html"
退出 tmux模式,按 Ctrl + B ,再按 d,此时goaccess在后台运行。
通过nginx访问统计日志,日志位置与上面的 output-format 对应:
server {
listen 80;
server_name 192.168.58.128;
charset utf-8;
location / {
alias /data/logs/html/; #统计日志的位置
index index.html index.htm;
}
}
浏览器访问:http://192.168.58.128/index.html