Install Nginx and Configure Load Balancing for Heavy Traffic
By Fahad Ahammed
- 3 minutes read - 523 wordsLoad 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.
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,
Let, your domain is domain.com. Create A record respectively.
# Install nginx in all your servers, master and slave.
# Configure Master nginx virtualhost
Create a vhost file “domain.com” and create folder of the root of that website.
Put your website in /var/www/domain.com by sftp or something and change the ownership.
And then put below configuration to the vhost.
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.
Now save it and restart nginx.
Now create and transfer your website to all your slaves.
# Slave1
Transfer the files to the folder and change ownership.
And then put below configuration to the vhost.
# Slave2
Transfer the files to the folder and change ownership.
And then put below configuration to the vhost.
# Slave3
Transfer the files to the folder and change ownership.
And then put below configuration to the vhost.
# Slave4
Transfer the files to the folder and change ownership.
And then put below configuration to the vhost.
FINAL STEP
Make sure you have restarted nginx in all your slaves and master server.
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. 🙂