nginx监控与调优(三)
nginx监控通常有两种方法:一是status监控;二是ngxtop监控。
一、status监控
使用status监控的步骤:
1.确定nginx中status模块是否已安装
[root@localhost sbin]# nginx -V
nginx version: nginx/1.13.7
built by gcc 4.4.7 20120313 (Red Hat 4.4.7-18) (GCC)
configure arguments: --with-http_stub_status_module
如果没有安装status模块,那么使用以下步骤进行安装:
--->找到nginx安装包所存放的位置,nginx安装包的目录是下/home/software/nginx-1.13.7
--->执行以下命令
./configure --with-http_stub_status_module
make
--->在/home/software/nginx-1.13.7/objs目录下会多一个文件nginx
将这个nginx文件拷贝到已经安装好的nginx目录下,覆盖sbin目录下的nginx文件
2.在http上下文中的server上下文中声明一个location的上下文,代码如下:
location /nginx_status {
stub_status on;
access_log off;
allow all;
#deny all;
}
3.进入监控界面
3.进入监控界面
Active connections: 6
server accepts handled requests
36 36 63
Reading: 0 Writing: 1 Waiting: 5
Active connections:活跃的连接数,最大连接数受worker_processes和worker_connections两个
参数的影响。正常情况下最大的连接数可以worker_processes*worker_connections,但是如果做了
反向代理,那么最大的连接数可以到达的值为:worker_processes*worker_connections/2。
accepts:表示已接收的连接数
handled:表示已处理的连接数
requests:表示一共收到的HTTP请求
Reading:表示正在读取请求header的信息
Writing:表示正在响应的请求
Waiting:表示Writing之后状态,为等待的意思
二、ngxtop监控
ngxtop其实是一个日志分析工具
ngxtop语法格式:
Usage:
ngxtop [options]
ngxtop [options] (print|top|avg|sum) ...
ngxtop info
ngxtop [options] query ...
常见用法如下:
1.ngxtop
打开默认ngxtop日志文件
2.ngxtop -l /usr/local/nginx/logs/access_log
打开指定的日志文件
3ngxtop top request_path --filter 'status == 404'
将错误状态码为404的前10个请求打印出来
4.ngxtop --order-by 'avg(bytes_sent) * count'
将发送字节数前10位的排序并显示出来
5.ngxtop --group-by remote_addr
按访问服务器的远程地址进行分组
6.ngxtop -i 'status >= 400' print request status http_referer
将状态码大于400的打印出来,打印的包括请求信息、状态码、相关的URL信息
ngxtop监控到的信息如下:
running for 80 seconds, 24 records processed: 0.30 req/sec
运行时间、已读取的线程数、点击率
Summary:
| count | avg_bytes_sent | 2xx | 3xx | 4xx | 5xx |
|---------+------------------+-------+-------+-------+-------|
| 24 | 2657.167 | 23 | 0 | 1 | 0 |
count:表示总的点击次数
avg_bytes_sent:表示平均发送的字节数