插件窝 干货文章 nginx怎么查看运行状态

nginx怎么查看运行状态

Nginx 配置 状态 status 426    来源:    2025-04-07

在Nginx中查看运行状态可以通过以下几种方法实现:


1. 使用Nginx内置状态模块(需提前配置)

如果Nginx编译时包含ngx_http_stub_status_module模块(默认通常已包含),可以通过以下步骤启用状态页:

配置步骤:

  1. 修改Nginx配置文件(通常位于/etc/nginx/nginx.conf或站点配置文件中):

    server {
       listen 80;
       server_name localhost;
    
       location /nginx_status {
           stub_status on;
           access_log off;
           allow 127.0.0.1;   # 仅允许本地访问(按需改为允许的IP)
           deny all;
       }
    }
    
  2. 重新加载Nginx配置: bash sudo nginx -s reload

访问状态页:

通过浏览器或curl访问:

curl http://localhost/nginx_status

输出示例:

Active connections: 3 
server accepts handled requests
 100 100 200 
Reading: 0 Writing: 1 Waiting: 2 
  • Active connections: 当前活跃连接数。
  • accepts/handled/requests: 总接受的连接、成功处理的连接、总请求数。
  • Reading/Writing/Waiting: 处于不同状态的连接数。

2. 检查Nginx进程状态

通过系统命令查看Nginx是否运行:

# 查看进程
ps aux | grep nginx

# 检查服务状态(Systemd系统)
systemctl status nginx

# 检查端口监听
ss -tulnp | grep nginx
或
netstat -tulnp | grep nginx

3. 使用Nginx Plus或第三方模块

  • Nginx Plus:提供官方的状态仪表板(需商业授权)。
  • 第三方模块:如ngx_http_status_modulenginx-module-vts(需自行编译安装)。

4. 查看日志文件

  • 访问日志:默认路径通常为/var/log/nginx/access.log
  • 错误日志:默认路径通常为/var/log/nginx/error.log
tail -f /var/log/nginx/error.log  # 实时查看错误日志

5. 验证配置

检查配置是否正确:

sudo nginx -t

输出显示 syntax is oktest is successful 表示配置无误。


常见问题排查

  • 如果访问/nginx_status返回404
    • 确认配置已正确加载(nginx -s reload)。
    • 检查Nginx是否编译了stub_status模块(nginx -V 2>&1 | grep -o stub_status)。
  • 权限问题:确保配置中的allow规则允许你的IP访问。

通过以上方法,你可以全面掌握Nginx的运行状态和性能指标。