Using nginx as http load balancer

Load balancing across multiple application instances is a commonly used technique for optimizing resource utilization, maximizing throughput, reducing latency, and ensuring fault-tolerant configurations.
Nginx is a very efficient HTTP load balancer to distribute traffic to several application servers and to improve performance, scalability and reliability of web applications.

Nginx Load balancing methods
The following load balancing mechanisms (or methods) are supported in nginx:
  • round-robin
  • least-connected
  • ip-hash
See detail at http://nginx.org/en/docs/http/load_balancing.html

Initialization
using nginx as load balancing on centos 7
Nginx Load Banlancing
In this tutorial I use any hostname and ip addresses as follows:
srv-lb01 (10.11.218.250) (Nginx load balancer)
srv-web01 (10.11.218.251) ( Web server)
srv-web02 (10.11.218.252) ( Web server)
srv-web03 (10.11.218.253) ( Web server)
Note: pointing nginx.vn to 10.11.218.250 (Replace nginx.vn with your real site name)

Default load balancing configuration (round-robin)
When load balancing method is not specifically configured, it defaults to round-robin. All requests are proxied to the server group, and nginx applies HTTP load balancing to distribute the requests.

Install Nginx on srv-lb01, see at http://vietsystem.blogspot.com

Create a new configuration file called /etc/nginx/sites-available/nginx.vn with the following contents:
vim /etc/nginx/sites-available/nginx.vn
#Defines a group of servers andc Load balancing methods
upstream nginx.vn {
            server 10.11.218.251;
            server 10.11.218.252;
            server 10.11.218.253;
}
server {
            listen 80;
            error_log /var/log/nginx/nginx.vn-error.log;
            location / {
                          proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                          proxy_pass http://nginx.vn;
            }
}

Enable the site and restart Nginx:
# cd /etc/nginx/sites-enabled
# ln -s ../sites-available/nginx.vn .
#systemctl restart nginx.service
Open web browser and go to http://nginx.vn


Least connected load balancing
Least-connected load balancing is activated when the least_conn directive is used as part of the server group configuration:
#Defines a group of servers andc Load balancing methods
upstream nginx.vn {
     least_conn;
            server 10.11.218.251;
            server 10.11.218.252;
            server 10.11.218.253;
}

Ip hash load balancing
To configure ip-hash load balancing, just add the ip_hash directive to the server (upstream) group configuration:
#Defines a group of servers andc Load balancing methods
upstream nginx.vn {
     ip_hash;
            server 10.11.218.251;
            server 10.11.218.252;
            server 10.11.218.253;
}

Weighted load balancing
When the weight parameter is specified for a server, the weight is accounted as part of the load balancing decision.

#Defines a group of servers andc Load balancing methods
upstream nginx.vn {
            server 10.11.218.251 weight=2;
            server 10.11.218.252;
            server 10.11.218.253;
}
With this configuration, every 4 new requests will be distributed across the application instances as the following: 2 requests will be directed to srv-web01, one request will go to srv-web02, and another one to srv-web03.

Comments

Popular posts from this blog

Some common php modules

Install Nginx With PHP And MySQL On CentOS 7

Redirect all unencrypted traffic to HTTPS in Apache