Tuesday, August 16, 2011

Simple Load Balancing with nginx on Debian

Suppose you need distribute http workload across multiple computers/processes. Here we will be using nginx to implement load balancing (using round-robin scheduling). Let start by installing nginx package:
apt-get -y install nginx
Place the following into /etc/nginx/sites-available/my-site:
upstream backend {
    // every 7 requests: 5 requests to s1 server 
    // and one request to the second and the third
    server s1 weight=5;
    // after 3 unsuccessful attempts within 
    // the 30 seconds s2 is considered 
    // inoperative
    server unix:/tmp/s2 max_fails=3 fail_timeout=30s;
    server 127.0.0.1:8080;
}   
 
server {
    location / {
       proxy_pass http://backend;
    }   
}  
Now disable default site configuration, enable a new one and restart nginx:
rm /etc/nginx/sites-enabled/default
ln -s /etc/nginx/sites-available/my-site \
    /etc/nginx/sites-enabled/my-site
/etc/init.d/nginx restart
Read more about nginx upstream module here.

No comments :

Post a Comment