在 Linux 下定位 Nginx 故障,通常可以按「由外到内、由浅入深」的思路来排查。下面是一套比较系统的排错流程。
ps -ef | grep nginx
# 或
systemctl status nginx
ss -lntp | grep 80
# 或
netstat -lntp | grep 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 -t
如果有错误,会直接指出 文件和行号。
重载配置(不中断服务):
nginx -s reload
curl -I http://127.0.0.1
curl -I http://localhost
tail -f /var/log/nginx/access.log
iptables -L -n
# 或
firewall-cmd --list-all
开放端口:
firewall-cmd --add-port=80/tcp --permanent
firewall-cmd --reload
getenforce
如果为 Enforcing,可临时关闭测试:
setenforce 0
错误日志常见:
connect() failed (111: Connection refused)
检查:
fastcgi_passsystemctl status php-fpm
ss -ant | wc -l
worker_processes auto;
worker_connections 10240;
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 配置我可以帮你精准定位问题。