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

nginx运行状态怎么查看

Nginx 配置 状态 端口 985    来源:    2025-04-07

要查看 Nginx 的运行状态,可以通过以下几种方法:


1. 检查 Nginx 进程状态

ps aux | grep nginx
  • 如果有 nginx: master processnginx: worker process 进程,说明 Nginx 正在运行。

2. 检查 Nginx 服务状态(Systemd 系统)

systemctl status nginx
  • 输出会显示服务是否 active (running),以及最近的日志片段。

3. 检查监听端口

Nginx 默认监听 80(HTTP)或 443(HTTPS)端口:

ss -tulnp | grep nginx
# 或
netstat -tulnp | grep nginx
  • 如果看到 0.0.0.0:80:::80,说明 Nginx 正在运行。

4. 测试 Nginx 配置并重新加载

nginx -t          # 测试配置文件语法
nginx -s reload   # 重新加载配置(不重启服务)
  • 如果配置有误,会显示错误信息。

5. 查看 Nginx 错误日志

日志路径通常为 /var/log/nginx/error.log

tail -f /var/log/nginx/error.log

6. 使用 curl 测试 HTTP 响应

curl -I http://localhost
  • 如果返回 HTTP/1.1 200 OK,说明服务正常。

7. 启用 Nginx 状态模块(需配置)

在 Nginx 配置中添加以下内容(通常在 server 块内):

location /nginx_status {
    stub_status on;
    allow 127.0.0.1;   # 仅允许本地访问
    deny all;
}

重启 Nginx 后访问 http://localhost/nginx_status,输出类似:

Active connections: 3
server accepts handled requests
 100 100 200
Reading: 0 Writing: 1 Waiting: 2

常见问题排查

  • Nginx 未启动:运行 nginxsystemctl start nginx
  • 端口冲突:检查是否有其他服务占用 80/443 端口(如 Apache)。
  • 权限问题:确保 Nginx 用户(如 www-datanginx)有权限访问相关文件。

根据实际需求选择合适的方法即可快速确认 Nginx 状态。