本文共 3096 字,大约阅读时间需要 10 分钟。
Nginx提供了一个内置的状态信息监控页面,可用于监控Nginx的整体访问情况。这个内置功能由模块ngx_http_stub_status_module实现。如果在安装的过程中没有启用该模块,则无法使用其状态信息。本文主要描述这个状态页面的启用以及相关状态值描述。
# more /etc/redhat-releaseCentOS release 6.7 (Final)# /u01/app/nginx/sbin/nginx -vnginx version: nginx/1.8.1查看是否启用了with-http_stub_status_module模块,如果没有重新编译一次,增加该模块即可# /u01/app/nginx/sbin/nginx -Vnginx version: nginx/1.8.1built by gcc 4.4.7 20120313 (Red Hat 4.4.7-17) (GCC)built with OpenSSL 1.0.1e-fips 11 Feb 2013TLS SNI support enabledconfigure 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
下面我们添加一个单独的配置文件以启用该功能# vim /u01/app/nginx/conf/conf.d/nginx_status.confserver { 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 -tnginx: the configuration file /u01/app/nginx/conf/nginx.conf syntax is oknginx: configuration file /u01/app/nginx/conf/nginx.conf test is successful重载nginx# /u01/app/nginx/sbin/nginx -s reload
打开网站首页,多几次点击,然后在服务器端查看nginx状态信息
# curl Active connections: 19server accepts handled requests943 943 4651Reading: 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值相同。1.根据访问IP统计UV
# awk '{print $1}' /tmp/http-access.log.0919|sort | uniq -c |wc -l 3552.统计访问URL统计PV
# awk '{print $7}' /tmp/http-access.log.0919|wc -l3.查询访问最频繁的URL
# awk '{print $7}' /tmp/http-access.log.0919|sort | uniq -c |sort -n -k 1 -r|more4.查询访问最频繁的IP
# awk '{print $1}' /tmp/http-access.log.0919|sort | uniq -c |sort -n -k 1 -r|more5.根据时间段统计查看日志
# cat access.log| sed -n '/14/Mar/2015:21/,/14/Mar/2015:22/p'|more