Nginx的七种状态

今天,我们通过shell脚本来对Nginx进行状态跟踪。
操作步骤:
1)启动Nginx的跟踪服务;
2)编写shell脚本获取;

1. 启动Nginx的跟踪服务

Nginx 的功能模块中有一个ngx_http_stub_status_module的模块,记录着Nginx的基本访问状态信息,但需要在配置才能启动起来。
注意:yum安装方式也默认已安装该模块;

# 1. 安装Nginx
[root@shellLab ~]# yum install -y nginx
# 2. 启动跟踪服务
[root@shellLab nginx]# cd /etc/nginx
[root@shellLab nginx]# cat nginx.conf
worker_processes 1;
events {
    worker_connections 1024;
}
http {
    include mime.types;
    default_type application/octet-stream;
    sendfile on;
    keepalive_timeout 65;
    server {
        listen 80;
        server_name localhost;
        location / {
            root html;
            index index.html index.htm;
        }
        error_page 500 502 503 504 /50x.html;
        location = /50x.html {
            root html;
        }
        # 启动状态跟踪
        location /nginx_status {
            stub_status on; #打开状态跟踪的功能
            access_log off; #关闭记录访问日志的功能
        }
    }
}
[root@shellLab nginx]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@shellLab nginx]# nginx 
[root@shellLab nginx]# netstat -lntup
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name   
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 1405/nginx          
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1188/sshd           
tcp 0 0 :::22 :::* LISTEN 1188/sshd       
# 3. curl检测,获取到当前Nginx的状态信息    
[root@shellLab nginx]# curl 10.0.0.88/nginx_status
Active connections: 1 
server accepts handled requests
 1 1 1 
Reading: 0 Writing: 1 Waiting: 0 

Bash

Copy

2. 编写脚本获取状态

[root@shellLab scripts]# cat nginx_status.sh
#!/bin/bash
################################################
# Author: alys114
# Blog: blog.alys114.com
# Time: 2018-11-19 09:34:54
# Name: nginx_status.sh
# Version: V1.0
# Description: This is Nginx status check function
################################################

NGINX_URL=$2
NGINX_PORT=$3

[ ! $NGINX_URL ] && {
echo $"USAGE:$0 {active|reading|writing|waiting|accepts|handled|requests} url [port]"
exit 1 
    }

[ ! $NGINX_PORT ] && {
 NGINX_PORT=80
}

NGINX_COMMAND=$1
nginx_active(){
 /usr/bin/curl -s "http://$NGINX_URL:"$NGINX_PORT"/nginx_status/" |awk '/Active/ {print $NF}'
}
nginx_reading(){
 /usr/bin/curl -s "http://$NGINX_URL:"$NGINX_PORT"/nginx_status/" |awk '/Reading/ {print $2}'
}
nginx_writing(){
 /usr/bin/curl -s "http://$NGINX_URL:"$NGINX_PORT"/nginx_status/" |awk '/Writing/ {print $4}'
}
nginx_waiting(){
 /usr/bin/curl -s "http://$NGINX_URL:"$NGINX_PORT"/nginx_status/" |awk '/Waiting/ {print $6}'
}
nginx_accepts(){
 /usr/bin/curl -s "http://$NGINX_URL:"$NGINX_PORT"/nginx_status/" |awk 'NR==3 {print $1}'
}
nginx_handled(){
 /usr/bin/curl -s "http://$NGINX_URL:"$NGINX_PORT"/nginx_status/" |awk 'NR==3 {print $2}'
}
nginx_requests(){
 /usr/bin/curl -s "http://$NGINX_URL:"$NGINX_PORT"/nginx_status/" |awk 'NR==3 {print $3}'
}
case $NGINX_COMMAND in
 active)
  nginx_active;
  ;;
 reading)
  nginx_reading;
  ;;
 writing)
  nginx_writing;
  ;;
 waiting)
  nginx_waiting;
  ;;
 accepts)
  nginx_accepts;
  ;;
 handled)
  nginx_handled;
  ;;
 requests)
  nginx_requests;
  ;;
 *)
  echo $"USAGE:$0 {active|reading|writing|waiting|accepts|handled|requests} url [port]"
esac

# 2. 我们来测试一把
[root@shellLab scripts]# curl 10.0.0.88/nginx_status
Active connections: 1 
server accepts handled requests
 7 7 7 
Reading: 0 Writing: 1 Waiting: 0 
[root@shellLab scripts]# sh nginx_status.sh handled 10.0.0.88
8
[root@shellLab scripts]# sh nginx_status.sh active 10.0.0.88
1
[root@shellLab scripts]# sh nginx_status.sh requests 10.0.0.88
10

Bash

Copy

Ngnix状态跟踪指标解析:

  • active connections >> 对后端发起的活动连接数;
  • server accepts handled requests >> nginx 总共处理了[accepts]个连接, 成功创建[handled]次握手,总共处理了[requests]个请求
  • reading >> nginx 读取到客户端的Header信息数;
  • writing >> nginx 返回给客户端的Header信息数;
  • waiting >> 开启 keep-alive 的情况下,这个值等于 active – (reading + writing),意思就是Nginx说已经处理完正在等候下一次请求指令的驻留连接;
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值