Nginx故障排查 #

一、故障排查概述 #

1.1 排查思路 #

  1. 确认问题现象
  2. 查看错误日志
  3. 检查配置文件
  4. 分析系统资源
  5. 网络连通性测试
  6. 逐步定位问题

1.2 常用工具 #

工具 用途
nginx -t 测试配置
tail -f 查看日志
curl 测试请求
netstat/ss 查看连接
lsof 查看文件
strace 跟踪系统调用
tcpdump 抓包分析

二、启动失败 #

2.1 端口被占用 #

错误信息:

text
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)

排查:

bash
netstat -tlnp | grep :80
lsof -i :80

解决:

bash
kill -9 <PID>

或修改Nginx端口:

nginx
listen 8080;

2.2 配置文件错误 #

错误信息:

text
nginx: [emerg] unknown directive "servername" in /etc/nginx/nginx.conf:35

排查:

bash
nginx -t

解决:

修正配置语法:

nginx
server_name example.com;

2.3 权限问题 #

错误信息:

text
nginx: [emerg] open() "/var/log/nginx/error.log" failed (13: Permission denied)

解决:

bash
chown -R nginx:nginx /var/log/nginx
chmod -R 755 /var/log/nginx

2.4 文件不存在 #

错误信息:

text
nginx: [emerg] open() "/etc/nginx/ssl/cert.pem" failed (2: No such file or directory)

解决:

检查文件路径和权限:

bash
ls -la /etc/nginx/ssl/

三、502 Bad Gateway #

3.1 后端服务未启动 #

排查:

bash
netstat -tlnp | grep 8080
ps aux | grep backend

解决:

启动后端服务。

3.2 后端服务超时 #

错误日志:

text
upstream timed out (110: Connection timed out) while reading response header from upstream

解决:

增加超时时间:

nginx
proxy_read_timeout 60s;
proxy_connect_timeout 60s;
proxy_send_timeout 60s;

3.3 后端服务拒绝连接 #

错误日志:

text
connect() failed (111: Connection refused) while connecting to upstream

排查:

bash
curl http://127.0.0.1:8080/health
telnet 127.0.0.1 8080

解决:

检查后端服务状态和防火墙配置。

3.4 upstream配置错误 #

排查:

nginx
upstream backend {
    server 127.0.0.1:8080;
}

确认IP和端口正确。

四、504 Gateway Timeout #

4.1 后端处理时间过长 #

解决:

增加超时时间:

nginx
proxy_read_timeout 300s;
proxy_connect_timeout 300s;
proxy_send_timeout 300s;

4.2 后端服务卡死 #

排查:

bash
ps aux | grep backend
top -p <PID>
strace -p <PID>

解决:

重启后端服务,排查代码问题。

4.3 网络问题 #

排查:

bash
ping backend-server
traceroute backend-server

五、403 Forbidden #

5.1 文件权限问题 #

排查:

bash
ls -la /var/www/html/
namei -l /var/www/html/index.html

解决:

bash
chmod -R 755 /var/www/html
chown -R nginx:nginx /var/www/html

5.2 SELinux限制 #

排查:

bash
getenforce
ausearch -m avc -ts recent

解决:

bash
setsebool -P httpd_read_user_content 1
chcon -R -t httpd_sys_content_t /var/www/html

5.3 目录索引禁用 #

错误信息:

text
directory index of "/var/www/html/" is forbidden

解决:

nginx
location / {
    autoindex on;
}

或添加默认索引文件:

nginx
index index.html index.htm;

5.4 IP访问限制 #

排查:

检查配置中的allow/deny指令。

解决:

nginx
location / {
    allow all;
}

六、性能问题 #

6.1 CPU使用率高 #

排查:

bash
top -p $(pgrep nginx)
pidstat -p $(pgrep nginx) 1

解决:

优化配置:

nginx
worker_processes auto;
worker_cpu_affinity auto;

6.2 内存使用高 #

排查:

bash
ps aux | grep nginx
pmap -x <PID>

解决:

调整缓冲区:

nginx
proxy_buffer_size 4k;
proxy_buffers 8 4k;

6.3 连接数过多 #

排查:

bash
ss -s
netstat -an | grep :80 | wc -l

解决:

增加连接数:

nginx
worker_connections 65535;

优化内核参数:

bash
sysctl -w net.core.somaxconn=65535

6.4 响应慢 #

排查:

bash
curl -w "time_total: %{time_total}\n" http://localhost/

解决:

