0% found this document useful (0 votes)
42 views4 pages

NGINX HTTP Load Balancing Methods

Nginx can be used as an efficient HTTP load balancer to distribute traffic across multiple application servers. It supports several load balancing methods including round-robin, least-connected, and IP hash. Round-robin is the default which distributes requests evenly. Least-connected sends requests to less busy servers. IP hash ensures a client's requests always go to the same server. Server weights can also be configured to influence load distribution. Nginx performs health checks and avoids failing servers.

Uploaded by

HallBrenton
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
42 views4 pages

NGINX HTTP Load Balancing Methods

Nginx can be used as an efficient HTTP load balancer to distribute traffic across multiple application servers. It supports several load balancing methods including round-robin, least-connected, and IP hash. Round-robin is the default which distributes requests evenly. Least-connected sends requests to less busy servers. IP hash ensures a client's requests always go to the same server. Server weights can also be configured to influence load distribution. Nginx performs health checks and avoids failing servers.

Uploaded by

HallBrenton
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

2021/8/22 Using nginx as HTTP load balancer

NGINX Sprint is on Aug 23-25 this year, virtual and free.


Register here.

Using nginx as HTTP load balancer


Load balancing methods english

Default load balancing configuration


Least connected load balancing
русский

Session persistence

Weighted load balancing news


Health checks
about

Further reading
download


security

documentation
Introduction faq

Load balancing across multiple application instances is a

books
commonly used
technique for optimizing resource utilization, support

maximizing throughput,
reducing latency, and ensuring fault-

tolerant configurations. trac


twitter
It is possible to use nginx as a very efficient HTTP load blog

balancer to
distribute traffic to several application servers and

to improve
performance, scalability and reliability of web unit
applications with nginx. njs

Load balancing methods


