How to Fix Nginx 404 Not Found on Debian 11


The Root Cause The “Nginx 404 Not Found” error on Debian 11 commonly indicates that Nginx is unable to locate the requested file within its configured webroot. This usually stems from incorrect file or directory permissions for the www-data user (the user Nginx runs as on Debian) or an inaccurate root directive in the Nginx server block pointing to a non-existent or inaccessible path.

Quick Fix (CLI) These commands correct common permission issues and restart Nginx. Adjust /var/www/html if your webroot is different.

# Set ownership of the webroot to the Nginx user (www-data)
sudo chown -R www-data:www-data /var/www/html

# Set directory permissions to 755 (read, write, execute for owner; read, execute for group and others)
sudo find /var/www/html -type d -exec chmod 755 {} \;

# Set file permissions to 644 (read, write for owner; read for group and others)
sudo find /var/www/html -type f -exec chmod 644 {} \;

# Restart Nginx to apply changes
sudo systemctl restart nginx

Configuration Check Examine your site’s Nginx configuration file, typically located at /etc/nginx/sites-available/your_site.conf (or the default /etc/nginx/sites-available/default). Ensure the root directive points to the correct absolute path of your web application’s files, and the index directive includes your default file (e.g., index.html, index.php).

# File: /etc/nginx/sites-available/your_site.conf

server {
    listen 80;
    server_name your_domain.com www.your_domain.com; # Verify this matches your domain or IP

    # CRITICAL: Ensure this 'root' directive points to the actual, existing directory
    # containing your website files (e.g., /var/www/your_website_folder)
    root /var/www/html; # <-- VERIFY THIS PATH

    # Ensure 'index' includes the name of your primary default file
    index index.html index.htm index.nginx-debian.html; # <-- VERIFY THIS LIST

    location / {
        # This directive instructs Nginx to try finding the requested URI,
        # then the URI as a directory, and if neither is found, return a 404.
        # Ensure it's not redirecting incorrectly or missing.
        try_files $uri $uri/ =404;
    }

    # ... other configurations
}

After modifying the configuration file:

  1. If you created a new site config, ensure it’s symlinked: sudo ln -s /etc/nginx/sites-available/your_site.conf /etc/nginx/sites-enabled/
  2. If you no longer need the default site, remove its symlink: sudo rm /etc/nginx/sites-enabled/default

Verification First, test your Nginx configuration syntax. If it passes, reload Nginx, then use curl to check for a successful response.

# Test Nginx configuration for syntax errors
sudo nginx -t

# If the configuration test passes, reload Nginx to apply changes
sudo systemctl reload nginx

# Check for a 200 OK status from the server
# Replace 'localhost' with your domain if checking externally
curl -I http://localhost/

# Alternatively, request a specific known file to verify content delivery
curl http://localhost/index.html