Creating a Load Balancer on Nginx Server. The following codes show how to create a load balancer on Nginx Server. Round Robin Load Balancer, IP Hash Load Balancer, Last Connections Load Balancer, Weighted Round Robin Load Balancer
Load Balancer Using Round Robin Algorithms:
upstream roundrobin {
server 192.168.33.12:7000;
server 192.168.33.12:7001;
server 192.168.33.12:7002;
}
Load Balancer Using Last Connections Algorithms:
upstream leastconn {
least_conn;
server 192.168.33.12:7000;
server 192.168.33.12:7001;
server 192.168.33.12:7002;
}
u
Load Balancer Using IP Hash Algorithms:
upstream iphash {
ip_hash;
server 192.168.33.12:7000;
server 192.168.33.12:7001;
server 192.168.33.12:7002;
}
Load Balancer Using Weighted Round Robin Algorithms:
upstream weighted {
server 192.168.33.12:7000 weight=2;
server 192.168.33.12:7001;
server 192.168.33.12:7002;
}
Create new virtual configuration file:
vim /etc/nginx/conf.d/load_balancer.conf
Add the following codes to laod_balancer.conf file:
upstream app_server_7000 {
server 192.168.33.12:7000;
}
upstream app_server_80 {
server 192.168.33.12:80;
}
upstream roundrobin {
server 192.168.33.12:7000;
server 192.168.33.12:7001;
server 192.168.33.12:7002;
}
upstream leastconn {
least_conn;
server 192.168.33.12:7000;
server 192.168.33.12:7001;
server 192.168.33.12:7002;
}
upstream iphash {
ip_hash;
server 192.168.33.12:7000;
server 192.168.33.12:7001;
server 192.168.33.12:7002;
}
upstream weighted {
server 192.168.33.12:7000 weight=2;
server 192.168.33.12:7001;
server 192.168.33.12:7002;
}
server {
listen 8181;
location / {
proxy_pass http://app_server_80/;
}
location /proxy {
proxy_pass http://app_server_7000/;
}
location /roundrobin {
proxy_pass http://roundrobin/;
}
location /leastconn {
proxy_pass http://leastconn/;
}
location /iphash {
proxy_pass http://iphash/;
}
location /weighted {
proxy_pass http://weighted/;
}
}