centos

nginx日志中常见的CentOS错误及解决

小樊
45
2025-12-07 18:11:19
栏目: 智能运维

Nginx 在 CentOS 的常见错误与排查

一 日志定位与常用命令

二 常见错误对照表

日志关键词或现象 含义 快速排查 解决方案
“bind() to 0.0.0.0:80 failed (98: Address already in use)” 端口被占用 ss -lntp 结束占用进程或调整端口;必要时 killall -9 nginx 后重启
“bind() to 0.0.0.0:80 failed (13: Permission denied)” 权限被拒(常见于 SELinux 或端口策略) getenforce;ausearch -m avc -ts recent 临时 setenforce 0 验证;永久方案用 SELinux 布尔值或策略放行
“connect() to 127.0.0.1:8080 failed (13: Permission denied) while connecting to upstream” 反向代理到本地端口被 SELinux 拦截 getsebool httpd_can_network_connect setsebool -P httpd_can_network_connect 1
“invalid PID number “” in “/run/nginx.pid”” PID 文件无效或未生成 systemctl status nginx;cat /run/nginx.pid nginx -c /etc/nginx/nginx.conf 后再 reload;必要时创建 override 文件延迟写入 PID
“could not open error log file … failed (13: Permission denied)” 日志目录不可写 ls -ld /var/log/nginx chown -R nginx:nginx /var/log/nginx;必要时 chmod 755
“open() …/access.log failed (2: No such file or directory)” 日志目录不存在 ls /var/log/nginx mkdir -p /var/log/nginx && chown nginx:nginx /var/log/nginx
访问返回 403 Forbidden 目录无索引或权限不足 检查 root/index;ls -ld 目录 配置 index;修正目录属主/权限;必要时调整 nginx.conf 的 user
访问返回 404 且为前端路由(SPA) 刷新走后端找不到资源 查看 location 配置 在 location / 中加入 try_files $uri $uri/ /index.html;
访问返回 502 Bad Gateway 上游无响应/超时/被拒 curl 上游地址;查看上游日志 修正 upstream 地址/端口;排查上游进程与防火墙;SELinux 放行网络
日志出现 499 客户端提前关闭连接 前端超时设置过短 优化后端耗时;适当增大客户端超时;必要时优化慢查询/缓存

三 关键场景的修复示例

四 预防与优化建议

0
看了该问题的人还看了