原因:Nginx作为反向代理时,无法连接后端服务(如PHP-FPM、Node.js),常见于后端服务未运行或代理配置错误。
解决方法:
sudo systemctl status php-fpm(以PHP-FPM为例),若未运行则启动:sudo systemctl start php-fpm。fastcgi_pass(PHP-FPM)或proxy_pass(其他后端)指向正确地址,例如:location ~ \.php$ {
fastcgi_pass unix:/run/php/php-fpm.sock; # 确认sock文件路径与后端一致
include fastcgi_params;
}
sudo systemctl restart php-fpm nginx。原因:请求的文件路径不存在,或Nginx配置中的root/alias路径错误。
解决方法:
server块中的root指令指向正确目录(如/var/www/html),并确保index指令包含默认首页(如index.html)。www-data),并设置合理权限:sudo chown -R www-data:www-data /var/www/html
sudo chmod -R 755 /var/www/html
index.html)。原因:目录权限不足,或Nginx配置中未启用index文件。
解决方法:
sudo chmod -R 755 /var/www/html(允许所有者读写、其他用户读执行)。index文件:在location /块中添加index指令,例如:location / {
index index.html index.php; # 确保包含默认首页
}
sudo setenforce 0),若问题解决需调整SELinux策略。原因:后端脚本(如PHP)执行错误、硬盘空间满、系统资源限制(如文件描述符数)。
解决方法:
sudo tail -n 50 /var/log/nginx/error.log,定位具体错误(如PHP语法错误)。df -lh,若根分区占用100%需清理文件(如/var/log下的旧日志)。ulimit -HSn 102400(仅当前shell有效)。/etc/security/limits.conf,添加:* soft nofile 65535
* hard nofile 65535
/etc/nginx/nginx.conf,在worker_processes下添加:worker_rlimit_nofile 65535;。原因:Nginx配置文件(如nginx.conf或站点配置)存在拼写错误、未知指令或格式问题。
解决方法:
sudo nginx -t,若提示错误(如unknown directive "unknowndirective"),根据错误信息定位并修改配置文件。include指令(如include /etc/nginx/conf.d/*.conf;),需逐一检查子配置文件的语法。原因:Nginx监听的端口(如80、443)被其他程序(如Apache、IIS)占用。
解决方法:
sudo lsof -i :80或sudo netstat -tulnp | grep :80,获取进程ID(PID)。sudo kill -9 <PID>(谨慎操作,确认进程无用后再终止)。/etc/nginx/sites-enabled/your-site.conf,修改listen指令(如listen 8080;),然后重启Nginx。原因:私钥与证书不匹配、证书过期或路径错误。
解决方法:
openssl x509 -noout -modulus -in /etc/letsencrypt/live/yourdomain.com/cert.pem | openssl md5
openssl rsa -noout -modulus -in /etc/letsencrypt/live/yourdomain.com/privkey.pem | openssl md5
若两个哈希值不一致,需重新生成证书(如certbot --nginx -d yourdomain.com)。ssl_certificate(证书)和ssl_certificate_key(私钥)路径正确。sudo tail -f /var/log/nginx/error.log(实时监控错误),或sudo less /var/log/nginx/error.log(查看历史记录)。sudo systemctl status nginx,若未运行则启动:sudo systemctl start nginx。sudo nginx -t,确认语法无误后再重启服务。