Install Nginx and Configure Load Balancing for Heavy Traffic

Load Balancing is the mechanism which will balance the load upon server towards several servers which ultimately can handle heavy traffic. I will show you the basic configurations with normal HTML based webpages only but it will help you to build for PHP based websites.

 

Install_Nginx_and_Configure_Load_Balancing_for_Heavy_Traffic

I am going to use a main server for Load Balancer and 4 others as load balance support. We will install nginx and individually make sure we can access the website with certain port. This is actually a testing based procedure. And obviously i can’t guarantee that it will be useful to DDOS attack. It just simply transfer the load to other servers. Problem is the main server handles the first loads. You can test the efficiency of this type of balancing by Apache Benchmark.

Let,

0.0.0.0 = Master Server
1.1.1.1 = s1
2.2.2.2 = s2
3.3.3.3 = s3
4.4.4.4 = s4

Let, your domain is domain.com. Create A record respectively.

domain.com. >> 0.0.0.0
m.domain.com >> 0.0.0.0
s1.domain.com >> 1.1.1.1
s2.domain.com >> 2.2.2.2
s3.domain.com >> 3.3.3.3
s4.domain.com >> 4.4.4.4

# Install nginx in all your servers, master and slave.

apt-get update;apt-get install nginx -y

# Configure Master nginx virtualhost

Create a vhost file “domain.com”  and create folder of the root of that website.

touch /etc/nginx/sites-enabled/s1.domain.com
mkdir -p /var/www/domain.com

Put your website in /var/www/domain.com by sftp or something and change the ownership.

chown -R www-data:www-data /var/www/domain.com

And then put below configuration to the vhost.

nano /etc/nginx/sites-enabled/domain.com
upstream balancer {
 server s1.domain.com:7777 weight=1;
 server s2.domain.com:7777 weight=2;
 server s3.domain.com:7777 weight=3;
 server s4.domain.com:7777 weight=4;
 }
 server {
 listen 80;
 root /var/www/domain.com;
 index index.html index.htm index.php;
server_name domain.com;
location / {
 proxy_pass http://balancer;
 proxy_set_header Host $host;
 }
 }

Save the file and restart. Now you will not be able to access the website. To get access of your master server copy of your website you have to configure another vhost. “m.domain.com” whose domain root will be your website root.

server {
 listen 7777;
 root /var/www/domain.com;
 index index.html index.htm index.php;
 server_name m.domain.com;
 }

Now save it and restart nginx.

service nginx restart

Now create and transfer your website to all your slaves.

# Slave1

mkdir -p /var/www/s1.domain.com

Transfer the files to the folder and change ownership.

chown -R www-data:www-data /var/www/s1.domain.com

And then put below configuration to the vhost.

nano /etc/nginx/sites-enabled/s1.domain.com
server {
 listen 7777;
 root /var/www/s1.domain.com;
 index index.html index.htm index.php;
 server_name s1.domain.com;
 }

# Slave2

mkdir -p /var/www/s2.domain.com

Transfer the files to the folder and change ownership.

chown -R www-data:www-data /var/www/s2.domain.com

And then put below configuration to the vhost.

nano /etc/nginx/sites-enabled/s2.domain.com
server {
 listen 7777;
 root /var/www/s2.domain.com;
 index index.html index.htm index.php;
 server_name s2.domain.com;
 }

# Slave3

mkdir -p /var/www/s3.domain.com

Transfer the files to the folder and change ownership.

chown -R www-data:www-data /var/www/s3.domain.com

And then put below configuration to the vhost.

nano /etc/nginx/sites-enabled/s3.domain.com
server {
 listen 7777;
 root /var/www/s3.domain.com;
 index index.html index.htm index.php;
 server_name s3.domain.com;
 }

# Slave4

mkdir -p /var/www/s4.domain.com

Transfer the files to the folder and change ownership.

chown -R www-data:www-data /var/www/s4.domain.com

And then put below configuration to the vhost.

nano /etc/nginx/sites-enabled/s4.domain.com
server {
 listen 7777;
 root /var/www/s4.domain.com;
 index index.html index.htm index.php;
 server_name s4.domain.com;
 }

FINAL STEP

Make sure you have restarted nginx in all your slaves and master server.

service nginx restart

You should be able to access every slave individually by the port and then you can just use “domain.com” and you will be ok with the load balancing. You can sort out which server will be hit first and so on by “weight=#” in your main config file.

By default, requests are distributed between the servers using a weighted round-robin balancing method. In the above example, each 10 requests will be distributed as follows: 4 requests go to s4.domain.com, 3 requests go to s3.domain.com, 2 requests go to s2.domain.com and 1 request to s1.domain.com. If an error occurs during communication with a server, the request will be passed to the next server and so on until all of the functioning servers will be tried. If a successful response could not be obtained from any of the servers, the client will receive the result of the communication with the last server.

Hope this works. It is just a fun project or setup i did with one of my website. If it looks good or useless or you face problem then please leave feedback. Thank You. 🙂