Nginx 日志中出现 404(Not Found) 通常表示请求的资源在服务器上不存在或不可访问。排查和解决可以按下面步骤一步一步来。
查看 Nginx 错误日志和访问日志:
tail -f /var/log/nginx/access.log
tail -f /var/log/nginx/error.log
重点看:
现象
GET /images/logo.png HTTP/1.1 404
解决
ls -l /var/www/html/images/logo.png
location /static/ {
root /var/www/html/static;
}
访问 /static/a.png → 实际找 /var/www/html/static/static/a.png
location /static/ {
alias /var/www/html/static/;
}
或
location / {
root /var/www/html;
}
现象 访问目录返回 404
解决
index index.html index.php;
示例:
server {
listen 80;
root /var/www/html;
index index.html;
}
现象
GET /api/user HTTP/1.1 404
解决 确认是否有对应 location:
location /api/ {
proxy_pass http://127.0.0.1:8080;
}
现象 Nginx 本身没文件,但转发给后端还是 404
解决
proxy_pass 路径是否正确location /api/ {
proxy_pass http://127.0.0.1:8080/;
}
注意结尾 / 的区别
chown -R www-data:www-data /var/www/html
chmod -R 755 /var/www/html
常见于:
示例(WordPress):
location / {
try_files $uri $uri/ /index.php?$args;
}
你可以把下面信息发给我:
我可以直接帮你指出是哪一行配置导致的 404。