本文共 1856 字,大约阅读时间需要 6 分钟。
ngxtop 是一个实时命令行工具,专为分析 nginx 访问日志设计,能够将 http 请求的主要指标以表格形式直观展示。它支持按字段过滤请求,能够实时动态显示 nginx 访问请求数据。
#安装pythonyum install epel-releaseyum install python-pip#安装ngxtoppip install ngxtop
# 更新系统apt upgrade -y# 安装pythonapt install python3# 安装 pipsudo apt install pip# 配置 pipxpipx ensure# 安装 ngxtoppathsource ~/.bashrcpipx install ngxtop
ngxtop 提供了丰富的命令行参数,能够根据需求灵活配置。以下是常用参数说明:
-c:指定 nginx 配置文件,解析 log_format 和 access_log 路径。例如:
ngxtop -c /usr/local/nginx/conf/nginx.conf
-i:添加过滤条件。例如:
ngxtop -c /usr/local/nginx/conf/nginx.conf -i 'status == 200'
-p:在读取日志前应用过滤条件,不匹配的行不会读取。与 -i 的区别在于 -p 会在读取日志前过滤。
-l:指定 access-log 文件路径。例如:
ngxtop -l /usr/local/nginx/logs/access_log.log
-f:指定 nginx 日志格式,与 log_format 指令定义的格式一致。
--no-follow:强制解析 access-log 的当前内容,默认情况下 ngxtop 只会解析最新的访问日志。
-t:实时监控模式下的数据刷新时间间隔,默认为 2 秒。例如:
ngxtop -c /usr/local/nginx/conf/nginx.conf -t 1
-g:按指定字段分组,默认按 request_path 分组。例如按客户端 IP 分组:
ngxtop -c /usr/local/nginx/conf/nginx.conf -t 1 -g remote_addr
-w:对分组结果进行筛选,类似 SQL 的 having 效果。例如:
ngxtop -c /usr/local/nginx/conf/nginx.conf -w 'avg_bytes_sent > 30' -t 1
-o:指定排序字段。例如:
ngxtop -c /usr/local/nginx/conf/nginx.conf -o count -t 1
-n:设置显示的记录条数,默认为 10 条。例如:
ngxtop -c /usr/local/nginx/conf/nginx.conf -o count -t 1 -n 5
-a:在分析结果中添加聚合字段,如 sum、avg、min、max 等。
-v:启用详细输出。
-d:打印每一行及解析记录。
-h:打印帮助信息。
--version:打印版本信息。
ngxtop -c /usr/local/nginx/conf/nginx.conf
ngxtop -c /usr/local/nginx/conf/nginx.conf -i 'status == 200'
ngxtop -c /usr/local/nginx/conf/nginx.conf --no-follow
ngxtop -c /usr/local/nginx/conf/nginx.conf -t 1 -g remote_addr
ngxtop -c /usr/local/nginx/conf/nginx.conf -t 1
ngxtop -c /usr/local/nginx/conf/nginx.conf -o count -t 1
ngxtop -c /usr/local/nginx/conf/nginx.conf -o count -t 1 -n 5
转载地址:http://sxqfk.baihongyu.com/