开启缓存:

nginx
open_file_cache max=10000 inactive=30s;
proxy_cache my_cache;
gzip on;

七、SSL/TLS问题 #

7.1 证书过期 #

排查:

bash
openssl x509 -in /etc/nginx/ssl/cert.pem -noout -dates
echo | openssl s_client -connect example.com:443 2>/dev/null | openssl x509 -noout -dates

解决:

更新证书:

bash
certbot renew

7.2 证书链不完整 #

错误信息:

浏览器提示证书不受信任

解决:

使用完整证书链:

nginx
ssl_certificate /etc/nginx/ssl/fullchain.pem;

7.3 协议不兼容 #

解决:

调整SSL协议:

nginx
ssl_protocols TLSv1.2 TLSv1.3;

八、日志分析 #

8.1 查看错误日志 #

bash
tail -f /var/log/nginx/error.log
grep "error" /var/log/nginx/error.log
grep "emerg\|alert\|crit" /var/log/nginx/error.log

8.2 分析访问日志 #

请求量统计:

bash
wc -l /var/log/nginx/access.log

状态码统计:

bash
awk '{print $9}' /var/log/nginx/access.log | sort | uniq -c | sort -rn

Top 10 IP:

bash
awk '{print $1}' /var/log/nginx/access.log | sort | uniq -c | sort -rn | head -10

慢请求:

bash
awk '$NF > 1 {print $0}' /var/log/nginx/access.log

8.3 日志级别 #

调整日志级别获取更多信息:

nginx
error_log /var/log/nginx/error.log debug;

九、网络问题 #

9.1 DNS解析失败 #

错误信息:

text
no live upstreams while connecting to upstream

解决:

检查DNS配置:

nginx
resolver 8.8.8.8 valid=300s;

或使用IP地址:

nginx
upstream backend {
    server 192.168.1.10:8080;
}

9.2 防火墙阻止 #

排查:

bash
iptables -L -n
firewall-cmd --list-all

解决:

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

9.3 连接被重置 #

排查:

bash
tcpdump -i eth0 port 80 -w nginx.pcap

十、调试技巧 #

10.1 测试配置 #

bash
nginx -t
nginx -t -c /etc/nginx/nginx.conf

10.2 查看编译参数 #

bash
nginx -V

10.3 查看进程状态 #

bash
ps aux | grep nginx
pstree -p | grep nginx

10.4 查看连接状态 #

bash
ss -tlnp | grep nginx
netstat -tlnp | grep nginx

10.5 跟踪系统调用 #

bash
strace -p <PID> -f -e trace=network

10.6 抓包分析 #

bash
tcpdump -i eth0 port 80 -w nginx.pcap
wireshark nginx.pcap

10.7 使用debug日志 #

nginx
error_log /var/log/nginx/error.log debug;
rewrite_log on;

十一、常见错误速查 #

错误 原因 解决
bind() failed 端口被占用 杀掉占用进程或换端口
Permission denied 权限不足 修改文件权限
Connection refused 后端未启动 启动后端服务
Connection timed out 后端超时 增加超时时间
No such file 文件不存在 检查文件路径
Too many open files 文件描述符不足 增加ulimit
worker process exited Worker崩溃 查看错误日志

十二、预防措施 #

12.1 监控告警 #

  • 配置日志监控
  • 设置资源告警
  • 健康检查

12.2 定期维护 #

  • 日志轮转
  • 证书更新
  • 配置备份

12.3 压力测试 #

bash
ab -n 10000 -c 100 http://localhost/
wrk -t12 -c400 -d30s http://localhost/

12.4 文档记录 #

  • 配置变更记录
  • 故障处理记录
  • 架构文档

十三、总结 #

本章我们学习了:

  1. 故障排查思路:确认问题、查看日志、逐步定位
  2. 启动失败:端口占用、配置错误、权限问题
  3. 502错误:后端未启动、超时、连接拒绝
  4. 504错误:处理超时、服务卡死
  5. 403错误:权限、SELinux、目录索引
  6. 性能问题:CPU、内存、连接数、响应慢
  7. SSL问题:证书过期、证书链、协议
  8. 日志分析:错误日志、访问日志
  9. 网络问题:DNS、防火墙、连接重置
  10. 调试技巧:配置测试、系统调用、抓包

恭喜你完成了Nginx完全指南的学习!现在你已经掌握了从基础到高级的Nginx知识,可以胜任Nginx的配置、优化和运维工作。

最后更新:2026-03-27