NginX: Load Balancing, Failover, and Geo Location (Part 1)

by admin on January 19, 2012

in Http Maid Note

Yeah guys, let’s talk about this post title, really it is interesting, i mean for me this is very interesting :p. If you have a site, and then something happen to your server’s network, and unfortunately your site does not have backup or mirror site which is very essential because your site is your income. So the first thing you need to do is this, do not put your site in shared / reseller hosting. Why? Because in shared hosting, your site is not alone, there are so much neighbor in it, and your site could be exploited from those neighbor, for instance bug in script. Another thing is you cannot get root access from shared hosting, your creativity is limited by non-ssh regular user assigned to your account. So take a look at LowEndBox and get a vps for your shared hosting substitution.

Nuff said for the appetizer, let’s get to the main course. The simplest failover method for website is round robin dns, the illustration would be like this:

where the server has been assigned with two or more ip addresses, so if end user fail to access the server with the first ip address, it will be routed to the second ip address. To use that settings, you simply

  • Install DNS in your server and create nameservers with your ip addresses

    after that assign the nameservers to your domain in your domain panel.

  • If you don’t want to use DNS, you can add your ip addresses directly in your domain panel by creating A Record for each ip, for instance:

    A Record 100ms
    A Record 100ms

    but not all domain panels support two or more of the same hostname, so you can do research for supported domain panel or using DNS.

The illustration below is as same as the failover method above, but this is more reliable because using an additional / separated server for live backup.

If the network or assigned ip address in server a is troubled or have an issue, then it will automatically routed to the second server’s network. The setting for ip addresses is same as the above, add the first server’s ip address to your domain A Record and same thing for the second server’s ip address. The files within both server is synchronized one each other with a software like rsync. Here is the way to install and run rsync.

tar -zxvf rsync-3.0.9.tar.gz
make install

Here’s one example of using rsync, you want to synchronize all of your files inside your public_html directory to the second server, and the public_html directory is located in /home/www/, so here is the command

rsync -avz /home/www/ user@second_servers_ip_or_host:/home/www/

now take a look at below command

rsync -avz /home/www/ user@second_servers_ip_or_host:/home/www/

the command is almost the same with the first command, but the second command is syncronizing / copying public_html directory and its content to the path /home/www/ of the second server.

I usually use rsync with cron daemon, the command will run either every x minute or every x hour depending on my needs, for more information about commands and manuals you can visit rsync documentation page.

The advantage of using this trick is you do not need to spend large money, especially for the first method, you only need to spend about one or two more dollars to get the second ip address assigned to your server, or double your server cost by adding one more if you want it to be more reliable. Also the setting is not too complicated, just add the nameserver / ip and install rsync. If there’s advantage, so there will be disadvantage, the dns failover depends on the end user’s dns, if end user’s dns is good then the dns shifting from the dead ip to the live ip will not be taking a long time, resolving the dns will be fast and smooth, otherwise if there’s a problem with end user’s dns, then the shifting will take more time, especially if you are implementing the second way and your both servers are located in two different areas or datacenters.

So that’s it for the simple failover (dns and static only files), we will continue later with the more advance failover. See Part 2 Here.

{ 9 comments… read them below or add one }

Leave a Comment

Previous post:

Next post: