404 Not Found on page refresh (React/Vue/Angular routes) (nginx) — how to fix
404 Not Found
nginx/1.24.0
GET https://example.com/dashboard/settings 404 (Not Found)
Quick fix: Make the server fall back to index.html for unknown paths. nginx: location / { try_files $uri $uri/ /index.html; }, then sudo nginx -t && sudo systemctl reload nginx. Apache: FallbackResource /index.html. Or switch the router to hash mode.
What it means
Client-side routers (React Router BrowserRouter, Vue Router history mode, Angular's default router) change the URL with the History API without asking the server. When you click a link, no request goes out. When you reload /dashboard/settings, the browser asks the server for that path, the server finds no such file, and returns 404.
The fix is to serve index.html for any path that isn't a real file, so the app boots and the router renders the right page. See HTTP status codes for what each response means.
Fix 1: nginx
server {
listen 80;
server_name example.com;
root /var/www/app/dist;
index index.html;
location / {
try_files $uri $uri/ /index.html;
}
}
try_files checks for the file, then a directory, then falls back to /index.html. Test and reload:
sudo nginx -t && sudo systemctl reload nginx
If the app lives under a sub-path (/app/):
location /app/ {
try_files $uri $uri/ /app/index.html;
}
and configure the same base in the app (base: "/app/" in Vite, basename="/app" for React Router, <base href="/app/"> in Angular).
Keep API routes in their own location /api/ { proxy_pass ...; } block so they don't fall back to index.html.
Fix 2: Apache
In the site config or .htaccess in the document root:
FallbackResource /index.html
(FallbackResource needs mod_dir, which is enabled by default on most installs.)
Fix 3: static hosts
- Netlify: file
public/_redirects(copied into the build output) containing/* /index.html 200 - Vercel:
vercel.jsonwith{ "rewrites": [{ "source": "/(.*)", "destination": "/index.html" }] } - GitHub Pages: has no rewrite rules. A common workaround is copying
index.htmlto404.htmlin the build output; the page loads, but the HTTP status is still 404. Hash routing (Fix 5) avoids that.
Fix 4: Docker images serving with nginx
The default nginx image config has no fallback. Copy your own config:
FROM nginx:alpine
COPY dist/ /usr/share/nginx/html/
COPY nginx.conf /etc/nginx/conf.d/default.conf
with a server block like Fix 1 (root /usr/share/nginx/html;).
Fix 5: hash routing
If you can't change the server, use URLs like /#/dashboard. The part after # is never sent to the server, so reloads always load index.html:
- React Router:
HashRouter(orcreateHashRouter) - Vue Router:
createWebHashHistory() - Angular:
provideRouter(routes, withHashLocation())
Check: assets 404 too?
If the reload returns HTML but the page is blank and JS/CSS 404 at /dashboard/assets/..., the build uses relative asset paths. Set an absolute base (base: "/" in Vite, "homepage" in CRA's package.json) and rebuild.
Last updated 2026-12-01.