Skip to content
Snippets Groups Projects
Commit 2b1adef3 authored by Rokas Stankunas's avatar Rokas Stankunas
Browse files

feat: implement nginx load balancer

parent 13f79cc7
Branches 18-exercise-3-scaling-additional-features
No related tags found
1 merge request!13feat: implement nginx load balancer
Pipeline #16880 passed
version: '3.9'
services:
mongodb:
image: mongo:latest
......@@ -33,4 +32,16 @@ services:
depends_on:
- mongodb
environment:
- MONGO_URI=${MONGO_URI}
\ No newline at end of file
- MONGO_URI=${MONGO_URI}
nginx:
image: nginx:latest
container_name: nginx
ports:
- "80:80"
depends_on:
- todo-app-1
- todo-app-2
volumes:
- "./nginx.conf:/etc/nginx/nginx.conf:ro"
- "/var/log/nginx/access.log:/var/log/nginx/access.log"
events {
# Set the maximum number of simultaneous connections that can be opened by a worker process
worker_connections 1024;
}
http {
# Define an upstream group for load balancing the todo-app services
upstream todo-app {
ip_hash;
server todo-app-1:3000; # First server in the upstream group
server todo-app-2:3000; # Second server in the upstream group
}
# Define a custom log format for capturing upstream request details
log_format upstreamlog '[$time_local] $remote_addr - $remote_user - $request '
'"$status" $body_bytes_sent '
'"$http_referer" "$http_user_agent" '
'"$gzip_ratio" '
'${host} $request_uri '
'$upstream_addr ';
server {
listen 80; # Listen on port 80 for incoming HTTP requests
location / {
# Proxy requests to the upstream todo-app group
proxy_pass http://todo-app;
proxy_http_version 1.1; # Use HTTP/1.1 for proxying
proxy_set_header Upgrade $http_upgrade; # Set the Upgrade header
proxy_set_header Connection 'upgrade'; # Set the Connection header to upgrade
proxy_set_header Host $host; # Forward the original Host header
proxy_cache_bypass $http_upgrade; # Bypass cache for upgraded requests
# Log access requests using the defined upstream log format
access_log /var/log/nginx/access.log upstreamlog;
}
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment