NGINX Load Balancing
Setting up a load balancer with NGINX is easy. Really simple load balancing configuration. Just add this to your server block. This will use round-robin to balance connections by default.
NOTE - You are going to need a working NGINX isntallation first. If you don’t know how to do that just check our general NGINX guide HERE. Also, as an alternative you might want to consider using HAProxy which might be considered a better free, software based load balancing solution.
sudo vi /etc/nginx/sites-available/default
http {
upstream example.org {
server web1.example.com;
server web2.example.com;
server web3.example.com;
}
server {
listen 80;
location / {
proxy_pass http://example.org;
}
}
}
Thats it! You’re done!
Keep reading if you want to learn more.
Setting up a load balancer or reverse proxy gives you the following:
- optimizing resource utilization
- maximizing throughput
- reducing latency
- ensuring fault-tolerant
Load balancing alorithms to choose from:
- round-robin - DEFAULT - just picks the next server in they list, cycle through each server
- least-connected - server with least number of active connections will be selected
- ip-hash - This uses a hash function based on the clients IP to select the next server. This insures that each client connects back to the same server.
NGINX can load balance these things:
- HTTP
- HTTPS
- FastCGI ( fastcgi_pass module )
- uwsgi ( uwsgi_pass module )
- SCGI ( scgi_pass module )
- memcached ( memcached_pass module )
- gRPC ( grpc_pass module )
Least Connected
If you want to use least connected instead of round robin you can use the ‘least_conn’ directive as shown in the example below. Just change one line.
http {
upstream example.org {
least_conn;
server web1.example.com;
server web2.example.com;
server web3.example.com;
}
server {
listen 80;
location / {
proxy_pass http://example.org;
}
}
}
Weighted Load Balancing
If you want to give more weight to a given server you can. For example, out of 10 connections, web1 will receive 6 connections while web2 and web3 will only receive 2 connections each.
upstream myapp1 {
server web1.example.com weight=3;
server web2.example.com;
server web3.example.com;
}
Weighted load balancing also works with least-connected and ip-hash in newer versions of NGINX.
- in-band (or passive) server health checks
-
hosts that fail the test will be marked failed and avoided for a while
- max_fails - number of consecutive unsuccessful checks
- 0 - health checks disabled
- 1 - default
- fail_timeout - how long to stay in failed state before being probed with live client requests and marked live if these work.
More
There is a lot more to know. You can check the “further reading” section on this page:
HERE.