网站首页 > 技术教程 正文
背景
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-02 Centos7 配置Python3.8+Django+nginx+uwsgi环境(一)
- 2024-10-02 美畅物联|CentOS7通过yum安装nginx
- 2024-10-02 CentOS7下使用nginx实现TCP和UDP代理
- 2024-10-02 CentOS7部署nginx报错(nginx停止命令)
- 2024-10-02 centos7 安装配置nginx环境(centos8安装源设置)
你 发表评论:
欢迎- 08-06linux 和 windows文件格式互相转换
- 08-06谷歌 ChromeOS 已支持 7z、iso、tar 文件格式
- 08-06Linux下比较文件内容的6种方法
- 08-06文件格式及功能汇总
- 08-0610个Linux文件内容查看命令的实用示例
- 08-06Linux-如何区分不同文件类型
- 08-06Zabbix技术分享——监控windows进程资源使用情况
- 08-06Linux系统卡顿?学会ps命令这三招,轻松定位问题进程
- 最近发表
- 标签列表
-
- 下划线是什么 (87)
- 精美网站 (58)
- qq登录界面 (90)
- nginx 命令 (82)
- nginx .http (73)
- nginx lua (70)
- nginx 重定向 (68)
- Nginx超时 (65)
- nginx 监控 (57)
- odbc (59)
- rar密码破解工具 (62)
- annotation (71)
- 红黑树 (57)
- 智力题 (62)
- php空间申请 (61)
- 按键精灵 注册码 (69)
- 软件测试报告 (59)
- ntcreatefile (64)
- 闪动文字 (56)
- guid (66)
- abap (63)
- mpeg 2 (65)
- column (63)
- dreamweaver教程 (57)
- excel行列转换 (56)
本文暂时没有评论,来添加一个吧(●'◡'●)