Computersnyou

How to use NGINX as LoadBlancer

Posted on  4/6/2014

LoadBalancer

Loadbancing is a technique of load distribution , means load balancer distribute all incoming traffic to all available upstream servers .

LoadBlancer Flow
LoadBlancer Flow

Nginx is small but smart web server which is also capable of working as LoadBlancer .In this scenario i will use nginx as front facing web server and distribute all traffic between two server . they both server 1 and server 2 use common database behind the scene .

steps

Nginx Installation On all instances are similar . install as normal package configure your back-end servers according your requirements .

First Lets Install Nginx

sudo apt-get install python-software-properties
sudo apt-get install software-properties-common
sudo apt-add-repository ppa:nginx/stable
sudo apt-get update 
sudo apt-get install nginx -y

now nginx is installed start nginx service and verify that is running .

sudo /etc/init.d/nginx start

Now open your browser and type that server ip address bar . you will see the default nginx page .

Nginx Default Page
Nginx Default Page

 

 

Configuration

edit sites-available/default ( or whatever config your are using )

upstream app_server {
     ip_hash;
    server app1.example.com        fail_timeout=5s weight=5 max_fails=3;
    server app2.example.com:8080   fail_timeout=10s slow_start=5s; 
    server app3.example.com:8080   backup;
    server app4.example.com:8080   backup;
    server app5.example.com:8080   down;
}

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

weight = weight of the server , by default its 1
max_fails= number of unsuccessful connection attempt by default its 1
fail_timeout= time for making connection and marking them as unavailable by default 10 seconds
backup = mark that upstream server as backup server
down = mark that as down
ip_hash = Its ensure requests from the same client will always be passed to the same server unless its unavailable or down .
max_conns= number of maximum connection allowed at a time .
slow_start = its time while server get weight and accepting connection .

 

how to check nginx config is working

sudo nginx -t

  • Home
  • About