Nginx 日志里出现 404(Not Found),通常表示:客户端请求的资源在服务器上找不到。原因可以从「请求本身」和「Nginx 配置」两方面来看。
示例:
location /static/ {
root /var/www/html;
}
请求 /static/a.png
→ 实际找的是 /var/www/html/static/a.png
如果文件在 /var/www/html/a.png,就会 404。
root 和 alias 用错(非常常见)location /img/ {
root /var/www/img;
}
请求 /img/1.jpg
→ /var/www/img/img/1.jpg(多了一层)
location /img/ {
alias /var/www/img/;
}
访问目录但没指定文件:
location / {
root /var/www/html;
}
如果目录里没有 index.html,会 404(或 403,取决于配置)
解决:
index index.html index.php;
Vue / React 路由刷新 404:
location / {
try_files $uri $uri/ /index.html;
}
fastcgi_pass 配置错误SCRIPT_FILENAME 指向错误示例(PHP):
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
后端服务本身返回 404,Nginx 只是“转发”:
location /api/ {
proxy_pass http://127.0.0.1:8080/;
}
要看 后端日志,不是 Nginx 的问题。
请求没命中正确的 server 块,走了默认配置。
tail -f /var/log/nginx/access.log
tail -f /var/log/nginx/error.log
关注:
$request_filename(实际文件路径)error_log /var/log/nginx/error.log debug;
nginx -t
Nginx 404 = 请求的资源在「当前配置指向的位置」不存在
如果你愿意,可以把:
location 配置发出来,我可以直接帮你定位是哪一条原因。