Fix 404 Errors When Refreshing Frontend Pages

Understand single-page application fallbacks and prevent APIs and missing assets from returning HTML.

🎯 Introduction

Some sites let you navigate to /notes/1 through a menu but return 404 when you refresh. This usually happens because navigation inside the browser and file lookup on the server follow different rules. This article addresses that refresh problem.


🧱 Why Clicking and Refreshing Behave Differently

A single-page application (SPA) typically loads one index.html, then uses JavaScript in the browser to choose what to display based on the path. Clicking a menu can update the URL and view without requesting the entire HTML document again. The rules mapping paths to views are called frontend routing.

On refresh, the browser requests /notes/1 directly. Nginx does not know the frontend routing rules; it looks for html/notes/1 on disk. That file does not exist, so the original try_files returns 404.

The solution is to look for a real file first for page paths, then send index.html if none exists and let the frontend choose the view.


🛠 Apply the Fallback Only to Page Paths

Replace the original location / in site.conf and add asset rules:

location ^~ /assets/ {
    try_files $uri =404;
}

location = /index.html {
    add_header Cache-Control 'no-cache';
}

location / {
    try_files $uri $uri/ /index.html;
}

The final /index.html is an internal redirect path, not an error code. The address bar does not change. The exact-match index block also prevents repeated fallbacks if the entry file is missing. Cache-Control tells the browser how to cache the response; the next article explains no-cache.

Handle /assets/ separately because missing JavaScript and CSS files should return 404. Returning HTML instead can produce script syntax or content type errors in the browser, making diagnosis harder.

The future /api/ location will use a reverse proxy and must not fall back to the home page either. If images and fonts live under /images/ or /fonts/, give those directories equivalent separate file rules.


🧪 Observe the Server's Behavior

Check and reload, then run:

curl -i http://127.0.0.1:8080/notes/1
curl -i http://127.0.0.1:8080/assets/missing.js

The first command should return 200 with the home page HTML; the second should return 404. The exercise page has no frontend router, so opening /notes/1 in a browser still displays “My Study Notes”. This proves the fallback works; it does not implement a note detail page.

If you later build the site with a frontend build tool, put its build output in the public directory. Nginx sends files, while frontend code handles view changes. Applications that generate HTML on the server for each request should be proxied to the application as described in the proxy chapter, rather than using a blanket static fallback.


❓ Questions and Answers

Q1: Is it normal for nonexistent pages to return 200 after configuring a fallback?

That is the result of this example's fallback. The frontend determines whether a page exists. If you need the server to return accurate page-level 404 statuses, use an appropriate server-side rendering or generation approach.

Q2: Can I use error_page 404 /index.html as a site-wide fallback?

That is not recommended as a starting point. It can mix page errors with file and API errors that should not fall back. First distinguish page paths from asset paths explicitly.


🧪 Exercise

Temporarily rename site.css, request /assets/site.css, then request /notes/2. Restore the filename afterward.

Check your result: the missing stylesheet returns 404, while the page path still returns the home page HTML. Explain why the two URL categories need different rules. See the official try_files documentation for fallback behavior.


🎉 Page refreshes and missing assets now have separate, explicit handling!