Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
nginx
GitHub Repository: nginx/nginx.org
Path: blob/main/xml/en/docs/http/load_balancing.xml
1 views
1
<?xml version="1.0"?>
2
3
<!--
4
Copyright (C) Nginx, Inc.
5
-->
6
7
<!DOCTYPE article SYSTEM "../../../../dtd/article.dtd">
8
9
<article name="Using nginx as HTTP load balancer"
10
link="/en/docs/http/load_balancing.html"
11
lang="en"
12
rev="5">
13
14
<section name="Introduction">
15
16
<para>
17
Load balancing across multiple application instances is a commonly used
18
technique for optimizing resource utilization, maximizing throughput,
19
reducing latency, and ensuring fault-tolerant configurations.
20
</para>
21
22
<para>
23
It is possible to use nginx as a very efficient HTTP load balancer to
24
distribute traffic to several application servers and to improve
25
performance, scalability and reliability of web applications with nginx.
26
</para>
27
28
</section>
29
30
31
<section id="nginx_load_balancing_methods"
32
name="Load balancing methods">
33
34
<para>
35
The following load balancing mechanisms (or methods) are supported in
36
nginx:
37
<list type="bullet" compact="no">
38
39
<listitem>
40
round-robin — requests to the application servers are distributed
41
in a round-robin fashion,
42
</listitem>
43
44
<listitem>
45
least-connected — next request is assigned to the server with the
46
least number of active connections,
47
</listitem>
48
49
<listitem>
50
ip-hash — a hash-function is used to determine what server should
51
be selected for the next request (based on the client’s IP address).
52
</listitem>
53
54
</list>
55
</para>
56
57
</section>
58
59
60
<section id="nginx_load_balancing_configuration"
61
name="Default load balancing configuration">
62
63
<para>
64
The simplest configuration for load balancing with nginx may look
65
like the following:
66
<programlisting>
67
http {
68
upstream myapp1 {
69
server srv1.example.com;
70
server srv2.example.com;
71
server srv3.example.com;
72
}
73
74
server {
75
listen 80;
76
77
location / {
78
proxy_pass http://myapp1;
79
}
80
}
81
}
82
</programlisting>
83
</para>
84
85
<para>
86
In the example above, there are 3 instances of the same application
87
running on srv1-srv3.
88
When the load balancing method is not specifically configured,
89
it defaults to round-robin.
90
All requests are
91
<link doc="ngx_http_proxy_module.xml" id="proxy_pass">
92
proxied</link> to the server group myapp1, and nginx applies HTTP load
93
balancing to distribute the requests.
94
</para>
95
96
<para>
97
Reverse proxy implementation in nginx includes load balancing for HTTP,
98
HTTPS, FastCGI, uwsgi, SCGI, memcached, and gRPC.
99
</para>
100
101
<para>
102
To configure load balancing for HTTPS instead of HTTP, just use “https”
103
as the protocol.
104
</para>
105
106
<para>
107
When setting up load balancing for FastCGI, uwsgi, SCGI, memcached, or gRPC, use
108
<link doc="ngx_http_fastcgi_module.xml" id="fastcgi_pass"/>,
109
<link doc="ngx_http_uwsgi_module.xml" id="uwsgi_pass"/>,
110
<link doc="ngx_http_scgi_module.xml" id="scgi_pass"/>,
111
<link doc="ngx_http_memcached_module.xml" id="memcached_pass"/>, and
112
<link doc="ngx_http_grpc_module.xml" id="grpc_pass"/>
113
directives respectively.
114
</para>
115
116
</section>
117
118
119
<section id="nginx_load_balancing_with_least_connected"
120
name="Least connected load balancing">
121
122
<para>
123
Another load balancing discipline is least-connected.
124
Least-connected allows controlling the load on application
125
instances more fairly in a situation when some of the requests
126
take longer to complete.
127
</para>
128
129
<para>
130
With the least-connected load balancing, nginx will try not to overload a
131
busy application server with excessive requests, distributing the new
132
requests to a less busy server instead.
133
</para>
134
135
<para>
136
Least-connected load balancing in nginx is activated when the
137
<link doc="ngx_http_upstream_module.xml" id="least_conn">
138
least_conn</link> directive is used as part of the server group configuration:
139
<programlisting>
140
upstream myapp1 {
141
least_conn;
142
server srv1.example.com;
143
server srv2.example.com;
144
server srv3.example.com;
145
}
146
</programlisting>
147
</para>
148
149
</section>
150
151
152
<section id="nginx_load_balancing_with_least_time"
153
name="Least time load balancing">
154
155
<para>
156
Another load balancing discipline is least-time.
157
Least-time selects the server with the lowest average response time
158
and the lowest number of active connections, taking into account
159
inflight requests.
160
</para>
161
162
<para>
163
With the least-time load balancing, nginx selects the server based on
164
which of the following parameters is specified in the
165
<link doc="ngx_http_upstream_module.xml" id="least_time">
166
least_time</link> directive:
167
<list type="bullet" compact="no">
168
169
<listitem>
170
<literal>header</literal> — time to receive the first byte from the server,
171
</listitem>
172
173
<listitem>
174
<literal>last_byte</literal> — time to receive the full response from the server,
175
</listitem>
176
177
<listitem>
178
<literal>last_byte inflight</literal> — time to receive the full response from
179
the server, taking into account incomplete requests.
180
</listitem>
181
182
</list>
183
</para>
184
185
<para>
186
Least-time load balancing in nginx is activated when the
187
<link doc="ngx_http_upstream_module.xml" id="least_time">
188
least_time</link> directive is used as part of the server group configuration:
189
<programlisting>
190
upstream myapp1 {
191
least_time header;
192
server srv1.example.com;
193
server srv2.example.com;
194
server srv3.example.com;
195
}
196
</programlisting>
197
</para>
198
199
</section>
200
201
202
<section id="nginx_load_balancing_with_ip_hash"
203
name="Session persistence">
204
205
<para>
206
Please note that with round-robin or least-connected load
207
balancing, each subsequent client’s request can be potentially
208
distributed to a different server.
209
There is no guarantee that the same client will be always
210
directed to the same server.
211
</para>
212
213
<para>
214
If there is the need to tie a client to a particular application server —
215
in other words, make the client’s session “sticky” or “persistent” in
216
terms of always trying to select a particular server — the ip-hash load
217
balancing mechanism can be used.
218
</para>
219
220
<para>
221
With ip-hash, the client’s IP address is used as a hashing key to
222
determine what server in a server group should be selected for the
223
client’s requests.
224
This method ensures that the requests from the same client
225
will always be directed to the same server
226
except when this server is unavailable.
227
</para>
228
229
<para>
230
To configure ip-hash load balancing, just add the
231
<link doc="ngx_http_upstream_module.xml" id="ip_hash"/>
232
directive to the server (upstream) group configuration:
233
<programlisting>
234
upstream myapp1 {
235
ip_hash;
236
server srv1.example.com;
237
server srv2.example.com;
238
server srv3.example.com;
239
}
240
</programlisting>
241
</para>
242
243
</section>
244
245
246
<section id="nginx_weighted_load_balancing"
247
name="Weighted load balancing">
248
249
<para>
250
It is also possible to influence nginx load balancing algorithms even
251
further by using server weights.
252
</para>
253
254
<para>
255
In the examples above, the server weights are not configured which means
256
that all specified servers are treated as equally qualified for a
257
particular load balancing method.
258
</para>
259
260
<para>
261
With the round-robin in particular it also means a more or less equal
262
distribution of requests across the servers — provided there are enough
263
requests, and when the requests are processed in a uniform manner and
264
completed fast enough.
265
</para>
266
267
<para>
268
When the
269
<link doc="ngx_http_upstream_module.xml" id="server">weight</link>
270
parameter is specified for a server, the weight is accounted as part
271
of the load balancing decision.
272
<programlisting>
273
upstream myapp1 {
274
server srv1.example.com weight=3;
275
server srv2.example.com;
276
server srv3.example.com;
277
}
278
</programlisting>
279
</para>
280
281
<para>
282
With this configuration, every 5 new requests will be distributed across
283
the application instances as the following: 3 requests will be directed
284
to srv1, one request will go to srv2, and another one — to srv3.
285
</para>
286
287
<para>
288
It is similarly possible to use weights with the least-connected and
289
ip-hash load balancing in the recent versions of nginx.
290
</para>
291
292
</section>
293
294
295
<section id="nginx_load_balancing_health_checks"
296
name="Health checks">
297
298
<para>
299
Reverse proxy implementation in nginx includes in-band (or passive)
300
server health checks.
301
If the response from a particular server fails with an error,
302
nginx will mark this server as failed, and will try to
303
avoid selecting this server for subsequent inbound requests for a while.
304
</para>
305
306
<para>
307
The
308
<link doc="ngx_http_upstream_module.xml" id="server">max_fails</link>
309
directive sets the number of consecutive unsuccessful attempts to
310
communicate with the server that should happen during
311
<link doc="ngx_http_upstream_module.xml" id="server">fail_timeout</link>.
312
By default,
313
<link doc="ngx_http_upstream_module.xml" id="server">max_fails</link>
314
is set to 1.
315
When it is set to 0, health checks are disabled for this server.
316
The
317
<link doc="ngx_http_upstream_module.xml" id="server">fail_timeout</link>
318
parameter also defines how long the server will be marked as failed.
319
After
320
<link doc="ngx_http_upstream_module.xml" id="server">fail_timeout</link>
321
interval following the server failure, nginx will start to gracefully
322
probe the server with the live client’s requests.
323
If the probes have been successful, the server is marked as a live one.
324
</para>
325
326
</section>
327
328
329
<section id="nginx_load_balancing_additional_information"
330
name="Further reading">
331
332
<para>
333
In addition, there are more directives and parameters that control server
334
load balancing in nginx, e.g.
335
<link doc="ngx_http_proxy_module.xml" id="proxy_next_upstream"/>,
336
<link doc="ngx_http_upstream_module.xml" id="server">backup</link>,
337
<link doc="ngx_http_upstream_module.xml" id="server">down</link>, and
338
<link doc="ngx_http_upstream_module.xml" id="keepalive"/>.
339
For more information please check our
340
<link url="..">reference documentation</link>.
341
</para>
342
343
<para>
344
Last but not least,
345
application load balancing,
346
application health checks,
347
activity monitoring and
348
on-the-fly reconfiguration of server groups are available
349
as part of our paid NGINX Plus subscriptions.
350
</para>
351
352
</section>
353
354
</article>
355
356