How to Fix Nginx Error Log for 404 Errors
In this tutorial, you'll learn about How to Fix Nginx Error Log for 404 Errors. We cover key concepts, practical examples, and best practices.
Nginx returns 404 Not Found and the error log shows open() "/var/www/html/file.html" failed (2: No such file or directory) — the root directive points to the wrong directory or the file does not exist.
The Problem
2026/06/24 10:00:00 [error] 1234#0: *1 open() "/etc/nginx/html/style.css"
failed (2: No such file or directory), client: 10.0.0.1,
server: example.com, request: "GET /style.css"
Step-by-Step Fix
Step 1: Check the root directive
server {
root /var/www/example.com; # Must exist
index index.html;
}
Step 2: Verify the file exists
ls -la /var/www/example.com/style.css
Step 3: Use absolute paths in error log
error_log /var/log/nginx/error.log notice;
Step 4: Check location block root overrides
server {
root /var/www/example.com;
location /admin {
root /var/www/admin; # Overrides server root for /admin/*
}
}
Step 5: Test with try_files
location / {
root /var/www/example.com;
try_files $uri $uri/ /index.html;
}
Step 6: Add debug to error log
error_log /var/log/nginx/error.log debug;
# Search for specific errors
grep "open()" /var/log/nginx/error.log
Prevention Tips
- Always validate that root directories exist before starting Nginx
- Use
try_filesto provide fallback routing - Set
error_logtonoticeorinfoto capture all file lookup attempts - Test static file serving with
curl -Ibefore deploying
Common Mistakes with error log 404
- Using
returnto exit a function early instead of wrapping a pure value in the monad - Mixing let bindings with <- bindings in do notation, producing type errors
- Overlapping type class instances that cause GHC to reject the program with ambiguous dispatch errors
These mistakes appear frequently in real-world NGINX code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.
Practice Exercise
Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.
This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.
FAQ
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro