How to Fix Nginx 404 Not Found on DigitalOcean Droplet
The Root Cause
Nginx 404 Not Found on a DigitalOcean Droplet commonly occurs when the web server cannot locate the requested files within its configured root directory. This typically stems from an incorrect root path specified in the Nginx server block or inadequate file system permissions preventing Nginx from accessing the intended document root.
Quick Fix (CLI)
# Ensure correct ownership for the web root (common for Ubuntu/Debian)
sudo chown -R www-data:www-data /var/www/html
# Ensure correct permissions for the web root
sudo chmod -R 755 /var/www/html
# Create a basic index.html if it's missing or to test the root directory
# This will overwrite any existing index.html in this path
echo "<!DOCTYPE html><html><head><title>WebToolsWiz</title></head><body><h1>Nginx is operational on DigitalOcean!</h1><p>If you see this, Nginx is correctly serving files from /var/www/html.</p></body></html>" | sudo tee /var/www/html/index.html
# Test Nginx configuration for syntax errors
sudo nginx -t
# Restart Nginx to apply changes
sudo systemctl restart nginx
Configuration Check
File to edit: /etc/nginx/sites-available/default (or your specific site’s configuration file, e.g., /etc/nginx/sites-available/your_domain.conf). Ensure this file is symlinked to /etc/nginx/sites-enabled/.
Lines to verify/change within the server block:
server {
listen 80 default_server;
listen [::]:80 default_server;
# Ensure this path points to your application's public directory.
# This is the most critical directive for 404 errors.
root /var/www/html; # <-- IMPORTANT: Adjust this to your actual web root path
# List the index files Nginx should look for when a directory is requested.
index index.html index.htm index.nginx-debian.html;
# Basic location block for serving static files.
# The 'try_files' directive attempts to serve the URI as a file, then as a directory,
# and finally returns a 404 if neither is found.
location / {
try_files $uri $uri/ =404;
}
# Add or verify any other necessary configurations,
# e.g., for PHP processing or specific application routes.
# For PHP-FPM, you might have:
# location ~ \.php$ {
# include snippets/fastcgi-php.conf;
# fastcgi_pass unix:/var/run/php/php8.1-fpm.sock; # Adjust PHP version
# }
# ... other directives ...
}
After editing:
sudo nginx -t
sudo systemctl reload nginx
Verification
# From within the DigitalOcean Droplet, verify Nginx can serve content locally
curl -I http://localhost/
# Expected output will include 'HTTP/1.1 200 OK' and 'Content-Type: text/html'
# If you created the test index.html, you can view its content:
curl http://localhost/
# Expected body content: <h1>Nginx is operational on DigitalOcean!</h1>...
# From your local machine, open a web browser and navigate to:
# http://YOUR_DROPLET_IP_ADDRESS/
# You should see the content served by Nginx.