内容:
- nginx安装
- ngx_http_stub_status监控连接信息
- ngxtop监控请求信息
- nginx-rrd图形化监控
1.Nginx安装
略过
2.ngx_http_stub_status监控连接信息
2.1 ngx_http_stub_status配置
(1)添加配置
location = /nginx_status{
stub_status on;
access_log off;
allow 127.0.0.1;
deny all;
}
当前配置添加到nginx的 nginx.conf 文件中,
(2)目前本机有nginx,但没有安装 http_stub_status_module 模块,以下是安装流程:
重新添加:--with-http_stub_status_module --with-http_ssl_module
1)进入nginx原文件路径cd nginx-1.18.0
2)重新编译,添加模块--with-http_stub_status_module --with-http_ssl_module./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module
3)编译 make #不要 make installmake
4)备份&拷贝#备份旧程序
cp /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx.bak#拷贝
cp -r objs/nginx /usr/local/nginx/sbin/nginx
5)重启nginx
[root@rich nginx-1.8.0]# /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf6)查看是否安装成功、启动成功
[root@rich nginx-1.8.0]# wget http://127.0.0.1/nginx_status7)检查模块是否加载
[root@rich nginx-1.8.0]# /usr/local/nginx/sbin/nginx -V
nginx version: nginx/1.8.0
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-39) (GCC)
built with OpenSSL 1.0.2k-fips 26 Jan 2017
TLS SNI support enabled
configure arguments: --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module2.2 获取监控信息
(1)使用wget 下载监控信息文件
[root@rich nginx-1.8.0]# wget http://127.0.0.1/nginx_status
--2021-04-29 16:41:52-- http://127.0.0.1/nginx_status
正在连接 127.0.0.1:80... 已连接。
已发出 HTTP 请求,正在等待回应... 200 OK
长度:97 [text/plain]
正在保存至: “nginx_status.1”100%[===============================================================>] 97 --.-K/s 用时 0s
2021-04-29 16:41:52 (21.4 MB/s) - 已保存 “nginx_status.1” [97/97])
由于配置了只能本地连接,所以才去 wget 的方式将监控结果下载到本地文件中。
(2)查看并分析 nginx_status 文件
[root@rich nginx-1.8.0]# ls
auto CHANGES.ru configure html Makefile nginx_status objs src
CHANGES conf contrib LICENSE man nginx_status.1 README
[root@rich nginx-1.8.0]# cat nginx_status
Active connections: 1
server accepts handled requests
4 4 4
Reading: 0 Writing: 1 Waiting: 0
提供以下状态信息:
参数 | 说明 |
| 当前活动的客户端连接数,包括 |
| 接受的客户端连接总数。 |
| 已处理的连接总数。通常,参数值与 |
| 客户请求总数。 |
| nginx正在读取请求标头的当前连接数。 |
| nginx正在将响应写回到客户端的当前连接数。 |
| 当前等待请求的空闲客户端连接数。 |
3. ngxtop 监控请求信息
3.1 ngxtop 安装
(1)安装 python-pip
yum install epel-release
yum install python-pip
(2)安装ngxtop
pip install ngxtop
(3)启动ngxtop报错问题
[root@rich nginx]# ngxtop -c /usr/local/nginx/conf/nginx.conf
Error: Access log file is not provided and ngxtop cannot detect it from your config file (/usr/local/nginx/conf/nginx.conf).
开起日志时:
access_log logs/access.log main;
一定要开起 main 日志格式
打开上图中红圈部分的注释。
3.2 ngxtop使用
- 指定配置文件启动 : ngxtop -c /etc/nginx/nginx.conf
- 查询状态是200:ngxtop -c /etc/nginx/nginx.conf -i 'status == 200'
- 查询访问最多ip:ngxtop -c /etc/nginx/nginx.conf -g remote_addr
(1)指定配置文件启动
[root@rich logs]# ngxtop -c /usr/local/nginx/conf/nginx.conf
监控页面:
该工具只记录启动之后的访问,不记录启动之前的访问信息。
(2)其他不演示了o(* ̄︶ ̄*)o
4.nginx-rrd图形化监控
由于nginx-rrd是使用php实现的,所以在此之前,我们得先安装好php的运行环境,以及安装rrdtool的相关依赖,我这里使用yum进行安装
4.1环境安装
(1)安装命令php运行环境以及rrdtool
yum install -y php php-gd php-soap php-mbstring php-xmlrpc php-dom php-fpm yum install -y perl rrdtool perl-libwww-perl libwww-perl perl-rrdtool
(2)整合nginx和php-fpm
安装好php运行环境以及rrdtool后,我们还需要把nginx和php-fpm进行一个整合。修改php-fpm的配置文件,将文件中的user和group修改为与nginx.conf中的user一致:
命令:vi /etc/php-fpm.d/www.conf 修改这两个参数都为root,这个地方以你nginx.conf配置文件中用户为主 user = root group = root
还需要让nginx支持解析php,在虚拟主机配置文件中,增加如下内容: 需要增加的内容: location ~ .php$ { include fastcgi_params; fastcgi_pass unix:/tmp/php-fcgi.sock; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html$fastcgi_script_name; } 增加内容的位置:在这个配置文件的server{} 中配置
配置好后,启动php-fpm服务,并且重新加载nginx: 启动命令:systemctl start php-fpm 查看状态的命令:netstat -lntp |grep 9000 重启nginx的命令:nginx -s reload
然后在虚拟主机配置文件中所指向的网站根目录下,新建一个简单的php文件,用于测试nginx是否已经能够正常解析php代码: 命令:vi /usr/local/nginx/html/index.php 添加内容:
配置好后,启动php-fpm服务,并且重新加载nginx: 启动命令:systemctl start php-fpm 查看状态的命令:netstat -lntp |grep 9000 重启nginx的命令:nginx -s reload
然后在虚拟主机配置文件中所指向的网站根目录下,新建一个简单的php文件,用于测试nginx是否已经能够正常解析php代码:
命令:vi /usr/local/nginx/html/index.php
添加内容:
在该目录下分别执行 命令: 1. cp etc/nginx-rrd.conf /etc 2. cp usr/sbin/* /usr/sbin 3. rm -rf /usr/local/nginx/html/index.php # 删除之前测试用的php文件 4. cp html/index.php /usr/local/nginx/html/
修改nginx-rrd配置文件,配置数据存储目录以及图片存储目录,如下: 命令:vi /etc/nginx-rrd.conf dir where rrd databases are stored
RRD_DIR="/usr/local/nginx/html/nginx-rrd"; # 数据存储目录 dir where png images are presented
WWW_DIR="/usr/local/nginx/html"; # 图片存储目录
配置完之后,我们还需要使用 crontab 新建定时任务,用于定时执行nginx-rrd的两个脚本,因为nginx-rrd需要定时去采集数据才能实现一个监控的效果: 编辑命令:crontab -e 查看命令:crontab -l
/bin/sh /usr/sbin/nginx-collect # 采集数据脚本 */1 * * * * /bin/sh /usr/sbin/nginx-graph # 生成图片脚本
我们这里设定的定时任务是每一分钟执行一次,可以使用如下命令查看定时任务是否有在执行: 命令:tail -f /var/log/cron
确认定时任务有正常执行后,我们来安装apache的一个压测工具,方便于我们生成大量的请求数据: 安装命令:yum -y install httpd-tools
安装完成后,使用以下命令,对nginx进行压测,可以多执行几次: 命令:ab -n 10000 -c 10 http://127.0.0.1/index.html
命令说明:
-n 指定总共发送多少个请求 -c 指定同时发送多少个请求
使用压测工具产生了一些请求数据后,到浏览器上访问nginx-rrd的index.php文件,效果如下:
5.nginx优化
- 增加工作线程数和并发连接数
- 启用长连接
5.1 配置线程数和并发数
worker_processes 4;#cpu
events {
worker_connections 10240;#每一个进程打开的最大连接数,包含了nginx与客户端和nginx与upstream之间的连接
multi_accept on;#可以一次建立多个连接
use epoll
}
5.2 配置后端Server的长连接
upstream server_pool{
server localhost:8080 weight=1 max_fails=2 fail_timeout=30s
server localhost:8081 weight=1 max_fails=2 fail_timeout=30s
keepalive 300; #300个长连接
}
location / {
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_pass http://server_pool/;
}