Nginx 日志格式转换:GoAccess 示例
Nginx 日志格式转换:GoAccess 示例
本文将介绍如何将 Nginx 日志格式转换为 GoAccess 可识别的格式,方便使用 GoAccess 进行网站访问统计分析。
原始 Nginx 日志格式:
{"@timestamp":"2023-04-23T16:52:06+08:00","host":"10.0.0.4","clientip":"58.32.238.44","remote_user":"-","request":"GET /wms/rf/pick/pref HTTP/1.1","http_user_agent":"okhttp/4.5.0","size":762,"responsetime":0.008,"uct":0.001,"uht":0.008 ,"upstreamtime":"0.008","upstreamhost":"10.0.0.4:6001","http_host":"rmwms.pepsichina.cn","url":"/wms/rf/pick/pref","domain":"rmwms.pepsichina.cn","xff":"-","referer":"-","status":"200","connection_upgrade":"close","http_upgrade","-"}
GoAccess 标准日志格式:
$host - - [$time_local] "$request" $status $body_bytes_sent "$http_referer" "$http_user_agent" "$http_x_forwarded_for" $upstream_response_time $request_time
示例转换后的 GoAccess log-format:
%h - - [%d/%b/%Y:%T %z] "%r" %s %b "%R" "%u" "%^" %T %D
字段对应关系:
| 原始 Nginx 字段 | GoAccess 字段 | 说明 |
|---|---|---|
| host | %h | 服务器主机名 |
| clientip | %h | 客户端 IP 地址 |
| @timestamp | %d/%b/%Y:%T %z | 请求时间 |
| request | %r | 请求 URL |
| status | %s | 状态码 |
| size | %b | 响应大小 (字节) |
| referer | %R | 引用地址 |
| http_user_agent | %u | 浏览器信息 |
| xff | %^ | X-Forwarded-For 头部信息 |
| upstreamtime | %T | 上游服务器响应时间 |
| responsetime | %D | 响应时间 |
注意:
- 由于 GoAccess 不支持
upstreamtime字段,可以使用%T表示响应时间,或根据实际需求调整字段。 - 可以根据实际需求调整 GoAccess 日志格式,例如添加其他字段信息。
- 可以使用工具或脚本将 Nginx 日志转换为 GoAccess 格式,例如
logstash、fluentd等。
通过将 Nginx 日志格式转换为 GoAccess 可识别的格式,可以方便地使用 GoAccess 进行网站访问统计分析,例如:
- 网站访问量、流量统计
- 用户访问路径分析
- 页面停留时间统计
- 错误页面统计
- 等等
希望本文能帮助你了解如何将 Nginx 日志格式转换为 GoAccess 可识别的格式。
原文地址: https://www.cveoy.top/t/topic/nF2l 著作权归作者所有。请勿转载和采集!