检查与安装状态模块
首先确认系统中是否已安装用于检查状态的模块。
[root@localhost ~]# nginx -V
nginx version: nginx/1.12.2
built by gcc 4.8.5 20260623 (Red Hat 4.8.5-36) (GCC)
configure arguments: --prefix=/usr/local/nginx --with-http_sub_module
如果当前环境尚未安装该模块,则需要对Nginx进行重新编译和安装操作。
在编译过程中,必须指定启用检查状态模块,即添加参数--with-http_stub_status_module。
[root@localhost ~]# cd /usr/local/src/nginx-1.12.2/
[root@localhost ~]# ./configure --prefix=/usr/local/nginx --with-http_stub_status_module
[root@localhost ~]# make && make install
配置Nginx状态访问路径
接下来需要编辑Nginx的主配置文件以定义状态访问路径。
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
server {
listen 80;
server_name localhost;
#access_log logs/host.access.log main;
location /nginx_status {
stub_status on;
access_log off;
#allow 127.0.0.1; ##可对该页面的访问者进行过滤
#deny all;
}
}
[root@localhost ~]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@localhost ~]# nginx -s reload
修改配置后,务必测试语法是否正确以确保配置无误。
[root@localhost ~]# curl http://192.168.10.110:80/nginx_status
Active connections: 1
server accepts handled requests
1 1 1
Reading: 0 Writing: 1 Waiting: 0
解析状态输出内容详解
最后详细解析输出内容的各项含义,以便准确理解服务器负载情况。
| 第一行 Active connections: 1 ——活跃的连接数量,包括等待客户端数0 |
| 第二行 server accepts handled requests —— 总共处理了1个连接 , 成功创建1次握手, 总共处理了1个请求 |
| 第三行 Reading — 读取客户端的连接数,Writing — 响应数据到客户端的数量,Waiting — 开启 keep-alive 的情况下,这个值等于 active – (reading+writing), 意思就是 Nginx 已经处理完正在等候下一次请求指令的驻留连接. |
上述步骤涵盖了Nginx状态检测的完整流程,希望能为相关学习提供有益参考,欢迎继续支持本站获取更多技术内容。