Deploying a Node.js REST API to production requires more than just running node index.js. For enterprise reliability, you need container isolation using Docker, reverse proxying with Nginx, process supervision, and automated SSL termination via Let's Encrypt.
1. Multi-Stage Dockerfile Strategy
To keep production Docker images minimal, use multi-stage builds. Separating your build layer from runtime dependencies reduces final image sizes by up to 75%:
# Build stage FROM node:20-alpine AS builder WORKDIR /app COPY package*.json ./ RUN npm ci COPY . . RUN npm run build # Production stage FROM node:20-alpine AS runner WORKDIR /app ENV NODE_ENV=production COPY --from=builder /app/package*.json ./ COPY --from=builder /app/dist ./dist RUN npm ci --only=production EXPOSE 3000 CMD ["node", "dist/index.js"]
2. Configuring Nginx Reverse Proxy
Nginx handles TLS termination, protects your internal Node.js port 3000, and enables request rate limiting. Here is a battle-tested server block configuration:
server {
server_name api.yourdomain.com;
location / {
proxy_pass http://127.0.0.1:3000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
}3. Automated SSL & EC2 Deployment
Once your Docker container is running on AWS EC2, run sudo certbot --nginx -d api.yourdomain.com to automatically request and install an SSL certificate. This setup ensures zero downtime and automated SSL renewals.