How to Fix Nginx 404 Not Found on a Fresh Install

Why Nginx 404 Not Found Happens on a Fresh Install

A 404 on a fresh Nginx install usually means Nginx runs fine but cannot find the file you asked for. The server answers, the connection works, and the path is wrong. That distinction is good news: a 404 is a configuration problem, not a broken server. This guide shows how to fix Nginx 404 not found on a fresh install, starting with the checks that solve most cases in minutes.

Begin with the basics. Confirm Nginx is running and serving the default page:

sudo systemctl status nginx --no-pager
curl -I http://localhost/
ls /var/www/html/

If localhost returns the default Nginx welcome page, the server itself works and the problem is in your site configuration. If even the default page fails, check whether another service occupies port 80, or whether the firewall blocks it. A fresh server that skips basic hardening can also have conflicting default blocks, so the first 10 things to do after buying a VPS checklist is worth running before debugging further.

Check the Root Directive and File Location

The most common cause is a wrong root directive. Nginx looks for the requested file under the root path, so if your files live in /var/www/example.com but the configuration points at /var/www/html, every custom page returns 404 while the default page still works. Check both the configuration and the disk:

grep -rn "root " /etc/nginx/sites-enabled/
ls -la /var/www/example.com/
cat /etc/nginx/sites-enabled/example.com

The Nginx root directive is also the setting most often duplicated by mistake: a root inside a location block overrides the server-level value for that location only, which produces 404 errors on one path while the rest of the site works. Remove the location-level root unless you have a reason for it. Also check spelling and case. Linux paths are case-sensitive, so Index.html and index.html are different files, and a file uploaded as Home.html will not answer a request for home.html.

A Nginx 404 fresh install case often comes down to the index directive as well. If you request / and Nginx looks for index.html but your file is named home.html, the result is 404. Either rename the file to index.html or add your filename to the index list:

index index.html index.htm home.html;

Fix try_files and the Enabled Site Symlink

The second most common cause is a missing or broken symlink in sites-enabled/. Nginx only loads configurations linked there, so a file that exists in sites-available/ but was never enabled is silently ignored, and requests fall through to the default block, which serves a different root and returns 404 for your pages. Verify the symlink:

ls -la /etc/nginx/sites-enabled/
sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/
sudo nginx -t && sudo systemctl reload nginx

The third cause is a wrong try_files line. For a static site, the standard form is:

location / {
    try_files $uri $uri/ =404;
}

The standard Nginx try_files pattern above is the safest default for a static site, because it only serves files that exist and returns a clean 404 otherwise. If try_files points at a fallback that does not exist, such as /index.php on a server without PHP, every missing file becomes a confusing error instead of a clean 404. Also check for a stray alias directive, which replaces the location path instead of appending to the root and produces 404 errors that look identical to root problems but have a different fix. For WordPress or other PHP apps, the fallback should route to index.php with the query string preserved, which is covered in the LEMP setup and the Nginx reverse proxy setup tutorial. The full server block pattern, including where try_files belongs, is explained in the guide to setting up Nginx server blocks on Ubuntu 24.04.

Check Permissions and the Error Log

If the file exists and the root is right but the 404 persists, check permissions. Nginx runs as www-data and needs read permission on the file and execute permission on every parent directory. A file owned by root with mode 600, or a home directory with mode 700 above the web root, blocks access even though the file is exactly where the configuration says it should be:

ls -ld /var/www /var/www/example.com
namei -l /var/www/example.com/index.html
sudo tail -n 30 /var/log/nginx/error.log

The error log is the fastest way to confirm the diagnosis. It prints the exact filesystem path Nginx tried to open, so you can compare it against where the file actually lives. If the logged path differs from your expectation, the root or an alias directive is wrong. If the logged path matches but access is denied, permissions are wrong. If you are connecting to the server for the first time to run these checks, the guide to connecting to your VPS via SSH for the first time covers access basics. Fix one cause at a time, reload Nginx after each change, and retest the same URL, so you know which fix actually solved it.

Ditulis oleh

Firdaus Safari Putra

Founder, PT FirKaizen Digital Solutions

Firdaus Safari Putra adalah founder PT FirKaizen Digital Solutions. Ia membangun dan mengoperasikan infrastruktur server di balik panduan ini, dan menulis berdasarkan apa yang benar-benar ia jalankan, uji, dan ukur — bukan teori.