Nginx返回404错误表示服务器无法找到请求的资源。以下是常见原因和解决方法:
root
或alias
指令配置的路径不正确nginx
server {
listen 80;
server_name example.com;
root /var/www/html; # 确保此路径存在且包含请求的文件
index index.html;
}
root
指向的目录存在ls -l /var/www/html
nginx
location / {
index index.html index.htm index.php;
}
try_files
或rewrite
规则配置不当nginx
location / {
try_files $uri $uri/ /index.php?$args;
}
nginx
server {
listen 80;
server_name example.com www.example.com; # 确保域名匹配
...
}
bash
chown -R nginx:nginx /var/www/html
chmod -R 755 /var/www/html
检查Nginx错误日志:
tail -f /var/log/nginx/error.log
验证配置:
nginx -t
检查实际访问路径:
# 查看Nginx如何处理请求
curl -v http://example.com/path
检查文件是否存在:
ls -la /var/www/html/path/to/file
临时启用详细日志:
location / {
error_log /var/log/nginx/debug.log debug;
...
}
使用strace跟踪:
strace -p $(pgrep nginx | head -1)
检查符号链接:确保路径中的符号链接正确
检查SELinux(如果启用):
getenforce
setenforce 0 # 临时禁用测试
如果以上方法都不能解决问题,可能需要检查应用程序代码或框架的路由配置。