网站首页 > 博客文章 正文
背景
Web服务器如Nginx,Apache的access_log一般都包括了很多关键信息,可以用于性能分析或者运营分析,CentOS 7的EPEL源集成了一个非常好用access_log分析工具goaccess,本文主要记录了如何使用goaccess进行nginx的日志分析:
安装
配置
Nginx的log文件默认存放在/var/log/nginx/access_log, 基本的用法如下:
第一次使用goaccess的时候,会弹出下面的对话框,要求配置access_log的时间和日志格式, nginx默认的日志格式就是NCSA, 所以一般就选第一个就可以了,然后goaccess在分析完成后会在终端上显示分析结果。
Goaccess还可以生成html格式的报表,有点可惜的是,目前好像仅支持English,命令也非常简单:
最后生成的报表包括pv, hits, agents等各种统计:
高级用法
Goaccess也支持日志定制,比如现在绝大部分nginx可能都会包括vhost,即一个物理nginx服务器可以支持多个域名的虚拟主机,但是nginx默认的NCSA日志格式在/etc/nginx/nginx.conf中配置如下,不包括vhost字段, 所以我们就无法在报表中区分不同虚拟主机的请求:
为了支持vhost,我将nginx的log_format定制成下面这个样子, 主要是添加了$server_name字段用来显示vhost, 还有末尾的$xxx_time, 用来显示每个请求的耗时:
为了让goaccess能适配这个格式,需要将/etc/goaccess.conf中修改成如下格式:
其中,具体的指示符号可以参考goaccess的官方文档, 比如%^,代表忽略对应的字段:
SPECIFIERS
%x A date and time field matching the time-format and date-format variables. This is used when a timestamp is given instead of the date and time being in two separate variables.
%t time field matching the time-format variable.
%d date field matching the date-format variable.
%v The server name according to the canonical name setting (Server Blocks or Virtual Host).
%e This is the userid of the person requesting the document as determined by HTTP authentication.
%h host (the client IP address, either IPv4 or IPv6)
%r The request line from the client. This requires specific delimiters around the request (single quotes, double quotes, etc) to be parsable. Otherwise, use a combination of special format specifiers such as %m, %U, %q and %H to parse individual fields.
Note: Use either %r to get the full request OR %m, %U, %q and %H to form your request, do not use both.
%m The request method.
%U The URL path requested.
Note: If the query string is in %U, there is no need to use %q. However, if the URL path, does not include any query string, you may use %q and the query string will be appended to the request.
%q The query string.
%H The request protocol.
%s The status code that the server sends back to the client.
%b The size of the object returned to the client.
%R The "Referer" HTTP request header.
%u The user-agent HTTP request header.
%D The time taken to serve the request, in microseconds.
%T The time taken to serve the request, in seconds with milliseconds resolution.
%L The time taken to serve the request, in milliseconds as a decimal number.
%^ Ignore this field.
%~ Move forward through the log string until a non-space (!isspace) char is found.
~h The host (the client IP address, either IPv4 or IPv6) in a X-Forwarded-For (XFF) field.
Goaccess还可以从管道支持输入,这样可以做一些更灵活的处理,比如,access_log日志中的request字段并不包括server_name, 为了让request能够包含完整的url, 我们可以用下面的命令,因为awk的第4列包含了server_name, 这个命令将server_name放到了request的前面组成了完整的url,再用作goaccess的输入:
末尾有彩蛋奥........
我们的名字
上班的我,脸上写满了认真。
认真,让我们迈向成功
大多数人都有“职业病”,还不轻,比如:
别人在找不同,而我在在找bug......
看到不好的代码
有时候也佩服别人
世界上最可怕的事情,没有之一
在网页上输入程序员,出现的第一条就击倒了我!!!
然而,就算时光倒流,我还是想成为一个程序员。
云谷计算,原创出品。
猜你喜欢
- 2024-10-15 英国Access将与马来西亚数字经济发展局合作,加快在亚太地区扩张
- 2024-10-15 霍尼韦尔为香港麗翔公务航空提供GoDirect客舱互联服务|美通社
- 2024-10-15 详解Win10家庭版/专业版/企业版功能区别
- 2024-10-15 Go1.18 快讯:这个新特性太实用了(go1.17新特性)
- 2024-10-15 CentOS中Nginx日志自动分割及日志分析
- 2024-10-15 GO语言(三十):访问关系型数据库(上)
- 2024-10-15 从linux网站搭建到日志服务审计渗透溯源
- 2024-10-15 golang对接阿里云私有Bucket上传图片、授权访问图片
- 2024-10-15 开源的分析web日志工具-Python语言开发,具有灵活的日志格式配置
- 2024-10-15 nginx运维小纪(nginx应用与运维实战 电子版)
你 发表评论:
欢迎- 07-08Google Cloud Platform 加入支持 Docker 的容器引擎
- 07-08日本KDDI与Google Cloud 签署合作备忘录,共探AI未来
- 07-08美国Infoblox与Google Cloud合作推出云原生网络和安全解决方案
- 07-08GoogleCloud为Spanner数据库引入HDD层,将冷存储成本降低80%
- 07-08谷歌推出Cloud Dataproc,缩短集群启动时间
- 07-08Infovista与Google Cloud携手推进射频网络规划革新
- 07-08比利时Odoo与Google Cloud建立增强合作,扩大全球影响力
- 07-08BT 和 Google Cloud 通过 Global Fabric 加速 AI 网络
- 最近发表
-
- Google Cloud Platform 加入支持 Docker 的容器引擎
- 日本KDDI与Google Cloud 签署合作备忘录,共探AI未来
- 美国Infoblox与Google Cloud合作推出云原生网络和安全解决方案
- GoogleCloud为Spanner数据库引入HDD层,将冷存储成本降低80%
- 谷歌推出Cloud Dataproc,缩短集群启动时间
- Infovista与Google Cloud携手推进射频网络规划革新
- 比利时Odoo与Google Cloud建立增强合作,扩大全球影响力
- BT 和 Google Cloud 通过 Global Fabric 加速 AI 网络
- NCSA和Google Cloud合作开发AI驱动的网络防御系统,加强泰国网络空间的安全性
- SAP将在沙特阿拉伯 Google Cloud 上推出BTP服务
- 标签列表
-
- ifneq (61)
- 字符串长度在线 (61)
- googlecloud (64)
- messagesource (56)
- promise.race (63)
- 2019cad序列号和密钥激活码 (62)
- window.performance (66)
- qt删除文件夹 (72)
- mysqlcaching_sha2_password (64)
- ubuntu升级gcc (58)
- nacos启动失败 (64)
- ssh-add (70)
- jwt漏洞 (58)
- macos14下载 (58)
- yarnnode (62)
- abstractqueuedsynchronizer (64)
- source~/.bashrc没有那个文件或目录 (65)
- springboot整合activiti工作流 (70)
- jmeter插件下载 (61)
- 抓包分析 (60)
- idea创建mavenweb项目 (65)
- vue回到顶部 (57)
- qcombobox样式表 (68)
- tomcatundertow (58)
- pastemac (61)
本文暂时没有评论,来添加一个吧(●'◡'●)