Skip to content

Instantly share code, notes, and snippets.

@iambryancs
Created July 3, 2023 09:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iambryancs/df5d4207f262897881c259d4c1b6aaf5 to your computer and use it in GitHub Desktop.
Save iambryancs/df5d4207f262897881c259d4c1b6aaf5 to your computer and use it in GitHub Desktop.
SPA routing with Nginx

SPA routing with Nginx

To allow Nginx to route non-existing path back to index.html, do:

server {
      listen 80;
      listen [::]:80;
      server_name localhost;

      location / {
            root /usr/share/nginx/html;
            index index.html;
            try_files $uri $uri/ /index.html =404;
      }
}

The try_files directive tries the $uri first, then $uri/, then /index.html which should exist. In case all fails, it will use =404 to serve the default 404 page.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment