ngx_http_stub_status监控连接信息
nginx现已成为目前使用最广泛的web服务器和反向代理服务器,我们线上的Tomcat服务器一般都会由nginx进行代理,以此实现负载均衡的效果。既然nginx被应用得那么广泛,我们自然也得学习如何去对nginx做性能监控。本小节将介绍如何使用nginx的ngx_http_stub_status模块来对连接信息进行监控。本文默认读者有nginx的基础,所以一些基础性的东西不会过多介绍。
ASP站长网关于该模块的官方文档地址如下:
http://nginx.org/en/docs/http/ngx_http_stub_status_module.html
如果你的nginx是使用yum进行安装的,那么一般会自带这个模块,可以忽略以下这段为了安装ngx_http_stub_status模块而重新编译安装nginx的部分。因为我的nginx是编译安装的,当时并没有加上这个模块进行编译,所以现在还需要重新去编译安装一次。过程如下:
[root@01server ~]# /usr/local/nginx/sbin/nginx -V # 列出安装了哪些模块,可以看到我这里没有安装任何模块
nginx version: nginx/1.12.1
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-11) (GCC)
configure arguments: --prefix=/usr/local/nginx
[root@01server ~]# rm -rf /usr/local/nginx/ # 删除原本的nginx
[root@01server ~]# cd /usr/local/src/ # 进入存放安装包的路径
[root@01server /usr/local/src]# ls
nginx-1.12.1 nginx-1.12.1.tar.gz
[root@01server /usr/local/src]# cd nginx-1.12.1 # 进入之前已经解压好的目录
[root@01server /usr/local/src/nginx-1.12.1]# ./configure --prefix=/usr/local/nginx --with-http_stub_status_module # 加入编译参数,指定需要安装的模块
[root@01server /usr/local/src/nginx-1.12.1]# make && make install # 编译安装
[root@01server /usr/local/src/nginx-1.12.1]# cd /usr/local/nginx/sbin/
[root@01server /usr/local/nginx/sbin]# ./nginx -V # 可以已经把模块安装好了
nginx version: nginx/1.12.1
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-11) (GCC)
configure arguments: --prefix=/usr/local/nginx --with-http_stub_status_module
[root@01server /usr/local/nginx/sbin]#
安装好之后,还需要编辑一下配置文件,不使用nginx默认的配置文件:
[root@01server /usr/local/nginx/sbin]# cd ../conf/
[root@01server /usr/local/nginx/conf]# mv nginx.conf nginx.conf.bak # 不使用nginx自带的配置文件
[root@01server /usr/local/nginx/conf]# vim nginx.conf # 将以下内容粘贴进去
user nobody nobody;
worker_processes 2;
error_log /usr/local/nginx/logs/nginx_error.log crit;
pid /usr/local/nginx/logs/nginx.pid;
worker_rlimit_nofile 51200;
events
{
use epoll;
worker_connections 1024;
}
http
{
include mime.types;
default_type application/octet-stream;
server_names_hash_bucket_size 3526;
server_names_hash_max_size 4096;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /usr/local/nginx/logs/access.log main;
sendfile on;
tcp_nopush on;
keepalive_timeout 30;
client_header_timeout 3m;
client_body_timeout 3m;
send_timeout 3m;
connection_pool_size 256;
client_header_buffer_size 1k;
large_client_header_buffers 8 4k;
request_pool_size 4k;
output_buffers 4 32k;
postpone_output 1460;
client_max_body_size 10m;
client_body_buffer_size 256k;
client_body_temp_path /usr/local/nginx/client_body_temp;
proxy_temp_path /usr/local/nginx/proxy_temp;
fastcgi_temp_path /usr/local/nginx/fastcgi_temp;
fastcgi_intercept_errors on;
tcp_nodelay on;
gzip on;
gzip_min_length 1k;
gzip_buffers 4 8k;
gzip_comp_level 5;
gzip_http_version 1.1;
gzip_types text/plain application/x-Javascript text/css text/htm
application/xml;
include vhost/*.conf;
}
[root@01server /usr/local/nginx/conf]# mkdir vhost
[root@01server /usr/local/nginx/conf]# vim vhost/default.conf # 虚拟主机配置文件,将以下内容粘贴进去
server{
listen 80;
server_name localhost;
index index.html index.htm index.php;
root /usr/local/nginx/html;
location = /nginx_status{ # 配置访问路径,即uri
stub_status on; # 开启该模块
access_log off; # 关闭日志
allow 101.106.102.129; # 允许访问的ip,即白名单ip
allow 127.0.0.1;
deny all; # 拒绝白名单ip以外的ip访问
}
}
[root@01server ~]#
启动nginx:
[root@01server /usr/local/nginx/conf]# cd ../sbin/
[root@01server /usr/local/nginx/sbin]# ./nginx -t # 检查nginx的配置文件是否正常
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@01server /usr/local/nginx/sbin]# ./nginx -c /usr/local/nginx/conf/nginx.conf # 启动nginx
[root@01server /usr/local/nginx/sbin]# netstat -lntp |grep nginx
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 22713/nginx: master
[root@01server /usr/local/nginx/sbin]#
启动成功后,使用浏览器访问/nginx_status,访问成功响应的信息如下:
Nginx性能监控与调优
说明:
Active connections 当前活动的连接数量(包括等待的)
accepts 已接收的连接总数
handled 已处理的连接总数
requests 当前的请求总数
Reading nginx正在读取的连接数量
Writing nginx正在响应的连接数量
Waiting 当前空闲的连接数量
如上,通过这些简单的参数可以看到nginx当前的连接信息。在高并发场景下,可以根据Active connections参数判断当前的一个并发数量,Reading 参数则可以告诉我们当前nginx是否繁忙。当然,这只是最简单的一个nginx的监控方式,参数也就只有那么几个。但这些都是其他更高级的监控工具的基础,所以了解这些基础监控也是有必要的。
ngxtop监控请求信息
在上一小节中,我们介绍了如何利用 ngx_http_stub_status 模块去监控nginx的连接信息。本小节将介绍如何使用ngxtop工具来监控nginx的请求信息。
ngxtop可以实时解析nginx访问日志,并且将处理结果输出到终端,功能类似于系统命令top,所以这个软件起名ngxtop。有了ngxtop,你可以实时了解到当前nginx的访问状况,再也不需要tail日志看屏幕刷新。
ngxtop项目地址:
https://github.com/lebinh/ngxtop
安装ngxtop:
由于ngxtop是Python编写的,我们可以使用pip进行安装。如果你的机器上没有安装pip,需要先把pip装上,安装命令如下:
[root@01server ~]# yum install -y epel-release
[root@01server ~]# yum install -y python-pip
然后通过pip安装ngxtop,命令如下:
[root@01server ~]# pip install ngxtop
ngxtop使用说明:
[root@01server ~]# ngxtop --help
ngxtop - ad-hoc query for nginx access log.
Usage:
ngxtop [options]
ngxtop [options] (print|top|avg|sum) <var> ...
ngxtop info
ngxtop [options] query <query> ...
Options:
-l <file>, --access-log <file> # 需要分析的访问日志
-f <format>, --log-format <format> # log_format指令指定的日志格式 [默认: 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 <seconds>, --interval <seconds> report interval when running in follow mode [default: 2.0] # 指定监控信息刷新的间隔,单位为秒 [默认: 2.0]
-g <var>, --group-by <var> # 根据变量分组 [默认: request_path]
-w <var>, --having <expr> # 具备子句 [默认: 1] having clause [default: 1]
-o <var>, --order-by <var> # 排序 [默认: count]
-n <number>, --limit <number> # 显示的条数 [默认: 10]
-a <exp> ..., --a <exp> ... add exp (must be aggregation exp: sum, avg, min, max, etc.) into output # 添加聚合表达式到输出信息中
-v, --verbose # 更多的输出
-d, --debug # 打印所有行和解析记录,debug
-h, --help # 当前帮助信息.
--version # 输出版本信息.
高级选项:
-c <file>, --config <file> # 运行ngxtop解析nginx配置文件
-i <filter-expression>, --filter <filter-expression> filter in, records satisfied given expression are processed. # 根据指定的表达式进行过滤,仅显示过滤后的信息
-p <filter-expression>, --pre-filter <filter-expression> in-filter expression to check in pre-parsing phase. # 在筛选器表达式中检查预解析阶段
范例:
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:
$ ngxtop -c /usr/local/nginx/conf/nginx.conf
404前十的请求:
$ ngxtop top request_path --filter 'status == 404'
总流量前十的请求:
$ ngxtop --order-by 'avg(bytes_sent) * count'
访问量前十的ip地址:
$ ngxtop --group-by remote_addr
输出400以上状态码的请求以及请求来源:
$ 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")'
指定nginx的配置文件进行启动:
[root@01server ~]# ngxtop -c /usr/local/nginx/conf/nginx.conf
启动后如下:
Nginx性能监控与调优
注:Summary相当于请求的概览信息,Detailed就自然是请求的详细信息了。2xx、3xx、4xx以及5xx,都是表示的http状态。avg_bytes_sent表示请求所发送的字节数平均值,request_path则是请求路径,count表示请求的总次数。
默认情况下,ngxtop不会显示启动ngxtop之前的请求信息,只会显示ngxtop启动之后新的请求信息。所以我们可以到浏览器上刷新一下,随便访问一些页面,人为制造一些请求。如下图,这时就可以看到ngxtop成功监控到了请求信息:
Nginx性能监控与调优
我们可以通过选项来指定一些条件,例如我希望只显示http状态是200的,就可以使用-i进行指定:
[root@01server ~]# ngxtop -c /usr/local/nginx/conf/nginx.conf -i 'status == 200'
如下:
Nginx性能监控与调优
例如我希望显示访问最多的ip,就可以使用-g进行指定:
[root@01server ~]# ngxtop -c /usr/local/nginx/conf/nginx.conf -g remote_addr