Announcement

Collapse
No announcement yet.

How To Set Up Nginx Load Balancing

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • How To Set Up Nginx Load Balancing

    Load balancing is done to divert or distributing incoming traffic around several servers. Load balancing ensures redundancy, fault tolerance and heightened stability.

    Installing Nginx:

    Code:
     sudo apt-get install nginx
    Load balancing methods:
    • round-robin — requests to the application servers are distributed in a round-robin fashion,
    • least-connected — next request is assigned to the server with the least number of active connections,
    • ip-hash — a hash-function is used to determine what server should be selected for the next request (based on the client’s IP address).

    Let us discuss, all the load balancing methods in detail:

    Round Robin/ Default load balancing configuration:

    In Round Robin configuration, all incoming traffic is distributed between srv1, srv2 and srv3. Here, All incoming requests are proxied to the server group myapp1, and nginx applies HTTP load balancing to distribute the requests.

    Nginx implements load balancing for HTTP, HTTPS, FastCGI, uwsgi, SCGI, and memcached.

    Code:
    http {
                 upstream myapp1 {
                                server srv1.example.com;
                                server srv2.example.com;
                                server srv3.example.com;
                                             }
                    server
                            {
                            listen 80;
                            location /
                                      {
                                      proxy_pass http://myapp1;
                                       }
                              }
                        }


    Least connected load balancing

    In Least Connected load balancing, Nginx will divert the traffic to less busy server. Here, Nginx is loaded with least_conn directive.

    Code:
    upstream myapp1
                            {
                            least_conn;
                            server srv1.example.com;
                            server srv2.example.com;
                            server srv3.example.com;
                            }

    Session persistence

    In session persistence, Nginx will tie a client to a particular application server, so that it can make client session sticky or persistence. Here, ip-hash load balancing technique will be chosen.
    Code:
    upstream myapp1
                            {
                            ip_hash;
                            server srv1.example.com;
                            server srv2.example.com;
                            server srv3.example.com;
                            }
    Weighted load balancing

    In weighted load balancing, weight is counted while taking load balancing decision. For example, on every 5 request, 3 request will go to srv1 while srv 2 and srv 3 will serve

    Code:
    upstream myapp1
                            {
                            server srv1.example.com weight=3;
                            server srv2.example.com;
                            server srv3.example.com;
                            }
Working...
X