Nginx 日志格式配置:转换为 GoAccess 可解析的 JSON 格式
Nginx 日志格式配置:转换为 GoAccess 可解析的 JSON 格式
本文将介绍如何将 Nginx 日志格式配置为 GoAccess 可解析的 JSON 格式,以及使用 GoAccess 命令解析这些日志。
Nginx 日志格式配置
在 Nginx 配置文件中,添加以下 log_format 配置,将日志格式设置为 JSON:
log_format json '{"@timestamp":"$time_iso8601",'
'"host":"$server_addr",'
'"clientip":"$remote_addr",'
'"remote_user":"$remote_user",'
'"request":"$request",'
'"http_user_agent":"$http_user_agent",'
'"size":$body_bytes_sent,'
'"responsetime":$request_time,'
'"uct":"$upstream_connect_time",'
'"uht":"$upstream_header_time",'
'"upstreamtime":"$upstream_response_time",'
'"upstreamhost":"$upstream_addr",'
'"http_host":"$host",'
'"url":"$uri",'
'"domain":"$host",'
'"xff":"$http_x_forwarded_for",'
'"referer":"$http_referer",'
'"status":"$status"}';
GoAccess log-format
GoAccess 需要使用特定的格式来解析 JSON 日志,如下所示:
%^json[%Y-%m-%dT%H:%M:%S.%^]: %^json[host]: %^json[clientip]: %^json[remote_user]: '%^json[request]' %^json[status] %^json[size] '%^json[referer]' '%^json[http_user_agent]'
GoAccess 命令
使用以下命令解析 Nginx 访问日志:
goaccess -f /path/to/nginx/access.log --log-format=json --date-format=%Y-%m-%dT%H:%M:%S --time-format=%H:%M:%S --agent-list --no-query-string --no-term-resolver --no-csv-summary --no-html --no-progress --no-pie-chart
解释:
-f /path/to/nginx/access.log: 指定 Nginx 访问日志文件路径。--log-format=json: 指定日志格式为 JSON。--date-format=%Y-%m-%dT%H:%M:%S: 指定日期格式。--time-format=%H:%M:%S: 指定时间格式。--agent-list: 显示用户代理列表。--no-query-string: 不解析查询字符串。--no-term-resolver: 不解析终端。--no-csv-summary: 不生成 CSV 摘要。--no-html: 不生成 HTML 报告。--no-progress: 不显示进度条。--no-pie-chart: 不生成饼状图。
通过上述配置和命令,您就可以使用 GoAccess 分析 Nginx 日志数据,获取网站访问情况的详细信息。
原文地址: https://www.cveoy.top/t/topic/nGil 著作权归作者所有。请勿转载和采集!