在本页
- 前置条件
- 安装 ngxtop
- 如何使用 ngxtop
- 总结
ngxtop 是一款免费、开源、灵活且实时的 Nginx Web 服务器监控工具。它可以解析 Nginx 访问日志,并打印有关请求数量、请求的 URI、按状态码分类的请求数等信息。它是一款简单易用、用于监控访问 Nginx Web 服务器请求的工具。
在本文中,我将向您展示如何在 Ubuntu 20.04 上安装和使用 ngxtop 监控工具。
前置条件
- 一台运行 Ubuntu 20.04 的服务器。
- 您的服务器上已配置 root 密码。
安装 ngxtop
ngxtop 是一个基于 Python 的工具,因此您需要在系统中安装 Python 和 PIP 软件包。您可以使用以下命令将它们与 Nginx 一起安装:
apt-get install nginx python3 python3-pip -y
安装完成后,您可以如下所示使用 PIP 安装 ngxtop 软件包:
pip3 install ngxtop
安装 ngxtop 后,您可以使用以下命令验证 ngxtop 的版本:
ngxtop --version
您应该会得到如下输出:
xstat 0.1
如何使用 ngxtop
在本节中,我们将向您展示如何使用 ngxtop 来监控 Nginx Web 服务器。
运行不带任何参数的 ngxtop 命令将显示请求数量、请求的 URI、按状态码分类的请求数的摘要。
ngxtop
您应该会看到如下画面:

您可以使用 -l 选项来指定要分析的访问日志。
ngxtop -l /var/log/nginx/access.log
您应该会看到如下画面:

要列出访问您 Nginx 服务器最多的 IP,请运行以下命令:
ngxtop --group-by remote_addr -l /var/log/nginx/access.log
您应该会看到如下画面:

您可以使用以下命令打印发送总字节数最高的 10 个请求:
ngxtop --order-by 'avg(bytes_sent) * count' -l /var/log/nginx/access.log
您应该会看到如下画面:

ngxtop 还允许您解析来自远程服务器的 Apache 日志文件。您可以使用以下命令来实现:
ssh root@remote-server-ip tail -f /var/log/apache2/access.log | ngxtop -f common
要获取 ngxtop 所有可用选项的列表,请运行以下命令:
ngxtop --help
您应该会看到如下输出:
ngxtop - ad-hoc query for nginx access log.
Usage:
ngxtop [options]
ngxtop [options] (print|top|avg|sum)
...
ngxtop info
ngxtop [options] query
...
Options:
-l
, --access-log
access log file to parse.
-f
, --log-format
log format as specify in log_format directive. [default: combined]
--no-follow ngxtop default behavior is to ignore current lines in log
and only watch for new lines as they are written to the access log.
Use this flag to tell ngxtop to process the current content of the access log instead.
-t
, --interval
report interval when running in follow mode [default: 2.0]
-g
, --group-by
group by variable [default: request_path]
-w
, --having
having clause [default: 1]
-o
, --order-by
order of output for default query [default: count]
-n
, --limit
limit the number of records included in report for top command [default: 10]
-a
..., --a
... add exp (must be aggregation exp: sum, avg, min, max, etc.) into output
-v, --verbose more verbose output
-d, --debug print every line and parsed record
-h, --help print this help message.
--version print version information.
Advanced / experimental options:
-c
, --config
allow ngxtop to parse nginx config file for log format and location.
-i
, --filter
filter in, records satisfied given expression are processed.
-p
, --pre-filter
in-filter expression to check in pre-parsing phase.
Examples:
All examples read nginx config file for access log location and format.
If you want to specify the access log file and / or log format, use the -f and -a options.
"top" like view of nginx requests
$ ngxtop
Top 10 requested path with status 404:
$ ngxtop top request_path --filter 'status == 404'
Top 10 requests with highest total bytes sent
$ ngxtop --order-by 'avg(bytes_sent) * count'
Top 10 remote address, e.g., who's hitting you the most
$ ngxtop --group-by remote_addr
Print requests with 4xx or 5xx status, together with status and http referer
$ ngxtop -i 'status >= 400' print request status http_referer
Average body bytes sent of 200 responses of requested path begin with 'foo':
$ ngxtop avg bytes_sent --filter 'status == 200 and request_path.startswith("foo")'
Analyze apache access log from remote machine using 'common' log format
$ ssh remote tail -f /var/log/apache2/access.log | ngxtop -f common
总结
通过上面的教程,您已经学习了如何在 Ubuntu 20.04 上安装和使用 ngxtop。希望您现在可以从命令行界面轻松监控您的 Nginx 日志。