Skip to content

Instantly share code, notes, and snippets.

@caesartcs
Created April 16, 2022 20:55
Show Gist options
  • Save caesartcs/7e8a3d6a2de8481b1a51804a4fc3f485 to your computer and use it in GitHub Desktop.
Save caesartcs/7e8a3d6a2de8481b1a51804a4fc3f485 to your computer and use it in GitHub Desktop.
Load Balancing for root and different paths
#credit to Subhra Paladhi
# This is for layer 7 (application layer) load balancer
http{
# A http request is sent on port 80. A TCP connection is established between
# the client(the one who sent the request) and loadbalancer on port 80 over http.
# The load balancer further establishes TCP connections with one of the server
# depending on the algorithm it is using and forwards the request from the
# client to it. The server communicats with nginx.Hense nginx acts as a
# middle man between the client and server.
# default loadbalancing algo is round robin
# load balanances between app running on port 2222,3333,4444,5555
upstream allbackend {
server 127.0.0.1:2222;
server 127.0.0.1:3333;
server 127.0.0.1:4444;
server 127.0.0.1:5555;
}
# load balanances between server running on port 2222,3333
upstream app1backend{
server 127.0.0.1:2222;
server 127.0.0.1:3333;
}
# loadbalanances between server running on port 4444,5555
upstream app2backend{
server 127.0.0.1:4444;
server 127.0.0.1:5555;
}
# This block decides for which route which upstream will be used.
server{
# This is the port over will nginx communicats with the client.
# For HTTP it is 80.
listen 80;
# if someone requests "/" route then redirent to upstream allbackend
location /{
proxy_pass http://allbackend/;
}
# If someone requests "/app1" route then it should be loadbalanced
# between 2 ports.
# Redirent to upstream app1backend.
location /app1{
proxy_pass http://app1backend/;
}
# If someone requests "/app2" route then it should be loadbalanced
# between 2 ports.
# Redirent to upstream app2backend
location /app2{
proxy_pass http://app2backend/;
}
# Access to /admin is forbidden over port 80(public internet).
# Hense return status code 403 (forbidden) when /admin request
# is received.
location /admin{
return 403;
}
}
}
events { }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment