├── README.md ├── docker-compose.yml ├── nginx.conf └── webapi ├── .dockerignore ├── Dockerfile ├── package-lock.json ├── package.json └── server.js /README.md: -------------------------------------------------------------------------------- 1 | # Load Balancer Project with Nginx, Node.js, Docker, and Docker Compose 2 | 3 | This is a simple load balancer built using Nginx, Node.js, Docker, and Docker Compose. The purpose of this load balancer is to distribute incoming traffic to multiple instances of a Node.js application. 4 | 5 | ## Getting Started 6 | 7 | To get started with this project, you will need to have Docker and Docker Compose installed on your machine. 8 | 9 | 1. Clone the repository 10 | 11 | ```bash 12 | git clone https://github.com/ErickWendel/loadbalancer-nginx-docker-nodejs.git 13 | ``` 14 | 15 | 2. Navigate to the project directory 16 | 17 | ```bash 18 | cd loadbalancer-nginx-docker-nodejs 19 | ``` 20 | 21 | 3. Start the application 22 | 23 | ```bash 24 | docker-compose up -d 25 | ``` 26 | 27 | This will start the Node.js application and the Nginx load balancer. You can access the application by navigating to `http://localhost:3000`. 28 | 29 | Inspired by [josephDev123](https://github.com/josephDev123/loadbalancer-nginx-docker-nodejs.git) 30 | 31 | 32 | 33 | -------------------------------------------------------------------------------- /docker-compose.yml: -------------------------------------------------------------------------------- 1 | version: '3.8' 2 | 3 | x-webapi-common: &webapi-common 4 | build: ./webapi 5 | 6 | services: 7 | nginx: 8 | image: nginx:latest 9 | ports: 10 | - "3000:80" 11 | depends_on: 12 | - webapi1 13 | - webapi2 14 | - webapi3 15 | volumes: 16 | - ./nginx.conf:/etc/nginx/conf.d/default.conf:ro 17 | 18 | webapi1: 19 | <<: *webapi-common 20 | environment: 21 | - SERVICE_NAME=webapi1 22 | 23 | webapi2: 24 | <<: *webapi-common 25 | environment: 26 | - SERVICE_NAME=webapi2 27 | 28 | webapi3: 29 | <<: *webapi-common 30 | environment: 31 | - SERVICE_NAME=webapi3 32 | -------------------------------------------------------------------------------- /nginx.conf: -------------------------------------------------------------------------------- 1 | upstream loadBalancer { 2 | server webapi1:9000; 3 | server webapi2:9000; 4 | server webapi3:9000; 5 | } 6 | 7 | server { 8 | listen 80; 9 | location / { 10 | proxy_pass http://loadBalancer; 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /webapi/.dockerignore: -------------------------------------------------------------------------------- 1 | ./node_modules 2 | -------------------------------------------------------------------------------- /webapi/Dockerfile: -------------------------------------------------------------------------------- 1 | from node:22-alpine 2 | 3 | ADD . /app 4 | 5 | WORKDIR /app 6 | 7 | RUN npm ci --omit=dev 8 | 9 | CMD npm start -------------------------------------------------------------------------------- /webapi/package-lock.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "backend", 3 | "version": "1.0.0", 4 | "lockfileVersion": 3, 5 | "requires": true, 6 | "packages": { 7 | "": { 8 | "name": "backend", 9 | "version": "1.0.0", 10 | "license": "ISC", 11 | "engines": { 12 | "node": "v22.13.1" 13 | } 14 | } 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /webapi/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "backend", 3 | "version": "1.0.0", 4 | "description": "", 5 | "type": "module", 6 | "main": "server.js", 7 | "scripts": { 8 | "dev": "node --watch server.js", 9 | "start": "node server.js", 10 | "test": "echo \"Error: no test specified\" && exit 1" 11 | }, 12 | "author": "erickwendel", 13 | "license": "ISC", 14 | "engines": { 15 | "node": "v22.13.1" 16 | } 17 | } -------------------------------------------------------------------------------- /webapi/server.js: -------------------------------------------------------------------------------- 1 | import { createServer } from 'node:http' 2 | 3 | const PORT = process.env.PORT || 9000; 4 | createServer((req, res) => { 5 | res.end(`Hello from ${process.env.SERVICE_NAME}`) 6 | }) 7 | .listen(PORT, () => console.log(`Server running on port ${PORT}`)) 8 | --------------------------------------------------------------------------------