goaccess nginx 日志分析

时间:2022-05-12 17:05:05

用法介绍

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

goaccess nginx 日志分析的更多相关文章

  1. goaccess nginx日志分析工具简单使用

    goaccess 是一个比较方便的支持实时的日志分析工具,比较方便,同时安装&&配置简单 安装 centos yum yum install -y goaccess 运行 我的ngin ...

  2. nginx日志分析利器GoAccess

    面试的时候一定会被面到的问题是:给出web服务器的访问日志,请写一个脚本来统计访问前10的IP有哪些?访问前10的请求有哪些?当你领略过goaccess之后,你就明白,这些问题,除了考验你的脚本背诵记 ...

  3. nginx日志分析利器GoAccess&lpar;转&rpar;

    面试的时候一定会被面到的问题是:给出web服务器的访问日志,请写一个脚本来统计访问前10的IP有哪些?访问前10的请求有哪些?当你领略过goaccess之后,你就明白,这些问题,除了考验你的脚本背诵记 ...

  4. Nginx 日志分析及性能排查

    Nginx 日志分析及性能排查 2017-03-04 Linux爱好者 (点击上方公众号,可快速关注) 作者:-外星人- my.oschina.net/362228416/blog/844713 如有 ...

  5. ELK&plus;redis搭建nginx日志分析平台

    ELK+redis搭建nginx日志分析平台发表于 2015-08-19   |   分类于 Linux/Unix   |  ELK简介ELKStack即Elasticsearch + Logstas ...

  6. 使用elk&plus;redis搭建nginx日志分析平台

    elk+redis 搭建nginx日志分析平台 logstash,elasticsearch,kibana 怎么进行nginx的日志分析呢?首先,架构方面,nginx是有日志文件的,它的每个请求的状态 ...

  7. Nginx日志分析及脚本编写

    在我们日常的运维中,当Nginx服务器正常运行后,我们会经常密切关注Nginx访问日志的相关情况,发现有异常的日志信息需要进行及时处理. 那今天我将跟大家一起来研究和分析Nginx日志,nginx默认 ...

  8. nginx高性能WEB服务器系列之八--nginx日志分析与切割

    nginx系列友情链接:nginx高性能WEB服务器系列之一简介及安装https://www.cnblogs.com/maxtgood/p/9597596.htmlnginx高性能WEB服务器系列之二 ...

  9. &lpar;转&rpar;10 分钟内快速构建能够承载海量数据的 nginx 日志分析与报警平台

    10 分钟内快速构建能够承载海量数据的 nginx 日志分析与报警平台 原文:https://blog.qiniu.com/archives/8713

随机推荐

  1. 自建git node pm2 (不赘述,就说遇见的问题)

    //======================[git]部分 主题部分还是按照网上的办法进行安装. 安装的话  分为两个办法(一个是yum (contos办法)  或者sudo(ubuntu办法) ...

  2. &lbrack;Leetcode&rsqb;&lbrack;JAVA&rsqb; Insert Interval

    Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessa ...

  3. C&num; 高精度减法 支持小数(待优化)

    是现实思路 1,先小数点补位,8913758923475893274958738945793845-4893127498372459823745324532453245.284929384729837 ...

  4. Swagger 生成 ASP&period;NET Web API

    使用 Swagger 生成 ASP.NET Web API 在线帮助测试文档 原文:ASP.NET Web API Help Pages using Swagger作者:Shayne Boyer翻译: ...

  5. 【openstack N版】——云主机调整大小&bsol;冷迁移

    一.先决条件 云主机冷迁移,即:将一台云主机从一个计算节点上,迁移到另外一个计算节点上.因为环境原因,所以我们需要准备两个计算节点. 1.1准备环境 在控制节点上,安装一个计算节点 #在控制节点安装n ...

  6. OC——关于KVO

    我们知道在WPF.Silverlight中都有一种双向绑定机制,如果数据模型修改了之后会立即反映到UI视图上,类似的还有如今比较流行的基于MVVM设计模式的前端框架,例如Knockout.js.其实在 ...

  7. ubuntu 16&period;04 安装 Mask&lowbar;RCNN 遇到的问题集锦

    源码网页(Mask R-CNN for object detection and instance segmentation on Keras and TensorFlow): https://git ...

  8. Swift 编程语言【转载&plus;整理】

    原文地址 在过去的几年中,移动应用程序风靡全世界并且已经改变了我们使用互联网进行工作或者休闲的方式.为了创建移动应用程序,各种技术应运而生,同时开发过程也开始将其作为一等公民来对待.尽管移动似乎已经无 ...

  9. 自动化部署--shell脚本--2

    node1和node2都装apache   [root@linux-node1 ~]# yum install httpd -y Loaded plugins: fastestmirror Loadi ...

  10. C&num;编程(二十一)----------扩展方法

    C#中的扩展方法 有许多扩展类的方式.如果有类的源代码,继承就是给类添加功能的好方法.但是如果没有源代码,怎么办?吃屎可以使用扩展方法,它允许改变一个类,但不需要该类的源代码.扩展方法是静态方法,它是 ...