Nginx SPA `try_files` Returning HTML for Missing `.mjs`/`.css` Assets

Catch-all `try_files $uri /index.html` masks missing JS files as `text/html` 200 OK.

Runs locally in browser Free · No login Independently Verified 2026-11-25 v4.1.0
Identifier: Nginx SPA `try_files` Returning HTML for Missing `.mjs`/`.css` Assets Platform: Nginx
Common Root Causes:
  • Catch-all `try_files $uri /index.html` masks missing JS files as `text/html` 200 OK.

Interactive Diagnostic Checklist

Verified Solutions:
  1. Use `location /assets/ { try_files $uri =404; }` so missing assets return real 404s.
Safe Diagnostic Command:
curl -sI https://<domain>/assets/missing.mjs
Warning: Verify configuration changes in staging or with dry-run flags (e.g. nginx -t) before reloading production services.

Technical Explanation

Catch-all `try_files $uri /index.html` masks missing JS files as `text/html` 200 OK. Recommended diagnostic: `curl -sI https://<domain>/assets/missing.mjs`. Remediation: Use `location /assets/ { try_files $uri =404; }` so missing assets return real 404s.

Worked Examples

  • Run `curl -sI https://<domain>/assets/missing.mjs` to verify root cause before applying the remediation.

Limitations & Assumptions

  • Always verify configuration changes in staging or with dry-run flags (e.g. `nginx -t`) before reloading production services.

Source References & Verification

Resource ID: err-nginx-try-files-spa-404-assets · Independently verified: 2026-11-25

Related Resources