Nginx is a high-performance web server and reverse proxy server. It can serve static content, forward requests to backend applications, and distribute traffic among multiple servers (load balancing).
This project demonstrates load balancing with Nginx using multiple Node.js application instances in Docker. Each instance serves the same webpage, and Nginx distributes incoming requests among them in a round-robin manner. The webpage also displays which instance served the request, so you can see load balancing in action.
docker-compose.yaml
Dockerfile
index.html
package.json
server.js
images/
nginx.png
nginx/
nginx.conf
- Make sure Node.js is installed.
- Install dependencies:
npm install
- Start the app:
npm start
- Open your browser:
http://localhost:3000→ you will see the webpage served by a single Node.js instance.
- Build Docker image:
docker build -t myapp:v1 . - Run container:
docker run -d -p 3000:3000 myapp:v1
- Open your browser:
http://localhost:3000→ same webpage served from inside Docker.
- Bring down any running containers:
docker-compose down
- Start multiple Node.js app containers:
docker-compose up --build
- Access each instance individually (optional):
http://localhost:3001→ Instance 1http://localhost:3002→ Instance 2http://localhost:3003→ Instance 3
- Nginx container will distribute requests among the three Node.js instances.
- Start everything with Docker Compose (includes Nginx):
docker-compose up --build
- Open browser:
http://localhost→ webpage shows which instance served the request.- Refresh to see Nginx round-robin in action.
- The project uses HTTP only for simplicity. No SSL is needed for this demo.
- The webpage clearly displays instance name and container ID, so it’s easy to see load balancing working.
- All configuration is self-contained in Docker, so you don’t need to install Nginx locally.
- Nginx uses HTTP/1.1 keep-alive by default, so browsers may reuse connections and hit the same instance multiple times. Wait a few seconds between refreshes to see round-robin