Nginx监控

转载来源Nginx监控 : https://www.jianshu.com/p/9d53a5e74738

1、nginx的基础监控

  • 进程监控
  • 端口监控

注意: 这两个是必须要加在zabbix监控,加触发器有问题及时告警。
nginx 提供了 ngx_http_stub_status_module.这个模块提供了基本的监控功能

2、监控的指标

1、基本活跃指标
Accepts(接受)、Handled(已处理)、Requests(请求数)是一直在增加的计数器。Active(活跃)、Waiting(等待)、Reading(读)、Writing(写)随着请求量而增减。

2、每秒请求数 – QPS
通过持续的 QPS 监控,可以立刻发现是否被恶意攻击或对服务的可用性进行评估。虽然当问题发生时,通过 QPS 不能定位到确切问题的位置,但是他可以在第一时间提醒你环境可能出问题了

3、服务器错误率
通过监控固定时间间隔内的错误代码(4XX代码表示客户端错误,5XX代码表示服务器端错误)。

4、请求处理时间
请求处理时间也可以被记录在 access log 中,通过分析 access log,统计请求的平均响应时间。 ----$request_time 变量

3、指标的收集

通过在编译时加入 nginx 的 ngx_http_stub_status_module 模块我们可以实时监控以下基本的指标:
1、nginx Stub Status 监控模块安装
先使用命令查看是否已经安装这个模块:

# -V大写会显示版本号和模块等信息、v小写仅显示版本信息
[root@localhost ~]# nginx -V</pre>

注意:是如果没有此模块,需要重新安装,编译命令如下:

./configure –with-http_stub_status_module

具体的使用方法是在执行 ./configure 时,指定 --with-http_stub_status_module,然后通过配置:

[root@localhost ~]# vim /etc/nginx/conf.d/status.conf 
server {
   listen 80;
   server_name localhost;
   location /nginx-status {
   stub_status     on;
   access_log      on;
   }
}

}
2、nginx 状态查看
配置完成后在浏览器中输入http://10.0.105.207/nginx-status 查看显示信息如下:

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

3、Stub Status 参数说明

connection    #连接数,tcp连接                             
request       #http请求,GET/POST/DELETE/UPLOAD

nginx总共处理了26个连接,成功创建26次握手,也就是成功的连接数connection. 总共处理了48个请求

  • 失败连接=(总连接数(accepts)-成功连接数(handled))(相等表示中间没有失败的),
  • Reading : nginx读取到客户端的Header信息数。请求头 -----速度快。
  • Writing :nginx返回给客户端的Header信息数。响应头
  • Waiting :开启keep-alive的情况下,意思就是Nginx说已经处理完正在等候下一次请求指令的驻留连接。
    也可以通过ngx_req_status_module能够统计Nginx中请求的状态信息。需要安装第三方模块

安装模块:

tengine官方说req-status模块默认安装。但是并没有。从github引入第三方模块解决该问题
yum与编译安装的nginx扩展模块安装:
[root@nginx-server ~]# yum install -y unzip
1. 安装,先查看一下当前编译安装nginx的版本
[root@localhost nginx-1.16.0]# nginx -V  
下载或者上传一个和当前的nginx版本一样的nginx的tar包。
[root@nginx-server ~]# tar xzf nginx-1.16.0.tar.gz -C /usr/local/
2.下载ngx_req_status_module 模块, 这是第三方模块需要添加
[root@nginx-server ~]# wget https://github.com/zls0424/ngx_req_status/archive/master.zip -O ngx_req_status.zip
[root@nginx-server ~]# unzip ngx_req_status.zip
[root@nginx-server ~]# cp -r ngx_req_status-master/ /usr/local/ #与解压的nginx在同一级目录下
[root@nginx-server ~]# cd /usr/local/nginx-1.16.0/
[root@nginx-server nginx-1.16.0]# yum -y install pcre pcre-devel openssl openssl-devel gcc gcc-c++   zlib zlib-devel
[root@nginx-server nginx-1.16.0]# yum -y install patch.x86_64
[root@nginx-server nginx-1.16.0]# patch -p1 < ../ngx_req_status-master/write_filter-1.7.11.patch
[root@localhost nginx-1.16.0]# ./configure 添加上原来的参数 --add-module=/usr/local/ngx_req_status-master
[root@localhost nginx-1.16.0]# make -j2
由于原先已有nginx,所以不能执行make install,否则会覆盖掉以前的配置文件及内容
[root@localhost nginx-1.16.0]# mv /usr/sbin/nginx /usr/sbin/nginx_bak
[root@localhost nginx-1.16.0]# cp objs/nginx /usr/sbin/
[root@localhost nginx-1.16.0]# systemctl restart nginx 
[root@localhost nginx-1.16.0]# nginx -V   
如果发现编译的配置文件有变化就成功了!
=========================
指令介绍
 req_status_zone
语法: req_status_zone name string size
默认值: None
配置块: http
# 定义请求状态ZONE,请求按照string分组来排列,例如:
req_status_zone server_url  $server_name$uri 256k;
#域名+uri将会形成一条数据,可以看到所有url的带宽,流量,访问数

#在location中启用请求状态,你可以指定更多zones。
req_status
语法: req_status zone1[ zone2]
默认值: None
配置块: http, server, location

#在当前位置启用请求状态处理程序
req_status_show
语法: req_status_show on
默认值: None
配置块: location

=============================================
配置如下: 需要在http里面配置。
[root@localhost ~]# vim /etc/nginx/nginx.conf
    req_status_zone server_name $server_name 256k;
    req_status_zone server_addr $server_addr 256k;
    req_status_zone server_url  $server_name$uri 256k;

创建配置文件:
[root@localhost ~]# vim /etc/nginx/conf.d/rep_stat.conf
 server {
        listen 80;
        server_name localhost;
        req_status server_name server_addr server_url;

        location /req-status {
        req_status_show on;
        }
}

[root@localhost ~]# nginx -t
[root@localhost ~]# nginx -s reload 

在这里插入图片描述

参考链接 :

Nginx监控 : https://www.jianshu.com/p/9d53a5e74738

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值