Nginx 日志中出现 404(Not Found) 通常表示客户端请求的资源在服务器上不存在或不可访问。下面按常见原因分类说明,并给出排查思路。
示例
location /images/ {
root /var/www/html;
}
请求:
/images/logo.png
实际文件应为:
/var/www/html/images/logo.png
如果文件不存在 → 404
✅ 排查:
ls -l /var/www/html/images/logo.png
location /static/ {
root /data/www;
}
请求 /static/a.txt
实际找的是:
/data/www/static/a.txt
location /static/ {
alias /data/www/;
}
请求 /static/a.txt
实际找的是:
/data/www/a.txt
❌ 常见错误:
/location / {
root /var/www/html;
index index.html;
}
如果:
/var/www/html/index.html
不存在 → 404
location /api/ {
proxy_pass http://127.0.0.1:8080;
}
此时 404 可能来自后端服务,不是 Nginx 本身。
✅ 检查:
/location / {
try_files $uri $uri/ =404;
}
如果:
→ 直接 404
即使文件存在,也可能 404:
✅ 检查:
ps aux | grep nginx
ls -ld /var/www/html
ls -l /var/www/html/index.html
location / {
try_files $uri /index.html;
}
如果写成:
try_files $uri =404;
→ 前端路由刷新就 404
nginx -t
nginx -s reload
GET /test.html HTTP/1.1 404
确认请求路径
open() "/var/www/html/test.html" failed (2: No such file or directory)
直接告诉你文件不存在
Nginx 404 = 请求的资源在指定路径下找不到
排查顺序建议:
如果你愿意,可以把 Nginx 配置 + 具体 404 的访问 URL + error.log 片段发出来,我可以帮你精准定位。