debian

Nginx错误日志常见问题及解决方法

小樊
36
2025-12-12 12:27:07
栏目: 编程语言

Nginx 错误日志常见问题与排查要点

一 快速定位与通用排查

二 常见错误模式与修复建议

错误日志关键词 含义与常见场景 快速修复建议
connect() failed (111: Connection refused) while connecting to upstream 上游未启动、端口未监听、服务崩溃或防火墙拒绝 启动上游;确认监听端口与进程;检查防火墙/安全组;用 telnet/netstat 验证连通性
upstream timed out (110: Connection timed out) 上游处理慢或卡死,Nginx 读/连超时 优化上游性能;适当增大proxy_connect_timeout / proxy_read_timeout / proxy_send_timeout
no live upstreams while connecting to upstream 所有上游被判定不可用(如并发失败触发快速摘除) 调整max_fails/fail_timeout;排查上游稳定性与健康检查;必要时增加更多后端
upstream prematurely closed connection while reading response header from upstream 上游提前关闭连接(常见于后端主动关闭或异常) 检查上游应用日志与异常退出;确认协议与连接复用配置一致
recv() failed (104: Connection reset by peer) 对端重置连接(进程崩溃、超时、网络异常) 排查上游崩溃/重启;抓包与后端日志联动分析;优化超时与重试
Permission denied while reading upstream / open() … failed (13: Permission denied) 文件/目录/进程权限不足或SELinux/AppArmor拦截 修正文件权限与属主;检查进程运行用户;必要时调整 SELinux 上下文或策略
client intended to send too large body / 413 Request Entity Too Large 请求体超过client_max_body_size 增大client_max_body_size(如 10M);若使用 PHP,需同步调整post_max_size / upload_max_filesize
upstream sent invalid header / no valid HTTP/1.0 header 上游返回非法或不符合 HTTP 的响应头 修正上游响应格式;在代理上启用proxy_http_version 1.1并完善头部转发
SSL_do_handshake() failed / SSL_write() failed HTTPS 配置或证书问题、SSL 库/模块缺失 确认编译时启用SSL 模块且证书链完整;检查listen 443 ssl与证书路径
bind() to [::]:80 failed (98: Address already in use) IPv4 与 IPv6 监听冲突或未设置ipv6only=on 将 IPv6 监听改为listen [::]:80 ipv6only=on 或仅保留一种监听
rewrite or internal redirection cycle 重写规则导致无限重定向 增加break/last或修正目标路径,避免循环
worker process exited on signal 11 (core dumped) 段错误或第三方模块异常 使用gdb分析 core 文件;临时禁用可疑模块并升级修复

三 配置示例与关键参数

location /api/ {
    proxy_pass http://127.0.0.1:3000/;
    proxy_http_version 1.1;
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_connect_timeout 15s;
    proxy_read_timeout 60s;
    proxy_send_timeout 60s;
}
http {
    client_max_body_size 10m;
}
# 若后端为 PHP,需同步:
# post_max_size = 10M
# upload_max_filesize = 10M(或按业务分拆)
location / {
    root /var/www/html;
    index index.html;
    try_files $uri $uri/ /index.html;  # SPA 前端路由支持
}
error_log /var/log/nginx/error.log warn;  # 生产建议 warn/error;排查可临时 debug
access_log /var/log/nginx/access.log main;
listen 80;
listen [::]:80 ipv6only=on;
upstream backend {
    server 10.0.0.11:8080 max_fails=3 fail_timeout=30s;
    server 10.0.0.12:8080 max_fails=3 fail_timeout=30s;
}

以上示例覆盖了超时、上传大小、代理协议、路由回退、日志与监听冲突等高频场景,可直接按需裁剪到你的 server 配置中。

四 高频场景的排查清单

0
看了该问题的人还看了