Javatpoint Logo
Javatpoint Logo

HTTP Load Balancing

Proxying HTTP Traffic to a Group of Servers

Before start using Nginx open source or Nginx Plus server to load balance HTTP traffic to a group of servers, first we need to define the group with the upstream directive. The directive is placed in the http context.

Servers in the group are configured using the directive. Let's see an example, the following configuration defines a group named backend and consists of three server configurations which may resolve in more than three actual servers.

To pass the requests to a server group, the group name is specified in the proxy_pass directive. In the below example, a virtual server running on Nginx, passes all requests to the backend upstream group.

The following example combines the above two snippets and shows how to proxy HTTP request to the backend server group. The group consists of three servers, two of them instances of the same application while the third is a backup server. Because, there is no load-balancing algorithm is specified in the upstream block, Nginx uses the default algorithm, Round Robin.

Choosing a Load-Balancing Method

Nginx open source supports four methods for load-balancing, and Nginx Plus adds two more methods:

1. Round Robin: In this method, requests are distributed equally across the servers, with server weights taken into consideration. There is no directive for enabling it; this method is used by default.

2. Least Connections: A request is sent to the server with the least number of active connections, with server weights taken into considerations.

3. IP Hash: This method is used to determine which server should be selected for the next request. In this type of case, either the first three octets of the IPv4 address or the whole IPv6 address are used to calculate the hash value.

If one of the servers needs to be temporarily removed from the load-balancing rotation, it can be added with the down parameter in order to preserve the current hashing of client IP addresses.

4. Generic Hash: The server to which a request is sent is determined from a user defined key which can be a string, text, variable or a combination. For example, the key may be a paired source IP address and port, or key may be a URI as the following example:

Nginx Plus supports two more methods:

5. Least Time: For each and every request Nginx selects the server with the lowest average latency and the lowest number of active connections. Here, the lowest average latency is calculated based on the least_time directive parameters.

  • header: Time to receive the 1st byte from the server.
  • last_byte: Time to accept the full response from the server.
  • last_byte inflight: Time to receive the full response from the server, taking into account incomplete requests.

Next TopicNGINX Directives

Please Share

facebook twitter google plus pinterest

Learn Latest Tutorials


Trending Technologies

B.Tech / MCA