goaccess nginx 日志分析
用法介绍
GoAccess的基本语法如下:
goaccess [ -b ][ -s ][ -e IP_ADDRESS][ - a ] <-f log_file >
参数说明:
- -f – 日志文件名
- -b – 开启流量统计,如果希望加快分析速度不建议使用该参数
- -s – 开启HTTP响应代码统计
- -a – 开启用户代理统计
- -e – 开启指定IP地址统计,默认禁用
- -p -指定goacces用户配置文件路径
goaccess的配置文件介绍
打开/etc/goaccess.conf,里面的最主要的几个配置为:
(1) time-format %H:%M:%S
(2) date-format %d/%b/%Y
(3) log-format
4, 在使用goaccess分析nginx日志时碰到的坑
网络上大部分的文章和介绍都只适合没任何修改的nginx日志格式,对自定义的log format都不怎么涉及。如果你采用的自定义的nginx日志格式,那么此处就需要特别注意,一旦log-format配置不对,goaccess分析的结果会差很大。
以我nginx日志格式为例:
log_format main '$server_name $remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for" ' '$upstream_addr $request_time $upstream_response_time';
- 1
- 2
- 3
- 4
按照goaccess预设的log format,这样的日志是没法分析的,所以我们需要自定义log format。
我的log format为:
log-format %^ %h %^ %^ [%d:%t %^] “%r” %s %b “%R” “%u” “%^” %^ %T %^
为了设置正确的log format,踩了不少坑,先列出来避免大家重复碰到。
(1) log format默认是按照空格分隔日志信息的,所以,对于包含了特殊字符如空格等信息的字段,必须包含在“”里面。如字段request http_user_agent等
(2) nginx日志格式里面,采用空格分隔,但是此处一定注意,只能用一个空格。当时我有个地方用了两个空格,直接导致goaccess结果出错。
(3) nginx日志中的每一个字段都要和log format中的一一对应,如果log format中不需要nginx中的某一个信息,则用%^跳过该信息。
(4) 对于nginx日志中的每一个 -, log format都需要一个%^来跳过, 如果是“-”, 则用“%^”
(5) 如果nginx日志信息中有:, 则需要在log format中也显示出来。例如nginx日志中$time_local就包含了:,所以在log format的相应位置也是 [%d:%t %^]
希望这些能帮助使用goaccess的朋友。
time-format %H:%M:%S
date-format %d/%b/%Y
log-format %h %^ %^ %^ %^ [%d:%t %^] "%r" "%s %b" "%^" "%^" %^
goaccess时间和日志格式支持的参数:
date_format
The date_format variable followed by a space, specifies the log format date containing any combination of regular characters and special format specifiers. They all
begin with a percentage (%) sign. See http://linux.die.net/man/3/strftime
Note that there is no need to use time specifiers since they are not used by GoAccess. It’s recommended to use only date specifiers, i.e., %Y-%m-%d. IT网,http://www.it.net.cn
log_format
The log_format variable followed by a space or \t , specifies the log format string. Linux学习,http:// linux.it.net.cn
%d date field matching the date_format variable.
%h host (the client IP address, either IPv4 or IPv6) IT网,http://www.it.net.cn
%r The request line from the client. This requires specific delimiters around the request (as single quotes, double quotes, or anything else) to be parsable. If not, we
have to use a combination of special format specifiers as %m %U %H.
%m The request method.
%U The URL path requested (including any query string).
%H The request protocol.
%s The status code that the server sends back to the client. IT网,http://www.it.net.cn
%b The size of the object returned to the client.
%R The “Referrer” HTTP request header.
%u The user-agent HTTP request header. Linux学习,http:// linux.it.net.cn
%D The time taken to serve the request, in microseconds.
%T The time taken to serve the request, in seconds or milliseconds. Note: %D will take priority over %T if both are used.
%^ Ignore this field. Linux学习,http:// linux.it.net.cn
PS:
之前配置文件错误,分析nginx日志报错,经过求助软件作者解决,并对goaccess有更深入的了解。赞一下软件作者。
[root@Rootop ~]# goaccess -d -f /home/wwwlogs/www.rootop.org.log -a -p ~/.goaccesssrc > test.html
Parsing… [373,734] [373,734/s]
GoAccess – version 0.8.5 – Nov 20 2014 16:48:39
Fatal error has occurred
Error occured at: goaccess.c – main – 832
Nothing valid to process. IT网,http://www.it.net.cn
~/.goaccesssrc文件内容:
date_format %d/%b/%Y
log_format %^:%^ %h %^[%d:%^] “%r” %s %b
nginx日志:
58.251.136.61 – - [20/Nov/2014:17:29:21 +0800] “GET /wp-content/themes/g-white/js/all.js HTTP/1.1″200 1292 “http://www.rootop.org/pages/890.html”"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.63 Safari/537.36″ -
https://goaccess.io/man#custom-log
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
2016-12-21 log4j分离日志输出 自定义过滤 自定义日志文件
2015-12-21 如何破解安卓手机上的图形锁(九宫格锁)