当前位置: 代码网 > 服务器>服务器>Nginx > nginx status配置及参数配置小结

nginx status配置及参数配置小结

2024年05月26日 Nginx 我要评论
今天简单介绍下如何监控nginx的状态。其实要监控nginx的状态非常简单,它内建了一个状态页,只需修改nginx配置启用status即可,对于想了解nginx的状态以及监控nginx非常有帮助。1.

今天简单介绍下如何监控nginx的状态。

其实要监控nginx的状态非常简单,它内建了一个状态页,只需修改nginx配置启用status即可,对于想了解nginx的状态以及监控nginx非常有帮助。

1. 启用nginx status配置

大概nginx配置文件,在默认主机里面加上location或者你希望能访问到的主机里面加上如下配置。

   #nginx 状态监控 ,需要确认是否安装监控模块 http_stub_status_module,如果已经安装该模块,可以为 nginx 启用状态监控:
   server {
	    listen 91;
	    
	    location /status {            
	      stub_status on;            
	      access_log off;            
	    }
    }    

2. 重启nginx

操作命令比较简单,请依照你的环境重启你的nginx即可。

3. 打开status页面

在浏览器中输入nginx的地址:http://192.168.2.109:91/status,即可查看nginx的状态信息

在这里插入图片描述

4. nginx status详解

  • active connections – 活跃的连接数量
  • server accepts handled requests — 总共处理了93个连接 , 成功创建93次握手, 总共处理了152个请求。
  • reading — 读取客户端的连接数。
  • writing — 响应数据到客户端的数量。
  • waiting — 开启 keep-alive 的情况下,这个值等于 active – (reading+writing), 意思就是 nginx 已经处理完正在等候下一次请求指令的驻留连接。

5、使用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秒检查一次

6、总结

到此这篇关于nginx status配置及参数配置的文章就介绍到这了,更多相关nginx status配置内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com