Nginx 在 CentOS 上的系统化故障排查指南
一 快速定位思路
二 服务与端口检查
ps aux | grep nginxss -lntp | grep -E '80|443'(或 netstat -nltp | grep -E '80|443')systemctl start nginxnginx -tbind() to 0.0.0.0:80 failed (98: Address already in use)
ss -lntp | grep :80 找到 PID,kill -9 <PID> 后 systemctl start nginxnginx: [error] invalid PID number "" in "/run/nginx.pid"
nginx -c /etc/nginx/nginx.conf 再 nginx -s reloadbind() to 0.0.0.0:80 failed (13: Permission denied)
三 访问异常与日志分析
/var/log/nginx/access.log/var/log/nginx/error.logtail -f /var/log/nginx/error.logstat /var/www/html/、ls -l /var/www/html//etc/nginx/nginx.conf、/etc/nginx/vhosts/*.conf 中是否存在 deny;index 是否配置且文件存在root 指向的站点目录是否正确、域名绑定是否匹配、首页文件是否存在error.log 的错误行与时间点,结合 access.log 的 URL、时间、返回码进行关联分析tcpdump)、优化 Nginx 超时 与 后端性能,必要时协调客户端重试策略四 防火墙与 SELinux 策略
firewall-cmd --zone=public --add-port=80/tcp --permanent && firewall-cmd --reloadiptables -I INPUT -p tcp --dport 80 -j ACCEPTgetenforce(返回 Enforcing/Permissive/Disabled)setenforce 0(仅测试用)/etc/selinux/config 将 SELINUX=enforcing 改为 SELINUX=permissive 或 disabled 并重启semanage port -a -t http_port_t -p tcp <端口>(需安装 policycoreutils-python-utils)chcon -Rt httpd_sys_content_t /path/to/dirausearch -m avc -ts recent | grep nginx;按需生成策略模块:audit2allow -M mynginx && semodule -i mynginx.pp五 性能与资源类问题
accept() failed (24: Too many open files)ulimit -n 查看进程可用句柄;检查 worker_rlimit_nofile 与系统 fs.file-maxworker_connections