Using Nginx As HTTP Load Balancer
Using Nginx As HTTP Load Balancer
Session persistence
Further reading
download
security
documentation
Introduction faq
books
commonly used
technique for optimizing resource utilization, support
maximizing throughput,
reducing latency, and ensuring fault-
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
upstream myapp1 {
server srv1.example.com;
server srv2.example.com;
server srv3.example.com;
https://nginx.org/en/docs/http/load_balancing.html 1/4
2021/8/22 Using nginx as HTTP load balancer
}
server {
listen 80;
location / {
proxy_pass http://myapp1;
}
}
least_conn;
server srv1.example.com;
server srv2.example.com;
server srv3.example.com;
Session persistence
https://nginx.org/en/docs/http/load_balancing.html 2/4
2021/8/22 Using nginx as HTTP load balancer
ip_hash;
server srv1.example.com;
server srv2.example.com;
server srv3.example.com;
server srv2.example.com;
server srv3.example.com;
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
https://nginx.org/en/docs/http/load_balancing.html 4/4