前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Nginx日志实时分析利器(Ngxtop)

Nginx日志实时分析利器(Ngxtop)

作者头像
子润先生
修改2021-07-07 10:28:59
1.4K0
修改2021-07-07 10:28:59
举报
文章被收录于专栏:用户8644135的专栏

Ngxtop实时解析nginx访问日志,并且将处理结果输出到终端,功能类似于系统命令top,所以这个软件起名ngxtop。有了ngxtop,你可以实时了解到当前nginx的访问状况,再也不需要tail日志看屏幕刷新。

一、安装ngxtop

由于ngxtop是python编写,我们采用pip安装,可能有些用户没有安装pip,下面我们一块加上pip的安装步骤:

pip安装

Fedora:yum -y install python-pip

CentOS/RHEL需先安装EPEL:yum -y install epel-release && yum -y install python-pip

Nginx日志实时分析利器(Ngxtop)
Nginx日志实时分析利器(Ngxtop)

Debian/Ubuntu:apt-get install python-pip

安装完pip就可以安装ngxtop

Nginx日志实时分析利器(Ngxtop)
Nginx日志实时分析利器(Ngxtop)

ngxtop使用参数

ngxtop 参数 print|top|avg|sum

ngxtop info 显示日志格式信息

-l <file>或--access-log <file> 设置日志路径

-f <format>或--log-format <format> 设置日志格式,默认格式combined,另外一种较常用格式为common

--no-follow 处理以前的日志,实时日志不做处理

-t <seconds> 或 --interval <seconds> 刷新频率,默认2秒

-g <var>或 --group-by <var> 按变量分组,默认显示 request_path

-w <var>或 --having <expr> 筛选 [default: 1]

-o <var>或 --order-by <var> 输出的排序方式,默认: 访问数

-n <number>或 --limit <number> 显示top多条,默认前top 10条

-a <exp> ...或 --a <exp> ... 对输出字段做处理,可选 sum, avg, min, max

-v或 --verbose 详细输出

-d或 --debug debug模式,输出每行及记录

-h或 --help 显示帮助详细

--version 显示版本信息

高级参数

-c <file>或 --config <file> 指定nginx配置文件,自动分析日志格式

-i <filter-expression>或 --filter <filter-expression> 满足表达式的过滤将被处理

-p <filter-expression>或 --pre-filter <filter-expression> in-filter expression to check in pre-parsing phase.

另外一些变量可以在分析时用到,名字含义同日志格式里的设置:remote_addr、remote_user、time_local、request、request_path、status、body_bytes_sent、http_referer、http_user_agent。

二、Ngxtop使用实例

实时监控日志

[root@localhost ~]# ngxtop -l /usr/local/nginx/logs/access.log

Nginx日志实时分析利器(Ngxtop)
Nginx日志实时分析利器(Ngxtop)

日志分析

[root@localhost ~]# ngxtop -l /usr/local/nginx/logs/access.log --no-follow

Nginx日志实时分析利器(Ngxtop)
Nginx日志实时分析利器(Ngxtop)

按rquest_path且是404的前10请求:

[root@localhost ~]# ngxtop -l /usr/local/nginx/logs/access.log --no-follow top request_path --filter 'status == 404'

按总bytes sent最高的前10:

[root@localhost ~]# ngxtop -l /usr/local/nginx/logs/access.log --no-follow --order-by 'avg(bytes_sent) * count'

按remote address进行排序前10:

[root@localhost ~]# ngxtop -l /usr/local/nginx/logs/access.log --no-follow --group-by remote_addr

显示400或更高返回状态码的且只显示request、status、http_referer这三列信息:

[root@localhost ~]# ngxtop -l /usr/local/nginx/logs/access.log --no-follow -i 'status >= 400' print request status http_referer

显示bytes_sent平均值且状态码为200且request_path以www开始的前10:

[root@localhost ~]# ngxtop -l /usr/local/nginx/logs/access.log --no-follow avg bytes_sent --filter 'status == 200 and request_path.startswith("www")'

关于 ngxtop 的更多用法,可通过 ngxtop -h 查询。

本文系转载,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文系转载前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 一、安装ngxtop
  • 二、Ngxtop使用实例
相关产品与服务
Elasticsearch Service
腾讯云 Elasticsearch Service(ES)是云端全托管海量数据检索分析服务,拥有高性能自研内核,集成X-Pack。ES 支持通过自治索引、存算分离、集群巡检等特性轻松管理集群,也支持免运维、自动弹性、按需使用的 Serverless 模式。使用 ES 您可以高效构建信息检索、日志分析、运维监控等服务,它独特的向量检索还可助您构建基于语义、图像的AI深度应用。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档