Skip to content

Repository files navigation

🚀 Nginx Reverse Proxy & Load Balancing with Node.js (Dockerized)

This repository demonstrates a production-style architecture using:

  • Node.js (Express)
  • Nginx as Reverse Proxy
  • Horizontal Scaling (Multiple Instances)
  • Docker & Docker Compose
  • Load Testing using Autocannon

The goal of this project is to deeply understand:

  • How Nginx works as a reverse proxy
  • How Docker networking works
  • How horizontal scaling improves performance
  • How failover behaves when instances go down
  • When to rebuild images vs reuse containers

🏗 Architecture

Development Mode (Multi-instance + Nginx)

Client
   ↓
Nginx (port 8000)
   ↓
app1 | app2 | app3 (Node containers)

Nginx distributes traffic across multiple Node instances.


📦 Tech Stack

  • Node.js 22 (Alpine)
  • Express
  • Nginx
  • Docker
  • Docker Compose
  • Nodemon (dev only)
  • Autocannon (load testing)

🧠 Key Concepts Demonstrated

  • Reverse proxy configuration
  • Docker internal networking
  • Service name DNS resolution
  • Horizontal scaling
  • Failover testing
  • Dev vs Production Docker setups
  • Load testing and performance comparison

📁 Project Structure

.
├── app.js
├── package.json
├── Dockerfile
├── Dockerfile.dev
├── docker-compose.dev.yml
├── docker-compose.prod.yml
├── nginx.conf
└── README.md

⚙️ Setup & Installation

1️⃣ Clone the Repository

git clone <your-repo-url>
cd ngnixprac

🧪 Development Mode (With Nginx + 3 Instances)

Start the development environment:

npm run docker:dev

If you changed dependencies or Dockerfile:

npm run docker:dev:build

To stop everything:

npm run docker:dev:down

🌐 Access Application

http://localhost:8000

Each refresh should show different INSTANCE_ID, proving load balancing works.

Example response:

{
  "message": "Node js server running ngnix 🚀🚀🚀",
  "instance": "2"
}

🏭 Production Mode (Single Container)

Start production container:

npm run docker:prod

Access:

http://localhost:3000

Production image:

  • No nodemon
  • Uses npm install --production
  • Restart policy enabled

🐳 Docker Commands Explained

Start environment

docker compose up

Rebuild image

docker compose up --build

Stop & remove containers

docker compose down

Stop specific service

docker compose stop app1

🧪 Load Testing

Install autocannon globally (optional):

npm install -g autocannon

Test via Nginx:

npx autocannon -c 100 -d 15 http://localhost:8000

Test direct Node:

npx autocannon -c 100 -d 15 http://localhost:3000

📊 Scaling Experiment

With 1 instance:

  • ~13k req/sec (baseline)

With 3 instances:

  • Near-linear scaling under Nginx

Stop one container during load:

docker compose -f docker-compose.dev.yml stop app2

Observe:

  • Traffic reroutes automatically
  • System remains operational
  • Throughput decreases proportionally

🔍 Nginx Reverse Proxy Config

events {}

http {
    upstream backend {
        server app1:3000;
        server app2:3000;
        server app3:3000;
    }

    server {
        listen 80;

        location / {
            proxy_pass http://backend;
        }
    }
}

🧠 Key Learnings

  • localhost inside Docker refers to the container itself
  • Docker services communicate via internal DNS
  • Horizontal scaling improves throughput
  • Nginx handles connection management efficiently
  • docker compose up vs up --build
  • Dev vs Production image separation

🚀 Ngnix is Awesome! 🚀

This repository demonstrates practical backend infrastructure knowledge including:

  • Reverse proxy architecture
  • Container orchestration fundamentals
  • Load balancing
  • High availability testing
  • Performance benchmarking

About

Production-ready Node.js architecture using Nginx reverse proxy, Docker, and horizontal scaling with load balancing and failover simulation.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages