Skip to content

Instantly share code, notes, and snippets.

@dev-sareno
Last active September 29, 2023 08:33
Show Gist options
  • Save dev-sareno/d963cce9dd831e9d8f86cc822afad799 to your computer and use it in GitHub Desktop.
Save dev-sareno/d963cce9dd831e9d8f86cc822afad799 to your computer and use it in GitHub Desktop.
Nginx config for Single Page Application (SPA)
$ cat <<EOF > nginx.conf
server {
    listen 80;
    listen [::]:80;
    server_name _;
    
    root /usr/share/nginx/html;

    location / {
        try_files $uri $uri/ /index.html =404;
    }
}
EOF

$ cat <<EOF > Dockerfile
FROM node:18-alpine AS build
...

FROM nginx:alpine 
WORKDIR /usr/share/nginx/html
COPY --from=build /app/dist .
COPY nginx.conf /etc/nginx/conf.d/default.conf
EXPOSE 80
EOF

This fixes the 404 Not Found issue when you reload the page in the browser.

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