Skip to content

How to Fix Nginx Error Log for 404 Errors

DodaTech Updated 2026-06-24 2 min read

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_files to provide fallback routing
  • Set error_log to notice or info to capture all file lookup attempts
  • Test static file serving with curl -I before deploying

Common Mistakes with error log 404

  1. Using return to exit a function early instead of wrapping a pure value in the monad
  2. Mixing let bindings with <- bindings in do notation, producing type errors
  3. 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

### How do I find which root Nginx is using for a specific URL?

Enable debug logging: error_log /var/log/nginx/error.log debug;. The log shows every open() call with the full path Nginx is trying. The path is constructed as root + URI. For example, if root is /var/www and URI is /about.html, Nginx looks for /var/www/about.html.

Why does Nginx return 404 when the file exists?

Check file permissions: Nginx workers run as the www-data or nginx user and need read and execute permissions on the directory. Also check that the root path in the server block is not overridden by a nested location block without your knowledge.

What is the difference between root and alias in Nginx?

root prepends the path to the full URI: root /var/www; + URI /css/style.css = /var/www/css/style.css. alias replaces the location prefix: location /css { alias /var/www/styles; } + URI /css/style.css = /var/www/styles/style.css.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro