让我们逐步排查 Nginx 500 内部错误的可能原因:
- 查看错误日志
bash
# 默认错误日志位置
tail -f /var/log/nginx/error.log
# 或自定义错误日志位置
tail -f /usr/local/nginx/logs/error.log
- 常见原因和解决方案:
- PHP-FPM 配置问题
nginx
# 检查 php-fpm 是否运行
systemctl status php-fpm
# 检查 nginx 配置中的 fastcgi_pass 是否正确
location ~ \.php$ {
fastcgi_pass unix:/run/php/php7.4-fpm.sock; # 确认socket路径正确
# 或
fastcgi_pass 127.0.0.1:9000; # 确认端口正确
}
- 权限问题
bash
# 检查目录权限
chmod 755 /var/www/html
chown -R www-data:www-data /var/www/html
# 检查日志文件权限
chmod 755 /var/log/nginx
- 反代配置问题
nginx
# 检查上游服务器配置
upstream backend {
server backend.example.com:8080;
}
location / {
proxy_pass http://backend;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
- SELinux 问题
bash
# 临时关闭 SELinux 测试
setenforce 0
# 查看 SELinux 状态
getenforce
能否提供错误日志内容?这样可以更准确地定位问题。