插件窝 干货文章 详解:配置启用NGINX状态页面

详解:配置启用NGINX状态页面

NGINX status 配置 访问 930    来源:    2025-05-22

NGINX状态页面配置详解

NGINX状态页面(stub_status)是一个内置模块,可以提供基本的NGINX服务器状态信息。下面我将详细介绍如何配置和使用这个功能。

1. 检查NGINX是否包含stub_status模块

在配置前,先确认你的NGINX编译时包含了--with-http_stub_status_module

nginx -V 2>&1 | grep -o with-http_stub_status_module

如果有输出则表示支持该功能。

2. 基础配置方法

在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;  # 如需密码保护
    }
}

3. 配置说明

  • stub_status:启用状态页面
  • allow/deny:限制访问IP范围,强烈建议配置
  • auth_basic:可选的基础认证保护

4. 重载NGINX配置

nginx -t && nginx -s reload

5. 访问状态页面

访问配置的URL(如http://status.example.com/nginx_status),将看到类似以下内容:

Active connections: 291 
server accepts handled requests
 16630948 16630948 31070465 
Reading: 6 Writing: 179 Waiting: 106 

6. 状态信息解读

  • Active connections: 当前活跃连接数
  • accepts: 已接受的客户端连接总数
  • handled: 已处理的连接总数
  • requests: 客户端请求总数
  • Reading: NGINX正在读取请求头的连接数
  • Writing: NGINX正在向客户端写入响应的连接数
  • Waiting: 保持活跃的空闲客户端连接数

7. 安全注意事项

  1. 限制访问:务必使用allow/deny限制可访问IP
  2. 认证保护:考虑添加基础认证
  3. HTTPS:如果通过公网访问,应配置HTTPS加密
  4. 自定义URL:不要使用常见的/status路径

8. 高级配置示例

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;  # 可选:关闭此位置的访问日志
    }
}

9. 监控工具集成

状态页面数据可以被多种监控工具收集,如: - Prometheus + nginx_exporter - Zabbix - Nagios - Datadog

通过正确配置NGINX状态页面,你可以有效监控服务器性能并及时发现潜在问题。