The following load balancing mechanisms (or methods) are
supported in
nginx:
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).
Default load balancing configuration
The simplest configuration for load balancing with nginx may
look
like the following:
http {

upstream myapp1 {

server [Link];

server [Link];

server [Link];

[Link] 1/4
2021/8/22 Using nginx as HTTP load balancer
}

server {

listen 80;

location / {

proxy_pass [Link]

}
}

In the example above, there are 3 instances of the same


application
running on srv1-srv3.
When the load balancing
method is not specifically configured,
it defaults to round-
robin.
All requests are
proxied to the server group myapp1, and
nginx applies HTTP load
balancing to distribute the requests.
Reverse proxy implementation in nginx includes load balancing
for HTTP,
HTTPS, FastCGI, uwsgi, SCGI, memcached, and
gRPC.
To configure load balancing for HTTPS instead of HTTP, just
use “https”
as the protocol.
When setting up load balancing for FastCGI, uwsgi, SCGI,
memcached, or gRPC, use
fastcgi_pass,
uwsgi_pass,
scgi_pass,
memcached_pass, and
grpc_pass
directives
respectively.
Least connected load balancing
Another load balancing discipline is least-connected.
Least-
connected allows controlling the load on application
instances
more fairly in a situation when some of the requests
take
longer to complete.
With the least-connected load balancing, nginx will try not to
overload a
busy application server with excessive requests,
distributing the new
requests to a less busy server instead.
Least-connected load balancing in nginx is activated when the
least_conn directive is used as part of the server group
configuration:
upstream myapp1 {

least_conn;

server [Link];

server [Link];

server [Link];

Session persistence
[Link] 2/4
2021/8/22 Using nginx as HTTP load balancer

Please note that with round-robin or least-connected load


balancing, each subsequent client’s request can be potentially
distributed to a different server.
There is no guarantee that the
same client will be always
directed to the same server.
If there is the need to tie a client to a particular application
server —
in other words, make the client’s session “sticky” or
“persistent” in
terms of always trying to select a particular
server — the ip-hash load
balancing mechanism can be used.
With ip-hash, the client’s IP address is used as a hashing key
to
determine what server in a server group should be selected
for the
client’s requests.
This method ensures that the requests
from the same client
will always be directed to the same server
except when this server is unavailable.
To configure ip-hash load balancing, just add the
ip_hash
directive to the server (upstream) group configuration:
upstream myapp1 {

ip_hash;

server [Link];

server [Link];

server [Link];

Weighted load balancing


It is also possible to influence nginx load balancing algorithms
even
further by using server weights.
In the examples above, the server weights are not configured
which means
that all specified servers are treated as equally
qualified for a
particular load balancing method.
With the round-robin in particular it also means a more or less
equal
distribution of requests across the servers — provided
there are enough
requests, and when the requests are
processed in a uniform manner and
completed fast enough.
When the
weight
parameter is specified for a server, the weight
is accounted as part
of the load balancing decision.
upstream myapp1 {

server [Link] weight=3;

server [Link];

server [Link];

With this configuration, every 5 new requests will be distributed


across
the application instances as the following: 3 requests
[Link] 3/4
2021/8/22 Using nginx as HTTP load balancer

will be directed
to srv1, one request will go to srv2, and another
one — to srv3.
It is similarly possible to use weights with the least-connected
and
ip-hash load balancing in the recent versions of nginx.
Health checks
Reverse proxy implementation in nginx includes in-band (or
passive)
server health checks.
If the response from a particular
server fails with an error,
nginx will mark this server as failed,
and will try to
avoid selecting this server for subsequent
inbound requests for a while.
The
max_fails
directive sets the number of consecutive
unsuccessful attempts to
communicate with the server that
should happen during
fail_timeout.
By default,
max_fails
is set
to 1.
When it is set to 0, health checks are disabled for this
server.
The
fail_timeout
parameter also defines how long the
server will be marked as failed.
After
fail_timeout
interval
following the server failure, nginx will start to gracefully
probe
the server with the live client’s requests.
If the probes have
been successful, the server is marked as a live one.
Further reading
In addition, there are more directives and parameters that
control server
load balancing in nginx, e.g.
proxy_next_upstream,
backup,
down, and
keepalive.
For more
information please check our
reference documentation.
Last but not least,
application load balancing,
application
health checks,
activity monitoring and
on-the-fly
reconfiguration of server groups are available
as part of our
paid NGINX Plus subscriptions.
The following articles describe load balancing with NGINX Plus
in more detail:
Load Balancing with NGINX and NGINX Plus
Load Balancing with NGINX and NGINX Plus part 2

[Link] 4/4

Common questions

Powered by AI

Health checks in NGINX can be enhanced by incorporating more comprehensive failure detection strategies, such as incorporating both passive and active health checks. Active health checks could probe server health independently of client requests, verifying server responsiveness and functionality at regular intervals. Furthermore, integrating advanced anomaly detection algorithms can predict potential failure points based on performance metrics. Implementing a more granular failover strategy, employing techniques like circuit breaking, could further enhance robustness and reliability by dynamically rerouting traffic based on real-time server health data .

NGINX handles server failures using in-band or passive health checks. If a server responds with an error, it is marked as failed, and NGINX avoids sending requests to it for a defined fail_timeout period. The max_fails directive specifies how many consecutive errors trigger this response, with a default of 1. Post-fail_timeout, NGINX starts gradually probing the server with client requests until it successfully confirms the server is operational, thus ensuring minimal disruption to service availability .

NGINX uses the round-robin method to distribute requests evenly across all configured servers, sending each subsequent request to the next server in line. The least-connected method directs requests to the server with the fewest active connections. However, both methods lack session persistence, meaning that subsequent requests from the same client might be directed to different servers. This can be problematic when sessions need to be tied to a particular server for stateful operations. To achieve session persistence, NGINX employs the ip-hash method, which consistently routes requests from the same client to the same server based on the client’s IP address .

To handle sudden bursts of traffic, NGINX can employ a combination of load balancing strategies such as weighted round-robin and least-connected methods to distribute the load effectively. Implementing server weights allows NGINX to direct more traffic to stronger servers during spikes. Additionally, employing caching mechanisms can offload servers by serving static responses more quickly, while rate limiting can control the burst by temporarily slowing down requests from high-demand clients to maintain overall system balance. These strategies work in tandem to ensure each server maintains consistent performance .

NGINX's configuration flexibility allows seamless management of various protocols by supporting different directives tailored to each protocol. For instance, HTTP/HTTPS load balancing uses the proxy_pass directive, while FastCGI, uwsgi, SCGI, and memcached require fastcgi_pass, uwsgi_pass, scgi_pass, and memcached_pass directives respectively. This flexibility ensures that NGINX can effectively handle different communication protocols according to their specific requirements and maintain high performance across diverse application environments .

NGINX Plus offers advanced features over standard NGINX, such as application health checks, activity monitoring, and on-the-fly reconfiguration of server groups, which improve operational efficiency. These enhancements provide real-time monitoring and detailed insights into server performance and health, allowing for quicker issue resolution and more effective traffic management. On-the-fly reconfigurations enable administrators to adjust server group configurations without service interruptions, enhancing agility and responsiveness to changing traffic demands .

The ip-hash method in NGINX uses the client's IP address to hash and direct all requests from that client to the same server, ensuring consistent session persistence. This method is advantageous in scenarios where maintaining the state of a session is critical, such as in applications requiring user-specific data retention across multiple requests. Unlike round-robin and least-connected, which could distribute successive requests to different servers, ip-hash ensures continuity of experience by keeping the session tied to a specific server .

In NGINX's load balancing mechanisms, weights influence how requests are distributed across servers. By assigning higher weights to more capable servers, NGINX directs proportionally more traffic to them. For instance, configuring a server with a weight of 3 will result in it receiving three times the number of requests compared to a server with a weight of 1. This approach can optimize overall server performance and resource utilization by leveraging the capacity of more robust servers, ensuring that they handle a larger share of the load .

Session persistence in load balancing ensures that all requests from a single client session are consistently directed to the same server. This can be particularly beneficial in maintaining stateful sessions required for applications that depend on session data. However, potential drawbacks include the risk of uneven load distribution if certain clients generate significantly more traffic than others, potentially leading to an overburdened server. Furthermore, if the chosen server becomes unavailable, session data might become inaccessible unless redundancy measures like data replication are in place .

NGINX implements session persistence using the ip-hash method by using the client's IP address to decide which server should handle the client's requests. This ensures that all requests from a particular client are directed to the same server, thus maintaining session consistency. While this method is effective for applications requiring persistence, it might impact load balancing efficiency as it could lead to uneven traffic distribution, especially if many clients share similar IP ranges or if a particular server that handles a large number of IP addresses becomes overwhelmed .

You might also like