nginx 监控
1、nginx的基础监控
- 进程监控
- 端口监控
注意: 这两个是必须要加在zabbix监控,加触发器有问题及时告警。
nginx 提供了 ngx_http_stub_status_module.这个模块提供了基本的监控功能
2、监控的指标
2.1 基本活跃指标
Accepts(接受)、Handled(已处理)、Requests(请求数)是一直在增加的计数器。Active(活跃)、Waiting(等待)、Reading(读)、Writing(写)随着请求量而增减。
2.2 每秒请求数 – QPS
通过持续的 QPS 监控,可以立刻发现是否被恶意攻击或对服务的可用性进行评估。虽然当问题发生时,通过 QPS 不能定位到确切问题的位置,但是他可以在第一时间提醒你环境可能出问题了
2.3 服务器错误率
通过监控固定时间间隔内的错误代码(4XX代码表示客户端错误,5XX代码表示服务器端错误)。
2.4 请求处理时间
请求处理时间也可以被记录在 access log 中,通过分析 access log,统计请求的平均响应时间。 ----$request_time 变量
3、指标的收集
通过在编译时加入 nginx
的 ngx_http_stub_status_module
模块我们可以实时监控以下基本的指标:
3.1 nginx Stub Status 监控模块安装
先使用命令查看是否已经安装这个模块:
# -V大写会显示版本号和模块等信息、v小写仅显示版本信息
[root@localhost ~]# nginx -V
注意:如果没有此模块,需要重新安装,编译命令如下:
./configure –with-http_stub_status_module
具体的使用方法是在执行 ./configure 时,指定 --with-http_stub_status_module,然后通过配置:
将如下内容添加到默认的虚拟主机的在 server 配置块内
location /nginx-status {
stub_status on;
access_log on;
}
3.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.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 --prefix=/usr/local/nginx --group=nginx --user=nginx --sbin-path=/usr/local/nginx/sbin/nginx --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --http-client-body-temp-path=/tmp/nginx/client_body --http-proxy-temp-path=/tmp/nginx/proxy --http-fastcgi-temp-path=/tmp/nginx/fastcgi --pid-path=/var/run/nginx.pid --lock-path=/var/lock/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_gzip_static_module --with-pcre --with-http_realip_module --with-stream --add-module=/usr/local/ngx_req_status-master
[root@localhost nginx-1.16.0]# make -j2
make install
mkdir /tmp/nginx
/usr/local/nginx/sbin/nginx 启动
[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
http {
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
请求状态信息包括以下字段:
- zone_name - 利用req_status_zone定义的分组标准。例如,按照服务器名称对请求进行分组后;
- key - 请求按分组标准分组后的分组标识(即组名)。例如按服务器名称分组时,组名可能是localhost;
- max_active - 该组的最大并发连接数;
- max_bw - 该组的最大带宽;
- traffic - 该组的总流量;
- requests - 该组的总请求数;
- active - 该组当前的并发连接数;
- bandwidth - 该组当前带宽。