Nginx内置状态信息(http_stub_status)

时间:2022-06-05
本文章向大家介绍Nginx内置状态信息(http_stub_status),主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

Nginx提供了一个内置的状态信息监控页面,可用于监控Nginx的整体访问情况。这个内置功能由模块ngx_http_stub_status_module实现。如果在安装的过程中没有启用该模块,则无法使用其状态信息。本文主要描述这个状态页面的启用以及相关状态值描述。

一、环境信息

# more /etc/redhat-release
CentOS release 6.7 (Final)

# /u01/app/nginx/sbin/nginx -v
nginx version: nginx/1.8.1

查看是否启用了with-http_stub_status_module模块,如果没有重新编译一次,增加该模块即可
# /u01/app/nginx/sbin/nginx -V
nginx version: nginx/1.8.1
built by gcc 4.4.7 20120313 (Red Hat 4.4.7-17) (GCC)
built with OpenSSL 1.0.1e-fips 11 Feb 2013
TLS SNI support enabled
configure arguments: --prefix=/u01/app/nginx --sbin-path=/u01/app/nginx/sbin
--conf-path=/u01/app/nginx/conf/nginx.conf --error-log-path=/u01/log/nginx/error.log
--http-log-path=/u01/log/nginx/access.log --pid-path=/var/run/nginx.pid
--lock-path=/var/run/nginx.lock --http-client-body-temp-path=/tmp/client_temp
--http-proxy-temp-path=/tmp/proxy_temp --http-fastcgi-temp-path=/tmp/fastcgi_temp
--http-uwsgi-temp-path=/tmp/uwsgi_temp --http-scgi-temp-path=/tmp/scgi_temp
--user=nginx --group=nginx --with-http_ssl_module --with-http_realip_module
--with-http_addition_module --with-http_sub_module --with-http_dav_module
--with-http_flv_module --with-http_mp4_module --with-http_gunzip_module
--with-http_gzip_static_module --with-http_random_index_module
--with-http_secure_link_module --with-http_stub_status_module
--with-http_auth_request_module --with-mail --with-mail_ssl_module --with-file-aio
--with-http_spdy_module --with-ipv6
--add-module=/usr/local/src/nginx_mod_h264_streaming-2.2.7

二、配置Nginx启用状态功能

下面我们添加一个单独的配置文件以启用该功能
# vim /u01/app/nginx/conf/conf.d/nginx_status.conf
server {
    listen 10061;
    location /nginx_status {
    stub_status on;
    access_log off;
    allow 127.0.0.1;
    deny all;
    }
}

## Author : Leshami
## Blog    : http://blog.csdn.net/leshami
语法检查
# /u01/app/nginx/sbin/nginx -t
nginx: the configuration file /u01/app/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /u01/app/nginx/conf/nginx.conf test is successful

重载nginx
# /u01/app/nginx/sbin/nginx -s reload

三、状态信息描述

打开网站首页,多几次点击,然后在服务器端查看nginx状态信息 # curl http://127.0.0.1:10061/nginx_status Active connections: 19 server accepts handled requests 943 943 4651 Reading: 0 Writing: 1 Waiting: 18

当前的活动连接数为19个 共总接受了943个连接,处理的连接数为943,客户端请求总数为4651

提供以下状态信息:

Active connections   当前活动客户端连接的数量,包括Waiting连接。

accepts   接受的客户端连接总数。

handled   处理的连接总数。通常情况下该值等于accepts的值,除非达到某个资源限制(例如, worker_connections限制)。

requests   客户端请求的总数。

Reading   nginx正在读取请求头的当前连接数。

Writing   nginx将响应写回客户端的当前连接数。

Waiting   当前等待请求的空闲客户端连接数。

嵌入式变量

该ngx_http_stub_status_module模块支持以下嵌入式变量(1.3.14):

$connections_active   与Active connections值相同;

$connections_reading   与Reading值相同;

$connections_writing   与Writing值相同;

$connections_waiting   与Waiting值相同。

四、一些常用的Nginx日志命令

1.根据访问IP统计UV   # awk ‘{print $1}’ /tmp/http-access.log.0919|sort | uniq -c |wc -l   355

2.统计访问URL统计PV   # awk ‘{print $7}’ /tmp/http-access.log.0919|wc -l

3.查询访问最频繁的URL   # awk ‘{print $7}’ /tmp/http-access.log.0919|sort | uniq -c |sort -n -k 1 -r|more

4.查询访问最频繁的IP   # awk ‘{print $1}’ /tmp/http-access.log.0919|sort | uniq -c |sort -n -k 1 -r|more

5.根据时间段统计查看日志   # cat access.log| sed -n ‘/14/Mar/2015:21/,/14/Mar/2015:22/p’|more

五、模块详细描述参考链接

http://nginx.org/en/docs/http/ngx_http_stub_status_module.html