Nginx-深入剖析NginxStatus

一、简介

NginxStatus是Nginx自带的状态模块,提供了实时的HTTP请求统计和服务器状态监控。NginxStatus默认是关闭的,需要在nginx.conf配置文件中开启。

二、开启NginxStatus

location /nginx_status {
    stub_status on;
    access_log off;
    allow 127.0.0.1;
    allow localhost;
    deny all;
}

以上配置代码可以在http段或server段中配置,建议在server段中配置。

其中,location /nginx_status 配置了NginxStatus的访问地址,stub_status on开启了状态模块,access_log off关闭了访问日志记录,allow 127.0.0.1; allow localhost;允许本地IP访问,deny all;拒绝其他IP访问。完成上述配置后,重启Nginx服务,访问X.X.X.X/nginx_status,就可以访问Nginx子页面了。

三、查看NginxStatus信息

配置好NginxStatus后,可以通过http://IP地址/nginx_status 来访问状态信息。

NginxStatus提供了如下信息:

  • Active connections:当前活跃的连接数。
  • server accepts handled requests:nginx处理连接成功的数量数目,处理请求的数量、和总的请求数。
  • Reading:nginx正在读取请求头。
  • Writing:nginx正在将响应写回客户端。
  • Waiting:nginx正在等待另一个请求。开启keep-alive的情况下,这个值等于 active – (reading + writing),意思就是Nginx已经处理完成,正在等候下一次请求指令的驻留连接。

示例:

Active connections: 1 
server accepts handled requests
1 1 2 
Reading: 0 Writing: 1 Waiting: 0

四、使用NginxStatus统计及监控

1. 统计网站流量和请求情况

NginxStatus提供了requests和bytes两个信息,可以通过脚本定时获取并统计,实现对网站流量和请求情况的监控。

以下是获取requests和bytes信息的python脚本:

import urllib.request
import re
import time

url = 'http://localhost/nginx_status'

while True:
    response = urllib.request.urlopen(url)
    html = response.read().decode('utf-8')
    status = re.findall(r'Requests\s+(\d+)', html)[0]    # requests信息
    traffic = re.findall(r'(\d+)\skB', html)[0]    # bytes信息
    print('Requests:{} | Traffic:{}kB'.format(status, traffic))
    time.sleep(5)    # 每5秒更新一次

2. 监控服务器负载情况

NginxStatus提供了Active connectionsReadingWritingWaiting四个信息,可以用来监控服务器的负载情况。

以下是根据Active connections信息,通过脚本实现自动热备的例子:

#!/bin/bash

# 配置备用服务器地址
backup_server=192.168.1.2

while true
do
    # 获取Active连接数
    active_conn=$(curl -s http://localhost/nginx_status | grep 'Active' | awk '{print $3}')

    # 当Active连接数大于100时,自动将流量切到备用服务器
    if [ $active_conn -gt 100 ]
    then
        sed -i 's/server\ 192\.168\.1\.1/server\ 192\.168\.1\.2/g' /etc/nginx/nginx.conf
        nginx -s reload
    fi

    # 当Active连接数小于50时,自动将流量切回主服务器
    if [ $active_conn -lt 50 ]
    then
        sed -i 's/server\ 192\.168\.1\.2/server\ 192\.168\.1\.1/g' /etc/nginx/nginx.conf
        nginx -s reload
    fi

    sleep 10    # 每10秒检查一次
done

3. 检测Nginx服务状态

NginxStatus提供了server accepts handled requests信息,可以用来监控服务器的服务状态。

以下是检测Nginx服务状态的python脚本:

import urllib.request
import re
import time
import subprocess

url = 'http://localhost/nginx_status'

while True:
    response = urllib.request.urlopen(url)
    html = response.read().decode('utf-8')
    handled = re.findall(r'Handled\s+(\d+)', html)[0]
    status = subprocess.Popen('service nginx status', shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    output = status.communicate()[0].decode('utf-8')

    if 'active (running)' not in output or int(handled) == 0:
        subprocess.Popen('service nginx restart', shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
        time.sleep(30)    # 等待30秒后再次检测

    time.sleep(5)    # 每5秒检查一次

五、总结

NginxStatus是一个实用的状态模块,可以用来监控服务器的状态、统计网站流量和请求情况、自动热备和检测Nginx服务状态。

开启NginxStatus只需要简单的配置,即可实现以上功能。

你可能感兴趣的:(nginx,运维)