nginx-404

Nginx 404 Not Found 的定位与修复

现象

请求返回 404 Not Found。可能是文件确实不存在,也可能是 root/alias、location 或 try_files 配置导致路径解析错误。

快速定位

tail -f /var/log/nginx/error.log
nginx -T | grep -nE 'root|alias|try_files|location|rewrite'
ls -l /var/www/html/path
curl -v http://127.0.0.1/path

常见根因

  1. root 或 alias 指向了错误目录;alias 的尾斜杠语义尤其容易错。
  2. try_files 配置不匹配,回退到了不存在的文件。
  3. location 优先级和正则顺序导致请求进了错误的块。
  4. 前端路由(SPA)没有回退到 index.html。
  5. 文件大小写、尾斜杠或 URL 重写不一致。
  6. 反向代理的 proxy_pass 路径拼接错误,后端返回 404。

修复

相关命令