GoAccess 是一个开源的实时 网络日志分析器和交互式查看器,可以在 *nix
系统的终端中或通过浏览器运行。
默认情况下,goacccess
分析 COMBINED
类型的日志,也是 nginx/apache 默认的形式。goaccess
是支持多站点分析的,根据官网说法,只要日志格式中带有 %v
就会开启,其实比较简单的做法是使用 VCOMBINED
类型的日志分析即可。想要分析 VCOMBINED
类型的日志,需要在 nginx
等日志中做一点点细微的调整。
nginx
access.log
日志格式增加 host
如果多个网站的日志交织在同一个 access.log
日志中,首先需要调整 nginx access.log
日志格式:
根据官网,默认的格式为:
$remote_addr - $remote_user [$time_local] "$request" $status $body_bytes_sent "$http_referer" "$http_user_agent" $request_length $request_time [$proxy_upstream_name] [$proxy_alternative_upstream_name] $upstream_addr $upstream_response_length $upstream_response_time $upstream_status $req_id
来自 ingress-nginx 官网
修改为符合 VCOMBINED
规范的日志,仅需做一点调整即可:
$host:$server_port $remote_addr - $remote_user [$time_local] \"$request\" $status $body_bytes_sent \"$http_r
eferer\" \"$http_user_agent\" $request_length $request_time [$proxy_upstream_name] [$proxy_alternative_upstream_name] $upstream_addr $
upstream_response_length $upstream_response_time $upstream_status $req_id
仔细看,其实就是在最前面增加了 $host:$server_port
,经过实践,如果仅增加 $host
是不符合 VCOMBINED
格式的,在 goaccess
解析时会报错。
goaccess
分析
分析时可以指定日志格式,默认提供了多种格式,默认会采用 COMBINED
,可以解析 nginx
的默认日志格式。
goaccess "$NGINX_LOG_FILE" -o "/path/to/report.html" --log-format=COMBINED
再按照上面方法配置后,在 nginx
日志中具有了 host
信息后,就可以分析带有主机信息的日志了:
goaccess "$NGINX_LOG_FILE" -o "/path/to/report.html" --log-format=VOMBINED
如果一切顺利,打开报告即可看到多主机分析报表。
References
- https://kubernetes.github.io/ingress-nginx/user-guide/nginx-configuration/configmap/#log-format-upstream
- Nginx Virtual Host Log Format.md
- https://github.com/allinurl/goaccess
- https://goaccess.io/man
- https://github.com/allinurl/goaccess/blob/037c6c23b70b79559fc90904026ae1932c4d7f7c/config/goaccess.conf#L79
- https://nginx.org/en/docs/http/ngx_http_core_module.html#variables
- [What is a standard command for printing a date in RFC-3339 format?](https://unix.stackexchange.com/questions/120484/what-is-a-standard-command-for-printing-a-date-in-rfc-3339-format
- Kubernetes logs dump for some time range