Nginx运行状态检查与故障排查指南

📅 2026/7/27 3:11:16
Nginx运行状态检查与故障排查指南
1. 为什么需要检查Nginx运行状态在Linux服务器运维工作中Nginx作为最常用的Web服务器和反向代理工具其运行状态的监控是日常维护的基础操作。当网站无法访问时快速确认Nginx是否正常运行往往是故障排查的第一步。根据我的运维经验以下场景特别需要检查Nginx状态服务器重启后需要确认服务是否自动恢复修改Nginx配置后测试新配置是否生效网站出现502/504等错误时判断问题源头服务器资源异常CPU/内存占用高时排查原因安全审计时需要确认服务运行情况提示养成定期检查关键服务状态的习惯可以提前发现潜在问题。我通常会在每天早上的例行检查中查看Nginx状态。2. 系统服务管理命令检查法2.1 systemctl方式主流Linux发行版现代Linux系统CentOS 7/Ubuntu 16.04普遍采用systemd作为初始化系统对应的服务管理命令是systemctl。这是目前最推荐的方式systemctl status nginx典型输出解析● nginx.service - A high performance web server and a reverse proxy server Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled) Active: active (running) since Tue 2023-05-16 09:23:45 UTC; 3 days ago Docs: man:nginx(8) Main PID: 1234 (nginx) Tasks: 2 (limit: 1152) Memory: 5.3M CGroup: /system.slice/nginx.service ├─1234 nginx: master process /usr/sbin/nginx -g daemon on; master_process on; └─1235 nginx: worker process关键状态说明Active: active (running)表示服务正在运行enabled表示开机自启已启用下方会显示主进程和子进程的PID2.2 service命令旧版系统兼容对于使用SysVinit的旧系统可以使用service nginx status输出示例nginx is running (pid 1234)2.3 init.d脚本方式某些特殊环境下可能需要直接调用init脚本/etc/init.d/nginx status3. 进程检查方法3.1 使用ps命令检查直接查看Nginx相关进程是最可靠的方式ps -ef | grep nginx健康状态下应该能看到至少两个进程root 1234 1 0 May16 ? 00:00:00 nginx: master process /usr/sbin/nginx www-data 1235 1234 0 May16 ? 00:00:12 nginx: worker process注意如果只看到master进程没有worker说明Nginx工作异常3.2 使用pgrep快速查找更简洁的进程查找方式pgrep -lf nginx输出示例1234 nginx: master process /usr/sbin/nginx 1235 nginx: worker process4. 端口监听检查4.1 netstat方式检查Nginx是否监听默认的80/443端口netstat -tulnp | grep nginx典型输出tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 1234/nginx: master tcp 0 0 0.0.0.0:443 0.0.0.0:* LISTEN 1234/nginx: master4.2 ss命令更现代的替代ss -tulnp | grep nginx4.3 lsof检查监听端口lsof -i :80,443 -S | grep nginx5. 日志检查法5.1 查看错误日志tail -20 /var/log/nginx/error.log正常运行时应该没有错误信息如果看到类似以下内容说明有问题2023/05/19 10:23:45 [emerg] 1234#1234: bind() to 0.0.0.0:80 failed (98: Address already in use)5.2 检查访问日志tail -f /var/log/nginx/access.log然后在另一个终端用curl访问观察是否有新日志产生curl -I http://localhost6. 网络请求测试6.1 使用curl测试curl -I http://localhost健康响应HTTP/1.1 200 OK Server: nginx/1.18.0 Date: Fri, 19 May 2023 10:30:00 GMT Content-Type: text/html Connection: keep-alive6.2 telnet测试端口连通性telnet localhost 80成功连接会显示Trying 127.0.0.1... Connected to localhost. Escape character is ^].7. 综合检查脚本我通常会将常用检查方法整合成一个脚本#!/bin/bash echo Systemd Status systemctl status nginx --no-pager | head -n 5 echo -e \n Running Processes pgrep -lf nginx echo -e \n Listening Ports ss -tulnp | grep nginx echo -e \n Recent Errors tail -n 5 /var/log/nginx/error.log echo -e \n HTTP Test curl -Is http://localhost | head -n 1保存为check_nginx.sh后赋予执行权限chmod x check_nginx.sh8. 常见问题排查8.1 端口被占用错误现象nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)解决方法sudo ss -tulnp | grep :80 sudo kill 占用进程的PID8.2 配置文件错误错误现象nginx: [emerg] invalid number of arguments in listen directive解决方法sudo nginx -t # 测试配置文件8.3 权限问题错误现象nginx: [alert] could not open error log file: open() /var/log/nginx/error.log failed (13: Permission denied)解决方法sudo chown -R www-data:www-data /var/log/nginx sudo chmod -R 755 /var/log/nginx9. 自动化监控方案对于生产环境建议配置监控系统9.1 使用systemd的监控功能sudo systemctl edit --full nginx.service添加以下段落[Service] Restarton-failure RestartSec5s StartLimitInterval60s9.2 配置Prometheus监控在Nginx中启用stub_status模块location /nginx_status { stub_status; allow 127.0.0.1; deny all; }然后在Prometheus配置中添加- job_name: nginx static_configs: - targets: [localhost:80]10. 性能检查进阶当Nginx运行但性能不佳时可以检查10.1 工作进程状态sudo kill -USR2 $(cat /var/run/nginx.pid) tail -f /var/log/nginx/error.log10.2 连接数统计netstat -an | grep :80 | wc -l10.3 使用nginx-module-vts编译安装vts模块后配置http { vhost_traffic_status_zone; server { location /status { vhost_traffic_status_display; access_log off; } } }访问/status可查看详细统计信息。