Skip to content

Instantly share code, notes, and snippets.

@brownhash
Created January 6, 2024 19:00
Show Gist options
  • Save brownhash/3889b90d6d5c2e234af77bdbd820b1fc to your computer and use it in GitHub Desktop.
Save brownhash/3889b90d6d5c2e234af77bdbd820b1fc to your computer and use it in GitHub Desktop.
Configure Nginx for React application in Docker to prevent 404 on any route other than /
# Builder
FROM node:18-alpine3.17 as builder
WORKDIR /app
COPY . /app
WORKDIR /app/application_name
RUN npm install
RUN npm run build
# Executor
FROM nginx:alpine
# copy built package from builder image
COPY --from=builder /app/application_name/dist /usr/share/nginx/html
# copy nginx config and replace the default config with it
COPY ./nginx.conf /etc/nginx/conf.d/default.conf
EXPOSE 80
CMD ["nginx", "-g", "daemon off;"]
server {
listen 80;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
try_files $uri $uri/ /index.html =404;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment