在 Debian 上部署 Nginx 时出现 404 Not Found,通常不是 Nginx “坏掉了”,而是请求没有匹配到正确的资源或配置。下面按最常见原因 → 排查方法说明。
例如你访问:
http://ip/
但:
/var/www/html/index.html 不存在
✅ 检查:
ls -l /var/www/html
✅ 解决方法:
echo "hello" > /var/www/html/index.html
示例错误配置:
server {
root /var/www; # 实际文件在 /var/www/html
}
✅ 检查配置文件:
nginx -T
✅ 正确示例:
server {
listen 80;
server_name _;
root /var/www/html;
index index.html index.php;
}
修改后:
nginx -t
systemctl reload nginx
如果你访问:
http://example.com
但配置是:
server_name www.example.com;
✅ Nginx 会走到 默认 server,可能返回 404
✅ 检查:
nginx -T | grep server_name
✅ 建议测试用:
server_name _;
例如:
/about
但目录里没有 /about 或 about.html
✅ 检查:
ls /var/www/html
index.php 不存在访问:
/index.php
但:
index.php✅ 检查:
ls /var/www/html/index.php
✅ 安装 PHP:
apt install php-fpm
✅ Nginx 需有:
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php-fpm.sock;
}
例如 Laravel 必须:
location / {
try_files $uri $uri/ /index.php?$query_string;
}
否则:
/login → 404/Assets/logo.png ❌
/assets/logo.png ✅
# 1. 看错误日志
tail -n 50 /var/log/nginx/error.log
# 2. 看访问日志
tail -n 20 /var/log/nginx/access.log
# 3. 测试配置
nginx -t
# 4. 重载
systemctl reload nginx
你可以直接贴:
我可以一步一步帮你改到不再 404 ✅