Tuesday 11 August 2015

simple Load balancer using Nginx web server

Nginx Load balancing on linux ubuntu 14.04

Loadbalancer(lb):192.168.0.10(Nginx)
webserver(web1):192.168.0.11(apache)
webserver(web2):192.168.0.12(apache)
webserver(web3):192.168.0.13(apache)
database server(db):192.168.0.14(mysql)

note:we can increase web server for load balancing.

1.Install nginx webserver on Loadbalancer(lb)

apt-get install nginx

2.Edit the nginx default file.

 nano /etc/nginx/nginx.conf

note:you can use any text editor vi or pico.

events {
        worker_connections  1024;
        use                 epoll;
        multi_accept        on;
}

upstream backend  {
  server 192.168.0.11;
  server 192.168.0.12;
  server 192.168.0.13;
}

server {
  location / {
    proxy_pass  http://backend;
  }
}

3.Restart nginx webserver

sudo service nginx restart

4.Install apache webserver on web1, web2 and web3.

apt-get install apache2


5.Goto var/www/html folder of webserver respectively and remove index.html .
add new index.html and write web1 for web1 apache server.

6.Install mysql server on db

apt-get install mysql-server


7.To allow mysql accessible to all webserver run bellow command.


$ mysql -u root -p
Enter password:


mysql> GRANT ALL ON *.* to root@'192.168.0.11' IDENTIFIED BY 'your-root-password';


mysql> GRANT ALL ON *.* to root@'192.168.0.12' IDENTIFIED BY 'your-root-password';


mysql> GRANT ALL ON *.* to root@'192.168.0.13' IDENTIFIED BY 'your-root-password';


Note:I didnot add mysql server in above example but you can add .


8.Open your browser goto url http://192.168.0.10 and refresh the page.

you will see whenever you refresh the page it will show you from which webserver your page is loaded.(because we have added html file in step 5). It will distribute web request equally across the apache Webserver.


Nginx also have diffrent type of load balancing:

1.Session persistence

2.Weighted load balancing

3.Max Fails, etc.


for more:http://nginx.org/en/docs/http/load_balancing.html

No comments:

Post a Comment