Using Nginx as a load balancer

Install Nginx

>sudo aptitude install nginx -y

Go to edit file nginx.conf by command

>sudo vi /etc/nginx/nginx.conf

Find string “http {” (search in vi by type /string4search) and go to edit in this tag

For basic load blancing:

http {

upstream yoursite {

server       yoursite1.yoursite.com;

server       yoursite2.yoursite.com;

}

server {

server_name   yoursite.com  www.yoursite.com;

location / {

proxy_pass    http://yoursite;

}

}

}

This basic configuration will send 50% of the requests for http://www.yoursite.com to yoursite1.yoursite.com and the other 50% to yoursite2.yoursite.com.

ip_hash

You can specify the ip_hash directive that guarantees the client request will always be transferred to the same server.

If this server is considered inoperative, then the request of this client will be transferred to another server.

upstream yoursite {

ip_hash;

server      yoursite1.yoursite.com;

server     yoursite2.yoursite.com;

}

Down 

If one of the servers must be removed for some time, you must mark that server as down.

upstream yoursite {

ip_hash;

server        yoursite1.yoursite.com    down;

server        yoursite2.yoursite.com;

}

Weight

If you add a weight tag onto the end of the server definition you can modify the percentages of the requests send to the servers.

When there’s no weight set, the weight is equal to one.

upstream yoursite {

server yoursite1.yoursite.com weight=4;

server yoursite2.yoursite.com;

}

This configuration will send 80% of the requests to yoursite1.yoursite.com and the other 20% to yoursite2.yoursite.com.

Note: It’s not possible to combine ip_hash and weight directives.

max_fails and fail_timeout

max_fails 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 yoursite {

server      yoursite1.yoursite.com;

server      yoursite2.yoursite.com max_fails=3 fail_timeout=30s;

}

In this configuration nginx will consider yoursite2.yoursite.com as inoperative if a request fails 3 times with a 30s timeout.

backup

If the non-backup servers are all down or busy, the server(s) with the backup  directive will be used.

upstream yoursite {

server    yoursite1.yoursite.com   max_fails=3;

server    yoursite2.yoursite.com   max_fails=3;

server    yourbackup.yoursite.com backup;

}

This configuration will send 50% of the requests for http://www.yoursite.com to yoursite1.yoursite.com and other 50% to yoursite2.yoursite.com. If yoursite1.yoursite.com and yoursite2.yoursite.com both fails 3 times the requests will be send to yourbackup.yoursite.com.

Advertisements

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s