Deploy Angular: Nginx, Docker and Static Hosting

Angular 15 min min read Updated: Nov 16, 2026 Intermediate
Deploy Angular: Nginx, Docker and Static Hosting
Intermediate Topic 2 of 3

The Refresh Problem

Angular is a single-page app: only index.html exists. When a user opens /products/5 directly, the server looks for a file called products/5, finds nothing and returns 404. The server must send index.html for unknown paths so the Angular router can take over. This is called SPA fallback.

Nginx

nginx server { listen 80; server_name _; root /usr/share/nginx/html; index index.html; # compress gzip on; gzip_types text/css application/javascript application/json image/svg+xml; # hashed files can be cached for a year location ~* \.(?:js|css|woff2?|png|jpg|jpeg|gif|svg|ico|webp)$ { expires 1y; add_header Cache-Control "public, immutable"; try_files $uri =404; } # never cache index.html so users get the new version location = /index.html { add_header Cache-Control "no-cache"; } # SPA fallback location / { try_files $uri $uri/ /index.html; } }

Docker (Multi-Stage Build)

dockerfile # stage 1: build FROM node:22-alpine AS build WORKDIR /app COPY package*.json ./ RUN npm ci COPY . . RUN npm run build # stage 2: serve FROM nginx:alpine COPY nginx.conf /etc/nginx/conf.d/default.conf COPY --from=build /app/dist/my-app/browser /usr/share/nginx/html EXPOSE 80 CMD ["nginx", "-g", "daemon off;"]
bash docker build -t my-angular-app . docker run -p 8080:80 my-angular-app # open http://localhost:8080
Folder name: Replace my-app with your project name from angular.json. In some versions the output folder is dist/<name> without browser for client-only builds, so check your dist folder after building.

Firebase Hosting

bash pm install -g firebase-tools firebase login firebase init hosting # public dir: dist/my-app/browser, single-page app: Yes ng build firebase deploy

Netlify

  • Connect your Git repository on netlify.com
  • Build command: ng build
  • Publish directory: dist/my-app/browser
  • Add a file public/_redirects containing: /* /index.html 200

Vercel and GitHub Pages

HostNotes
VercelAdd vercel.json with a rewrite of all paths to /index.html
GitHub PagesUse --base-href /repo-name/ and copy index.html to 404.html for fallback
S3 + CloudFrontSet the 404 error response to return /index.html with 200

Deploying an SSR App

SSR apps need a Node.js server, not just static files. Run node dist/my-app/server/server.mjs on a VM, a container platform or a service that supports Node. Fully prerendered sites (SSG) can still go to static hosting.

dockerfile FROM node:22-alpine WORKDIR /app COPY package*.json ./ RUN npm ci --omit=dev COPY dist ./dist ENV PORT=4000 EXPOSE 4000 CMD ["node", "dist/my-app/server/server.mjs"]

API and CORS

  • Either serve the API from the same domain (reverse proxy /api to your backend)
  • Or enable CORS on the API for your site's domain only
  • Use HTTPS everywhere
nginx location /api/ { proxy_pass http://backend:3000/; proxy_set_header Host $host; }
Deploy checklist: Open a deep link directly, refresh a nested page, check the console for 404s on assets and confirm API calls reach the right URL.

Conclusion

An Angular build is static files plus an SPA fallback to index.html. Nginx or Docker works anywhere, Firebase and Netlify are easy, and SSR needs a Node server.

Quick Summary: dist/<app>/browser + SPA fallback; Nginx, Docker, Firebase, Netlify.

Get Newsletter

Subscibe to our newsletter and we will notify you about the newest updates on Edugators