Nginx
Nginx is a free, open source, high performance web server. Nginx is known for its high performance, stability, rich feature set, simple configuration, and low resource consumption.
Load balancing using Nginx
HttpUpstreamModule provides simple load-balancing (round-robin and client IP) across backend servers. Here is a basic nginx configuration file for load balancing.
upstream backend_server {
server 192.168.1.11:8080;
server 192.168.1.12:8080;
}
server {
server_name www.domain.com;
location / {
proxy_pass http://backend_server;
}
}
The above configuration will send 50% of the requests for www.domain.com to 192.168.1.11:8080 and the other 50% to 192.168.1.12:8080 servers.
ip_hash
For session based web applications in which sessions will be stored in a particular server you have to using the directive ip_hash. This directive guarantees the client request will always be transferred to the same server.
upstream backend_server {
ip_hash;
server 192.168.1.11:8080;
server 192.168.1.12:8080;
}
weight
Servers can be assigned different weights. If not specified, weight is equal to 1. Requests are distributed according to the servers in a round-robin manner but taking into account the server weight.
upstream backend_server {
server 192.168.1.11:8080 weight=4;
server 192.168.1.12:8080;
}
This configuration will send 80% of the requests to server 192.168.1.11:8080 and the other 20% to 192.168.1.12:8080.
Note: It’s not possible to combine ip_hash and weight directives.
down
If one of the servers must be removed for some time, you must mark that server as *down*.
upstream backend_server {
ip_hash;
server 192.168.1.11:8080 down;
server 192.168.1.12:8080;
}
Note: Marks server as permanently offline, to be used with the directive ip_hash.
backup
If the non-backup servers are all down or busy, the server(s) with the backup directive will be used. Cannot use the backup directive along with the directive ip_hash.
upstream backend_server {
server 192.168.1.11:8080;
server 192.168.1.12:8080;
server 192.168.1.15:8081 backup;
}
max_fails & fail_timeout
Is a directive defining the number of unsuccessful attempts in the time period defined by fail_timeout before the server is considered inoperative. If not set, the number of attempts is one. A value of 0 turns off this check. If fail_timeout is not set the time is 10 seconds
upstream backend_server {
server 192.168.1.11:8080 weight=5;
server 192.168.1.12:8080 max_fails=3 fail_timeout=30s;
}
In this configuration nginx will consider 192.168.1.12:8080 as inoperative if a request fails 3 times with a 30s timeout.