在 Linux 上用 Nginx 出现 404 Not Found,通常不是 Nginx 本身坏了,而是请求的资源没匹配到。下面按最常见原因 → 排查方法给你梳理。
例如访问:
http://域名/index.html
但 Nginx 配置中:
root /var/www/html;
而 /var/www/html/index.html 不存在
✅ 检查:
ls -l /var/www/html
location /static/ {
root /data/static/;
}
访问 /static/a.png
→ 实际找 /data/static/static/a.png
✅ 正确(用 alias):
location /static/ {
alias /data/static/;
}
例如:
location /api/ {
proxy_pass http://127.0.0.1:8080/;
}
但访问的是:
/api
(少了结尾斜杠)
✅ 建议:
location /api {
proxy_pass http://127.0.0.1:8080/;
}
server {
root /var/www/html;
# 没写 index
}
✅ 应该:
index index.html index.php;
proxy_pass http://127.0.0.1:9000;
检查:
curl http://127.0.0.1:9000
看错误页来源:
curl -I http://域名/xxx
如果是:
Server: nginx
✅ Nginx 的问题
如果是:
Server: Apache / gunicorn / tomcat
✅ 后端的问题
ps aux | grep nginx
通常是 www-data 或 nginx
修复:
chown -R www-data:www-data /var/www/html
chmod -R 755 /var/www/html
nginx -t
systemctl reload nginx
# 1. 测试配置
nginx -t
# 2. 查看错误日志
tail -f /var/log/nginx/error.log
# 3. 查看访问日志
tail -f /var/log/nginx/access.log
# 4. 本地测试
curl http://127.0.0.1
你可以贴出:
error.log 相关报错我可以直接告诉你哪一行配置导致 404。