>_ IzzyLab

Hands-on task

This task is step 6 of the course “nginx basics”

SPA: All Routes to index.html

medium~10 min

For a single-page application, configure nginx (port 80) so that any non-existent path serves index.html (with status 200) rather than 404 — then client-side routing will work.

What you'll practise:
  • nginx
  • try_files
  • spa
  • client-side routing
  • location

Topic breakdown

In single-page applications, routing lives in the browser: on disk there's a single index.html, while paths like /some/route don't physically exist. The try_files directive inside location goes through the options in order and serves the first one found; the key to an SPA is to make the last fallback not =404 but index.html itself, so any path not found returns the start page with status 200. The argument order is significant: first a file ($uri) and a directory are tried, and the trailing option is the fallback. After editing, the config is reread.

What's checked

  1. deep route serves index.html
  2. non-existent route returns 200

What it looks like