可以使用cacti、nagios等监控程序监控nginx服务器,不过有时候不用那么复杂,用nginx-rrd就能完成连接、请求的监控功能。

首先编译安装nginx,注意加载stub_status模块,Nginx中的stub_status模块主要用于查看Nginx的一些状态信息.
本模块默认是不会编译进Nginx的,如果你要使用该模块,则要在编译安装Nginx时指定

  1. tar xzvf nginx-1.2.0.tar.gz
  2. cd  nginx-1.2.0
  3. ./configure --prefix=/usr/local/nginx --with-http_stub_status_module
  4. make && make install

yum安装相关的perl、rrdtool的rpm包

  1. yum install perl rrdtool perl-libwww-perl libwww-perl perl-rrdtool

 rrdtool 和相应的perl 被安装上后,开始安装nginx-rrd

 

  1. tar zxvf nginx-rrd-0.1.4.tgz
  2. cd nginx-rrd-0.1.4
  3. cp -p usr/sbin/* /usr/sbin     //复制主程序文件到 /usr/sbin 下
  4. cp -p etc/nginx-rrd.conf /etc  //复制配置文件到 /etc 下

  5. 创建nginx-rrd生成目录
  6. mkdir /usr/local/nginx/html/rrd
  7. cp html/index.php /usr/local/nginx/html

.php文件的默认访问密码WCO。

用nginx-rrd监控nginx访问数_html

 

编辑修改/etc/nginx-rrd.conf

  1. vim /etc/nginx-rrd.conf

  2. #####################################################
  3. #
  4. # dir where rrd databases are stored
  5. RRD_DIR="/usr/local/nginx/html/rrd";
  6. # dir where png images are presented
  7. WWW_DIR="/usr/local/nginx/html";
  8. # process nice level
  9. NICE_LEVEL="-19";
  10. # bin dir
  11. BIN_DIR="/usr/sbin";
  12. # servers to test
  13. # server_utl;server_name
  14. SERVERS_URL="http://www.linuxom.com/nginx_status;www.linuxom.com"

多个虚拟主机,可以SERVERS_URL中空格分开,前部分为nginx_status 的地址,后面为被监控主机的域名。

查看已安装的nginx是否包含stub_status模块

  1. /usr/local/nginx/sbin/nginx -V
  2. nginx version: Nginx/1.2.0
  3. configure arguments: --with-http_stub_status_module

确定支持stub_status模块后编辑修改nginx.conf

  1. vim /usr/local/nginx/conf/nginx.conf

  2. //server{} 中,需要已经加入如下内容
  3. location / {
  4. root   html;
  5. index  index.php index.html index.htm;

  6. location /nginx_status {
  7. stub_status on;   //这个选项参数就是在编译时对stub_status模块的支持,如果不编译加入则会在启动nginx时有警告信息
  8. access_log off;
  9. }


  10. }

  11. #error_page  404              /404.html;

  12. # redirect server error pages to the static page /50x.html
  13. #
  14. error_page   500 502 503 504  /50x.html;
  15. location = /50x.html {
  16. root   html;
  17. }

  18. # proxy the PHP scripts to Apache listening on 127.0.0.1:80
  19. #
  20. #location ~ \.php$ {
  21. #    proxy_pass   http://127.0.0.1;
  22. #}

  23. # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
  24. #
  25. // 去掉如下内容的注释,这里是对php的支持,注意文件路径
  26. location ~ \.php$ {
  27. root           html;
  28. fastcgi_pass   127.0.0.1:9000;
  29. fastcgi_index  index.php;
  30. fastcgi_param  SCRIPT_FILENAME  /usr/local/nginx/html$fastcgi_script_name;
  31. include        fastcgi_params;
  32. }

重启Nginx服务器

  1. /usr/local/nginx/sbin/nginx -s reload

设置定时收集数据

  1. crontab -e
  2. * * * * * root /usr/sbin/nginx-collect
  3. */10 * * * * root /usr/sbin/nginx-graph

 

访问页面 ​​http://域名/index.php​​,nginx的连接数如下图

 

用nginx-rrd监控nginx访问数_nginx_02


 nginx请求数如下图

用nginx-rrd监控nginx访问数_php_03