Create L7 / Host or Path Based Load Balancer
events {
#empty;
}
http {
server {
listen 80;
location / {
proxy_pass http://frontend;
}
}
upstream frontend {
server 172.17.0.2:80 weight=3;
server 172.17.0.3:80 weight=1;
}
}
GitHub Link: https://github.com/sumonaust/nginx-static-loadbalance-docker
Create L4 / IP based Load Balancer
events {
#worker_connections 1024;
}
# Layer 4 (TCP) Load Balancing Configuration
stream {
upstream tcp_backend {
# Define backend servers for TCP load balancing
server backend1.example.com:80 weight=3; # Backend 1 (higher weight)
server backend2.example.com:80 weight=1; # Backend 2
}
# TCP Load Balancer listening on port 80
server {
listen 80; # Port for Layer 4 TCP load balancing
proxy_pass tcp_backend; # Pass the TCP connection to the upstream servers
}
}