linux's docs

26.10.0: NGINX配置-NGX&PHP状态页面


1. NGINX开启状态收集

1) 查看nginx编译时是否将状态模块编译

# /usr/local/nginx/sbin/nginx -V
nginx: nginx version: nginx/1.0.0
nginx: TLS SNI support enabled
nginx: configure arguments: --user=www --group=www --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module

2) 配置nginx status访问页面

vim /usr/local/nginx/conf/nginx.conf
************************************
## 放在server{}(server_name localhost)块中
   location ~ /nginx_status {
            stub_status on;
            access_log off;
            allow 127.0.0.1;
            deny all;
   }
***************************************
# 重启服务
/usr/local/nginx/sbin/nginx -s reload

3) 访问监控页面

curl http://127.0.0.1/nginx_status
Active connections: 3491
server accepts handled requests
 1159391 1159391 1262564
Reading: 85 Writing: 3406 Waiting: 0

## 状态项详解:
Active connections,活跃连接
server accepts handled requests,
成功处理的连接数,成功创建的握手数,总共处理的请求数
Reading,读取到客户端的Header数
Writing,返回给客户端的Header数
Waiting,驻留连接(在keep-alive开启时,等于active-(reading+writing)

## 访问效率很高,请求被很快处理的情况下,waiting数多是正常的,代表了已成功建立起连接,如果reading和writing数比较多,说明并发比较大,正在处理连接请求。

4) 创建状态收集脚本

vim nginx_status.sh
***********************************
#!/bin/bash

case $1 in
  active)
    curl -s http://127.0.0.1/nginx_status | awk '/Active/ {print $3}' ;;
  accepts)
    curl -s http://127.0.0.1/nginx_status | awk 'NR==3 {print $1}' ;;
  handled)
    curl -s http://127.0.0.1/nginx_status | awk 'NR==3 {print $2}' ;;
  requests)
    curl -s http://127.0.0.1/nginx_status | awk 'NR==3 {print $3}' ;;
  reading)
    curl -s http://127.0.0.1/nginx_status | awk '/Reading/ {print $2}' ;;
  writing)
    curl -s http://127.0.0.1/nginx_status | awk '/Writing/ {print $4}' ;;
  waiting)
    curl -s http://127.0.0.1/nginx_status | awk '/Waiting/ {print $6}' ;;
  *)
    echo "Usage: $0 { active | accepts | handled | requests | reading | writing | waiting }" ;;
esac
***********************************

2. php状态查看

1) 配置php-fpm,打开status开关

vim /etc/php/php-fpm.conf
***********************************
pm.status_path = /status
***********************************

2) 配置nginx,增加php-fpm访问路径

vim /etc/nginx/conf/nginx.conf
***********************************
location ~ ^/(status|ping)$ {
 allow 127.0.0.1;
 allow 172.16.2.28;      # 监控端的ip
 deny all;
 fastcgi_pass   127.0.0.1:9000;
 fastcgi_index  index.php;
 fastcgi_param  SCRIPT_FILENAME  /data/web/$fastcgi_script_name;
 include        fastcgi_params;
}
***********************************

3) 平滑重启nginx

nginx -t
nginx: the configuration file /data/server/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /data/server/nginx/conf/nginx.conf test is successful
nginx -s reload

4) 访问php-fpm状态页面

# curl http://127.0.0.1/status
accepted conn:   157
pool:             www
process manager:  static
idle processes:   50
active processes: 0
total processes:  50

5) 状态页面内容和格式详解

## 状态页面内容
; information:
;   accepted conn    - the number of request accepted by the pool;
;   pool             - the name of the pool;
;   process manager  - static or dynamic;
;   idle processes   - the number of idle processes;
;   active processes - the number of active processes;
;   total processes  - the number of idle + active processes.
; The values of 'idle processes', 'active processes' and 'total processes' are
; updated each second. The value of 'accepted conn' is updated in real time.

## 输出内容格式
Examples for summary status page:

1、http://127.0.0.1/status
2、http://127.0.0.1/status?json
3、http://127.0.0.1/status?html
4、http://127.0.0.1/status?xml

Example for detailed status page:

1、http://127.0.0.1/status?full
2、http://127.0.0.1/status?json&full
3、http://127.0.0.1/status?html&full
4、http://127.0.0.1/status?xml&full

6) 其他

发现php 5.3.3输出的php-fpm状态信息跟网络上查到的不一样,仔细查看php-fpm.conf模版才知道,在pm.status_path配置项之上有很详细的输出内容解释,新版本的输出内容和5.3.3的输出内容不同

Contents