说明:
http_stub_status模块能够获取Nginx的并发连接,请求等。
因 此模块非核心模块,所以需要在编译的时候需手动添加编译参数–with-http_stub_status_module
#/usr/local/nginx/sbin/nginx -V ./configure --user=www --group=www --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module
实现:
1.创建perl脚本pwd.pl,作为nginx basic auth模块的密码生成器,代码如下
#!/usr/bin/perl use strict; my $pw=$ARGV[0]; print crypt($pw,$pw)."\n";
也可以使用Apache htpasswd命令生成密码。htpasswd命令具体使用方法请看:Apache htpasswd命令用法详解及示例。
2.编辑nginx.conf文件,添加以下内容
#vim /usr/local/nginx/conf/nginx.conf
location /nginx_status {
auth_basic "NginxStatus";
auth_basic_user_file /lnmp/nginx/conf/htpasswd;
stub_status on;
access_log off;
allow 127.0.0.1; //本机测试需添加此条记录
deny 192.168.1.1;
allow 192.168.1.0/24; //允许192.168.1.0/24访问,拒绝192.168.1.1访问
allow 10.1.1.0/16;
allow 2620:100:e000::8001; //此处是ipv6的地址
deny all; //除了上述地址,拒绝所有连接。写allow,deny时,注意顺序
}
3、设置pwd.pl的可执行权限并执行./pwd.pl,将显示出来的密码复制。
4、创建或打开 /lnmp/nginx/conf/htpasswd文件,按用户名:加密后的密码的格式设置。
5.重启nginx
/usr/local/nginx/sbin/nginx -t //测试配置是否正确 /usr/local/nginx/sbin/nginx -s reload
6.测试and参数说明
浏览器地址栏输入http://ip/nginx_status,输入设置的用户名密码即可看到”stub status” 模块返回的状态信息跟 mathopd’s 的状态信息很相似.
返回的状态信息如下:
Active connections: 291
server accepts handled requests
: 16630948 16630948 31070465
Reading: 6 Writing: 179 Waiting: 106
active connections — 对后端发起的活动连接数
server accepts handled requests — nginx 总共处理了 16630948 个连接, 成功创建 16630948 次握手 (证明中间没有失败的), 总共处理了 31070465 个请求 (平均每次握手处理了 1.8个数据请求)
reading — nginx 读取到客户端的Header信息数
writing — nginx 返回给客户端的Header信息数
waiting — 开启 keep-alive 的情况下,这个值等于 active – (reading + writing),意思就是Nginx说已经处理完正在等候下一次请求指令的驻留连接
附录:
1、可以用此shell脚本算出平均每秒的请求数
#!/bin/bash
QINGQIU1=`curl -s http://ip/nginx-status | awk '/server accepts handled requests/ {getline a;split(a,s); print s[length(s)]}'`
sleep 5
QINGQIU2=`curl -s http://ip/nginx-status | awk '/server accepts handled requests/ {getline a;split(a,s); print s[length(s)]}'`
if [ $QINGQIU2 -gt 0 ]
then
QINGQIU=`expr $QINGQIU2 - $QINGQIU1`
PER=`expr $QINGQIU / 5`
echo "The nginx request is ${PER} per second"
fi
2、这两个网址是讲得还不错,先放这存着
http://blog.kovyrin.net/2006/04/29/monitoring-nginx-with-rrdtool/
http://dev.2xlp.com/svn/nginx_config/trunk/conf/_rrd/__README__.txt
参考资料:
nginx添加http_stub_status查看并发等:http://coolnull.com/150.html
利用nginx-status监控nginx服务器状态:http://my.oschina.net/gorillaz/blog/107878?p=1
0 条评论。