Nginx 提供了 stub_status 模块,允许你检查 Nginx 的性能状态。你需要在 Nginx 配置文件中为特定的 server 或 location 块启用它。

以下是如何设置 stub_status 的步骤:

1)编辑 Nginx 配置文件(通常是 /etc/nginx/nginx.conf 或者 /etc/nginx/conf.d/your-config.conf)。

2)在 server 或 location 块中添加 stub_status 指令。

例如,要为一个特定的 server 添加状态检查:

server {
    listen 80;
    server_name localhost;

    location /nginx_status {
        stub_status on;
        access_log off;
        allow 127.0.0.1; # 只允许本地访问
        deny all;        # 拒绝其他IP访问
    }
}

3)重新加载 Nginx 配置以应用更改:

sudo nginx -s reload

现在,你可以通过 curl 命令来获取 Nginx 的状态信息:

curl http://localhost/nginx_status

输出示例:

Active connections: 43 
server accepts handled requests
 7368 7368 10993 
Reading: 0 Writing: 5 Waiting: 38

这个状态信息提供了关于 Nginx 当前连接状态的快照,包括活跃连接数、处理的连接数、请求数等关键性能指标。