SyntaxStudy
Sign Up
Node.js Nginx Reverse Proxy and Docker Basics
Node.js Beginner 12 min read

Nginx Reverse Proxy and Docker Basics

In production, Node.js apps typically run behind a reverse proxy like Nginx that handles SSL termination, static file serving, and load balancing. Docker packages your app and all its dependencies into a container for consistent deployments.

Example
# --- Nginx configuration (/etc/nginx/sites-available/my-api) ---
# server {
#     listen 80;
#     server_name api.example.com;
#
#     location / {
#         proxy_pass         http://localhost: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;
#     }
# }

# Enable the site:
# sudo ln -s /etc/nginx/sites-available/my-api /etc/nginx/sites-enabled/
# sudo nginx -t && sudo systemctl reload nginx

# --- Dockerfile ---
# FROM node:20-alpine
# WORKDIR /app
# COPY package*.json ./
# RUN npm ci --only=production
# COPY . .
# EXPOSE 3000
# USER node
# CMD ["node", "src/index.js"]

# --- docker-compose.yml ---
# version: '3.9'
# services:
#   api:
#     build: .
#     ports:
#       - "3000:3000"
#     environment:
#       - NODE_ENV=production
#       - DB_HOST=db
#     depends_on:
#       - db
#   db:
#     image: postgres:16-alpine
#     environment:
#       POSTGRES_DB:       myapp
#       POSTGRES_USER:     admin
#       POSTGRES_PASSWORD: secret

# Build and run:
# docker build -t my-api .
# docker compose up -d

This is the last lesson in this section.

Create a free account to earn a certificate