温馨提示×

linux下如何定位nginx故障

小樊
51
2026-09-20 21:54:30
栏目: 智能运维

在 Linux 下定位 Nginx 故障,通常可以按「由外到内、由浅入深」的思路来排查。下面是一套比较系统的排错流程。


一、先确认 Nginx 是否在运行

1. 查看进程

ps -ef | grep nginx
# 或
systemctl status nginx

2. 查看端口监听

ss -lntp | grep 80
# 或
netstat -lntp | grep nginx

常见现象:

  • 没进程 → Nginx 没启动或启动失败
  • 端口没监听 → 配置错误或权限问题

二、查看 Nginx 错误日志(最重要)

默认路径

/var/log/nginx/error.log

实时查看:

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

常见错误:

  • permission denied → 权限问题
  • address already in use → 端口被占用
  • unknown directive → 配置语法错误
  • File not found → root / index 配置错误

三、检查 Nginx 配置是否正确

nginx -t

如果有错误,会直接指出 文件和行号。

重载配置(不中断服务):

nginx -s reload

四、确认访问是否到达 Nginx

1. 本地 curl 测试

curl -I http://127.0.0.1
curl -I http://localhost
  • 能访问 → Nginx 正常
  • 不能访问 → 防火墙 / 配置问题

2. 查看 access.log

tail -f /var/log/nginx/access.log
  • 有日志 → 请求已到 Nginx
  • 无日志 → 网络 / 防火墙 / 端口问题

五、网络与防火墙排查

1. 防火墙

iptables -L -n
# 或
firewall-cmd --list-all

开放端口:

firewall-cmd --add-port=80/tcp --permanent
firewall-cmd --reload

2. SELinux(CentOS / RHEL)

getenforce

如果为 Enforcing,可临时关闭测试:

setenforce 0

六、后端问题(反向代理 / PHP / API)

1. 反向代理失败

错误日志常见:

connect() failed (111: Connection refused)

检查:

  • 后端服务是否启动
  • proxy_pass 地址是否正确
  • 端口是否通

2. PHP 不解析

  • 检查 fastcgi_pass
  • 检查 php-fpm 是否运行
systemctl status php-fpm

七、性能 / 高并发问题

1. 连接数打满

ss -ant | wc -l

2. worker 配置

worker_processes auto;
worker_connections 10240;

3. 查看系统资源

top
free -h
df -h

八、常见故障速查表

现象 可能原因
502 后端服务挂了
503 限流 / 负载高
404 root / location 配置错
403 权限 / index 文件缺失
空白页 PHP 报错但未显示
启动失败 nginx -t 报错

九、一句话总结

先看 status,再看 error.log,然后 nginx -t,最后查网络和后端的链路。

如果你愿意,可以贴出:

  • nginx -t 输出
  • error.log 报错
  • 你的 nginx.conf 或 server 配置

我可以帮你精准定位问题。

0 踩