How to Fix Nginx 404 Not Found on AWS EC2
The Root Cause
On AWS EC2, the “Nginx 404 Not Found” error is frequently caused by incorrect file permissions or ownership for the Nginx worker process, preventing it from reading the web root directory and its contents. This often occurs when content is deployed by a different user (e.g., ec2-user) than the Nginx user (e.g., www-data or nginx), or when the root directive points to a non-existent or inaccessible path.
Quick Fix (CLI)
These commands address common permission and ownership issues. Adjust the Nginx user and web root path (/var/www/html) to match your specific EC2 instance configuration (e.g., nginx user for RHEL/CentOS, different web root).
# 1. Identify your Nginx user and web root
# Nginx user is typically 'www-data' (Debian/Ubuntu) or 'nginx' (RHEL/CentOS).
# The web root is defined by the 'root' directive in your Nginx config (e.g., /var/www/html).
# You can often find the user in /etc/nginx/nginx.conf (first line: "user www-data;")
# 2. Set correct ownership for the web root (e.g., /var/www/html)
# This command makes the Nginx user the owner of the web content.
# Replace 'www-data' with your Nginx user and '/var/www/html' with your web root.
sudo chown -R www-data:www-data /var/www/html
# 3. Set correct permissions for directories and files
# Directories need '755' (read/write/execute for owner, read/execute for group/others to traverse).
# Files need '644' (read/write for owner, read for group/others).
sudo find /var/www/html -type d -exec chmod 755 {} +
sudo find /var/www/html -type f -exec chmod 644 {} +
# 4. Restart Nginx to apply changes
sudo systemctl restart nginx
Configuration Check
The primary configuration files to inspect are /etc/nginx/nginx.conf and any server block configurations located in /etc/nginx/sites-available/ (symlinked to sites-enabled/) or /etc/nginx/conf.d/.
Look for the server block handling your domain or IP, and verify the root and index directives. Ensure the root path exactly matches the location of your website files on the EC2 instance, and that your main index file (e.g., index.html) is listed in the index directive.
# Example: /etc/nginx/sites-available/your_site.conf (or similar)
server {
listen 80;
server_name your_domain.com your_ec2_public_ip;
# IMPORTANT: Ensure this 'root' path is correct and exists on your EC2 instance.
# It must point to the directory containing your website's files.
root /var/www/html;
# Verify your main index file (e.g., index.html) is listed here.
index index.html index.htm index.nginx-debian.html;
location / {
# This standard 'try_files' directive is usually correct for 404 handling.
try_files $uri $uri/ =404;
}
# ... other directives
}
Verification
After applying the quick fix and checking configurations, use these commands to verify the issue is resolved.
# 1. Test Nginx configuration syntax for errors
# This must return "syntax is ok" and "test is successful".
sudo nginx -t
# 2. If Nginx syntax is OK, ensure Nginx is restarted
sudo systemctl restart nginx
# 3. Use 'curl' to check the web server from the command line
# Replace 'your_ec2_public_ip_or_dns' with your EC2 instance's public IP address or DNS name.
curl -I http://your_ec2_public_ip_or_dns/
# Expected output for a successful request: HTTP/1.1 200 OK (or 3xx redirect if configured)
# 4. To see the actual content of your index page:
curl http://your_ec2_public_ip_or_dns/
# Expected output: The HTML content of your website's index file.