Load Balance with nginx plus

To commence utilizing NGINX Plus or NGINX Open Source to load balance HTTP traffic to a group of servers, first you require to define the group with the upstream directive.
Servers in the group are configured utilizing the [serverhttps://nginx.org/en/docs/http/ngx_http_upstream_module.html#server) directive (not to be confounded with the server block that defines a virtual server running on NGINX). For example, the following configuration defines a group denominated backend and consists of three server configurations (which may resolve in more than three genuine servers):

http {
    upstream backend {
        server backend1.example.com weight=5;
        server backend2.example.com;
        server 192.0.0.1 backup;
    }
}

The following example cumulates the two snippets above and shows how to proxy HTTP requests to the backend server group. The group consists of three servers, two of them running instances of the same application while the third is a backup server. Because no load‑balancing algorithm is designated in the upstream block, NGINX utilizes the default algorithm, Round Robin:

http {
    upstream backend {
        server backend1.example.com;
        server backend2.example.com;
        server 192.0.0.1 backup;
    }
    
    server {
        location / {
            proxy_pass http://backend;
        }
    }
}

Load-Balancing Method

1 Round Robin

upstream backend {
   # no load balancing method is specified for Round Robin
   server backend1.example.com;
   server backend2.example.com;
}

2 Least Connection

upstream backend {
    least_conn;
    server backend1.example.com;
    server backend2.example.com;
}

3 IP Hash

upstream backend {
    ip_hash;
    server backend1.example.com;
    server backend2.example.com;
}

Enable Session Persistence

Session sedulousness betokens that NGINX Plus identifies utilizer sessions and routes all requests in a given session to the same upstream server.

NGINX Plus fortifies three session assiduousness methods. The methods are set with the sticky directive. (For session assiduousness with NGINX Open Source, utilize the hash or ip_hash directive as described above .)

upstream backend {
    server backend1.example.com;
    server backend2.example.com;
    sticky cookie sid expires=1h domain=.demo.com path=/;
}

In the example, the sid parameter sets the denomination of the cookie. The optional expires parameter sets the time for the browser to keep the cookie (here, 1 hour). The optional domain parameter defines the domain for which the cookie is set, and the optional path parameter defines the path for which the cookie is set. This is the simplest session sedulousness method.

About OakML

Hi we are enthusiastic developers team, we try to serve some good idea to open source community

Leave a Reply

Your email address will not be published. Required fields are marked *

Subscribe To Our Newsletter
Enter your email to receive a weekly round-up of our best posts. Learn more!