直接查 /var/log/nginx/access.log 中状态码为404的日志行可快速定位URL、客户端IP及时间;再结合error.log分析缺失原因,并用nginx -T和ls -l验证路径与权限。

直接查 /var/log/nginx/access.log 里状态码为 404 的日志行,就能快速知道是哪个 URL、哪个客户端 IP、什么时间触发的 404。
从 access.log 快速筛选 404 请求
每条日志包含客户端 IP、请求时间、HTTP 方法、URI、状态码等字段。404 一定出现在第 9 个字段(默认日志格式下)为 404 的记录中。
- 实时监控:运行
tail -f /var/log/nginx/access.log | grep " 404 "(注意前后空格,避免匹配到 4040 或 2404) - 查最近 20 条:运行
grep " 404 " /var/log/nginx/access.log | tail -20 - 按时间段查:例如查今天上午 8–11 点的 404,可用
awk '$4 ~ /\[13\/Oct\/2026:0[8-11]/ && $9 == "404"' /var/log/nginx/access.log
结合 error.log 分析缺失原因
access.log 告诉你“谁访问了什么并返回了 404”,error.log 才解释“为什么找不到”。打开 /var/log/nginx/error.log,常见线索有:
-
open() "/var/www/html/css/main.css" failed (2: No such file or directory)→ 文件确实不存在 -
directory index of "/var/www/html/" is forbidden→ 目录没配index,且未开启autoindex -
Permission denied→ 路径存在,但 Nginx 进程无读取权限
反向验证:把日志 URI 拼成真实路径
比如 access.log 中有一行:
192.168.1.100 - - [13/Oct/2026:07:15:22 +0000] "GET /js/app.js HTTP/1.1" 404 153 "-" "Mozilla/5.0"
说明请求的是 /js/app.js,结合配置中的 root(如 root /var/www/html;),完整路径就是 /var/www/html/js/app.js。用 ls -l /var/www/html/js/app.js 查看是否存在、权限是否可读。
检查配置是否支持该路径
运行 nginx -T 输出全部生效配置,确认:
- 当前请求的 location 是否被正确匹配
-
root或alias指令是否指向预期目录 - 如果是 history 路由(如 Vue/React SPA),是否漏了
try_files $uri $uri/ /index.html;


















