https://github.com/gitfrandu4/load-balancer
Load Balancing Node.js Applications with NGINX and Docker
https://github.com/gitfrandu4/load-balancer
Last synced: 6 months ago
JSON representation
Load Balancing Node.js Applications with NGINX and Docker
- Host: GitHub
- URL: https://github.com/gitfrandu4/load-balancer
- Owner: gitfrandu4
- Created: 2021-11-03T14:17:45.000Z (over 4 years ago)
- Default Branch: main
- Last Pushed: 2021-11-05T00:47:41.000Z (over 4 years ago)
- Last Synced: 2025-05-22T21:14:00.030Z (about 1 year ago)
- Language: HTML
- Homepage:
- Size: 44.9 KB
- Stars: 2
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
Awesome Lists containing this project
README
# Load Balancing Node.js Applications with NGINX and Docker
## Configuración 4

Ejemplo de **server nodejs**:
```javascript
const express = require('express')
const app = express()
let ejs = require('ejs')
let fs = require('fs')
const port = process.env.PORT
let objectSentFromServer = [...]
app.get('/', (req, res) => {
fs.readFile(__dirname + '/index.html', 'utf-8', (err, html) => {
res.send(ejs.render(html, { objectSentFromServer: objectSentFromServer}))
})
})
app.listen(port, () => {
console.log(`Example app listening at http://localhost:${port}`)
})
```
**Configuración nginx.conf - Load balancer**
```javascript
upstream node_cluster {
server server1:3000 weight=1;
server server2:3001 weight=1;
server server3:3002 weight=1;
}
server {
listen 80;
server_name _;
location / {
proxy_read_timeout 300s;
proxy_connect_timeout 75s;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_pass http://node_cluster/;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}
}
```
```
Build images:
docker build -t node-app .\node\.
docker build -t nginx-conf4 .\nginx\.
Start containers:
docker-compose up
```