NGINX状态页面(stub_status)是一个内置模块,可以提供基本的NGINX服务器状态信息。下面我将详细介绍如何配置和使用这个功能。
在配置前,先确认你的NGINX编译时包含了--with-http_stub_status_module
:
nginx -V 2>&1 | grep -o with-http_stub_status_module
如果有输出则表示支持该功能。
在NGINX配置文件中(通常在/etc/nginx/nginx.conf
或/etc/nginx/conf.d/
下的某个文件)添加以下内容:
server {
listen 80;
server_name status.example.com; # 替换为你的域名或IP
location /nginx_status {
stub_status;
allow 192.168.1.0/24; # 只允许特定IP访问
allow 127.0.0.1; # 允许本地访问
deny all; # 拒绝其他所有访问
# auth_basic "NGINX Status";
# auth_basic_user_file /etc/nginx/.htpasswd; # 如需密码保护
}
}
stub_status
:启用状态页面allow/deny
:限制访问IP范围,强烈建议配置auth_basic
:可选的基础认证保护nginx -t && nginx -s reload
访问配置的URL(如http://status.example.com/nginx_status
),将看到类似以下内容:
Active connections: 291
server accepts handled requests
16630948 16630948 31070465
Reading: 6 Writing: 179 Waiting: 106
allow/deny
限制可访问IP/status
路径server {
listen 443 ssl;
server_name status.example.com;
ssl_certificate /path/to/cert.pem;
ssl_certificate_key /path/to/key.pem;
location /custom_status_path {
stub_status;
allow 10.0.0.0/8;
deny all;
auth_basic "NGINX Status";
auth_basic_user_file /etc/nginx/.htpasswd;
access_log off; # 可选:关闭此位置的访问日志
}
}
状态页面数据可以被多种监控工具收集,如: - Prometheus + nginx_exporter - Zabbix - Nagios - Datadog
通过正确配置NGINX状态页面,你可以有效监控服务器性能并及时发现潜在问题。