什么是ngxtop

ngxtop是一个免费的开源、简单、灵活、完全可配置且易于使用的nginx服务器实时监控工具。它通过解析nginx访问日志(默认位置总是/var/log/nginx/access.log)来收集数据,并显示nginx服务器的有用指标,从而帮助您实时监视web服务器。它还允许您从远程服务器解析Apache日志。

如何安装Ngxtop

要安装ngxtop,首先需要在Linux中安装PIP,然后使用以下命令安装ngxtop。

$ sudo pip install ngxtop

解析apache日志

ngxtop 也可以解析apache的日志

$ ssh user@remote_server tail -f /var/log/apache2/access.log | ngxtop -f common

running for 20 seconds, 1068 records processed: 53.01 req/sec

Summary:
|   count |   avg_bytes_sent |   2xx |   3xx |   4xx |   5xx |
|---------+------------------+-------+-------+-------+-------|
|    1068 |        28026.763 |  1029 |    20 |    19 |     0 |

Detailed:
| request_path                             |   count |   avg_bytes_sent |   2xx |   3xx |   4xx |   5xx |
|------------------------------------------+---------+------------------+-------+-------+-------+-------|
| /xxxxxxxxxx                              |     199 |        55150.402 |   199 |     0 |     0 |     0 |
| /xxxxxxxx/xxxxx                          |     167 |        47591.826 |   167 |     0 |     0 |     0 |
| /xxxxxxxxxxxxx/xxxxxx                    |      25 |         7432.200 |    25 |     0 |     0 |     0 |
如何实时监控nginx的日志

可以使用ngxtop 在Linux中实时监控Nginx日志文件

监控nginx请求

ngxtop最简单的方法是不带参数运行。 它将解析/var/log/nginx/access.log,以跟随的模式运行。

$ sudo ngxtop

running for 411 seconds, 64332 records processed: 156.60 req/sec

Summary:
|   count |   avg_bytes_sent |   2xx |   3xx |   4xx |   5xx |
|---------+------------------+-------+-------+-------+-------|
|   64332 |         2775.251 | 61262 |  2994 |    71 |     5 |

Detailed:
| request_path                             |   count |   avg_bytes_sent |   2xx |   3xx |   4xx |   5xx |
|------------------------------------------+---------+------------------+-------+-------+-------+-------|
| /abc/xyz/xxxx                            |   20946 |          434.693 | 20935 |     0 |    11 |     0 |
| /xxxxx.json                              |    5633 |         1483.723 |  5633 |     0 |     0 |     0 |
| /xxxxx/xxx/xxxxxxxxxxxxx                 |    3629 |         6835.499 |  3626 |     0 |     3 |     0 |

ctrl+c退出。

指定格式

格式列表由log_format 指定

$ sudo ngxtop -f main -l /var/log/nginx/site1/access.log

nginx如何查看哪个ip访问最频繁

$ sudo ngxtop remote_addr -l  /var/log/nginx/site/access.log

running for 20 seconds, 3215 records processed: 159.62 req/sec

top remote_addr
| remote_addr     |   count |
|-----------------+---------|
| 138.163.137.161 |      20 |
| 44.78.45.3    |      16 |
| 17.17.53.7     |      16 |

指定要解析的文件

如果有多个日志,可以使用 -l指定

$ sudo ngxtop -l /var/log/nginx/site1/access.log
日期:2019-04-29 03:17:22 来源:oir作者:oir