Mercurial > hg > nginx-site
annotate xml/en/docs/http/load_balancing.xml @ 1652:68b647a96448
Documented that "map" does a caseless string comparison.
author | Ruslan Ermilov <ru@nginx.com> |
---|---|
date | Thu, 11 Feb 2016 12:32:04 +0300 |
parents | f9a138e5ab14 |
children | 4cafd82e5007 |
rev | line source |
---|---|
1076 | 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" | |
1527
f9a138e5ab14
Updated links to nginx.com website.
Valentin Bartenev <vbart@nginx.com>
parents:
1194
diff
changeset
|
12 rev="4"> |
1076 | 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, and memcached. | |
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> | |
1181 | 107 When setting up load balancing for FastCGI, uwsgi, SCGI, or memcached, use |
1076 | 108 <link doc="ngx_http_fastcgi_module.xml" id="fastcgi_pass"/>, |
1194 | 109 <link doc="ngx_http_uwsgi_module.xml" id="uwsgi_pass"/>, |
1181 | 110 <link doc="ngx_http_scgi_module.xml" id="scgi_pass"/>, and |
1076 | 111 <link doc="ngx_http_memcached_module.xml" id="memcached_pass"/> |
112 directives respectively. | |
113 </para> | |
114 | |
115 </section> | |
116 | |
117 | |
118 <section id="nginx_load_balancing_with_least_connected" | |
119 name="Least connected load balancing"> | |
120 | |
121 <para> | |
122 Another load balancing discipline is least-connected. | |
123 Least-connected allows controlling the load on application | |
124 instances more fairly in a situation when some of the requests | |
125 take longer to complete. | |
126 </para> | |
127 | |
128 <para> | |
129 With the least-connected load balancing, nginx will try not to overload a | |
130 busy application server with excessive requests, distributing the new | |
131 requests to a less busy server instead. | |
132 </para> | |
133 | |
134 <para> | |
135 Least-connected load balancing in nginx is activated when the | |
136 <link doc="ngx_http_upstream_module.xml" id="least_conn"> | |
137 least_conn</link> directive is used as part of the server group configuration: | |
138 <programlisting> | |
139 upstream myapp1 { | |
140 least_conn; | |
141 server srv1.example.com; | |
142 server srv2.example.com; | |
143 server srv3.example.com; | |
144 } | |
145 </programlisting> | |
146 </para> | |
147 | |
148 </section> | |
149 | |
150 | |
151 <section id="nginx_load_balancing_with_ip_hash" | |
152 name="Session persistence"> | |
153 | |
154 <para> | |
155 Please note that with round-robin or least-connected load | |
156 balancing, each subsequent client’s request can be potentially | |
157 distributed to a different server. | |
158 There is no guarantee that the same client will be always | |
159 directed to the same server. | |
160 </para> | |
161 | |
162 <para> | |
163 If there is the need to tie a client to a particular application server — | |
164 in other words, make the client’s session “sticky” or “persistent” in | |
165 terms of always trying to select a particular server — the ip-hash load | |
166 balancing mechanism can be used. | |
167 </para> | |
168 | |
169 <para> | |
170 With ip-hash, the client’s IP address is used as a hashing key to | |
171 determine what server in a server group should be selected for the | |
172 client’s requests. | |
173 This method ensures that the requests from the same client | |
174 will always be directed to the same server | |
175 except when this server is unavailable. | |
176 </para> | |
177 | |
178 <para> | |
179 To configure ip-hash load balancing, just add the | |
180 <link doc="ngx_http_upstream_module.xml" id="ip_hash"/> | |
181 directive to the server (upstream) group configuration: | |
182 <programlisting> | |
183 upstream myapp1 { | |
184 ip_hash; | |
185 server srv1.example.com; | |
186 server srv2.example.com; | |
187 server srv3.example.com; | |
188 } | |
189 </programlisting> | |
190 </para> | |
191 | |
192 </section> | |
193 | |
194 | |
195 <section id="nginx_weighted_load_balancing" | |
196 name="Weighted load balancing"> | |
197 | |
198 <para> | |
199 It is also possible to influence nginx load balancing algorithms even | |
200 further by using server weights. | |
201 </para> | |
202 | |
203 <para> | |
204 In the examples above, the server weights are not configured which means | |
205 that all specified servers are treated as equally qualified for a | |
206 particular load balancing method. | |
207 </para> | |
208 | |
209 <para> | |
210 With the round-robin in particular it also means a more or less equal | |
211 distribution of requests across the servers — provided there are enough | |
212 requests, and when the requests are processed in a uniform manner and | |
213 completed fast enough. | |
214 </para> | |
215 | |
216 <para> | |
217 When the | |
218 <link doc="ngx_http_upstream_module.xml" id="server">weight</link> | |
219 parameter is specified for a server, the weight is accounted as part | |
220 of the load balancing decision. | |
221 <programlisting> | |
222 upstream myapp1 { | |
223 server srv1.example.com weight=3; | |
224 server srv2.example.com; | |
225 server srv3.example.com; | |
226 } | |
227 </programlisting> | |
228 </para> | |
229 | |
230 <para> | |
231 With this configuration, every 5 new requests will be distributed across | |
232 the application instances as the following: 3 requests will be directed | |
233 to srv1, one request will go to srv2, and another one — to srv3. | |
234 </para> | |
235 | |
236 <para> | |
237 It is similarly possible to use weights with the least-connected and | |
238 ip-hash load balancing in the recent versions of nginx. | |
239 </para> | |
240 | |
241 </section> | |
242 | |
243 | |
244 <section id="nginx_load_balancing_health_checks" | |
245 name="Health checks"> | |
246 | |
247 <para> | |
248 Reverse proxy implementation in nginx includes in-band (or passive) | |
249 server health checks. | |
250 If the response from a particular server fails with an error, | |
251 nginx will mark this server as failed, and will try to | |
252 avoid selecting this server for subsequent inbound requests for a while. | |
253 </para> | |
254 | |
255 <para> | |
256 The | |
257 <link doc="ngx_http_upstream_module.xml" id="server">max_fails</link> | |
258 directive sets the number of consecutive unsuccessful attempts to | |
259 communicate with the server that should happen during | |
260 <link doc="ngx_http_upstream_module.xml" id="server">fail_timeout</link>. | |
261 By default, | |
262 <link doc="ngx_http_upstream_module.xml" id="server">max_fails</link> | |
263 is set to 1. | |
264 When it is set to 0, health checks are disabled for this server. | |
265 The | |
266 <link doc="ngx_http_upstream_module.xml" id="server">fail_timeout</link> | |
267 parameter also defines how long the server will be marked as failed. | |
268 After | |
269 <link doc="ngx_http_upstream_module.xml" id="server">fail_timeout</link> | |
270 interval following the server failure, nginx will start to gracefully | |
271 probe the server with the live client’s requests. | |
272 If the probes have been successful, the server is marked as a live one. | |
273 </para> | |
274 | |
275 </section> | |
276 | |
277 | |
278 <section id="nginx_load_balancing_additional_information" | |
279 name="Further reading"> | |
280 | |
281 <para> | |
282 In addition, there are more directives and parameters that control server | |
283 load balancing in nginx, e.g. | |
284 <link doc="ngx_http_proxy_module.xml" id="proxy_next_upstream"/>, | |
285 <link doc="ngx_http_upstream_module.xml" id="server">backup</link>, | |
286 <link doc="ngx_http_upstream_module.xml" id="server">down</link>, and | |
287 <link doc="ngx_http_upstream_module.xml" id="keepalive"/>. | |
1177
e33858baaecd
Links to the nginx reference docs and lb blogposts added.
Maxim Konovalov <maxim@nginx.com>
parents:
1165
diff
changeset
|
288 For more information please check our |
1178
52747e115aba
A relative URL instead of absolute one used.
Maxim Konovalov <maxim@nginx.com>
parents:
1177
diff
changeset
|
289 <link url="..">reference documentation</link>. |
1076 | 290 </para> |
291 | |
292 <para> | |
293 Last but not least, | |
1527
f9a138e5ab14
Updated links to nginx.com website.
Valentin Bartenev <vbart@nginx.com>
parents:
1194
diff
changeset
|
294 <link url="https://www.nginx.com/products/application-load-balancing/"> |
1165
0a441212ef0f
A link to nginx.com application load balancing product page fixed.
Maxim Konovalov <maxim@nginx.com>
parents:
1076
diff
changeset
|
295 application load balancing</link>, |
1527
f9a138e5ab14
Updated links to nginx.com website.
Valentin Bartenev <vbart@nginx.com>
parents:
1194
diff
changeset
|
296 <link url="https://www.nginx.com/products/application-health-checks/"> |
1076 | 297 application health checks</link>, |
1527
f9a138e5ab14
Updated links to nginx.com website.
Valentin Bartenev <vbart@nginx.com>
parents:
1194
diff
changeset
|
298 <link url="https://www.nginx.com/products/live-activity-monitoring/"> |
1076 | 299 activity monitoring</link> and |
1527
f9a138e5ab14
Updated links to nginx.com website.
Valentin Bartenev <vbart@nginx.com>
parents:
1194
diff
changeset
|
300 <link url="https://www.nginx.com/products/on-the-fly-reconfiguration/"> |
1076 | 301 on-the-fly reconfiguration</link> of server groups are available |
302 as part of our paid NGINX Plus subscriptions. | |
303 </para> | |
304 | |
1177
e33858baaecd
Links to the nginx reference docs and lb blogposts added.
Maxim Konovalov <maxim@nginx.com>
parents:
1165
diff
changeset
|
305 <para> |
e33858baaecd
Links to the nginx reference docs and lb blogposts added.
Maxim Konovalov <maxim@nginx.com>
parents:
1165
diff
changeset
|
306 The following articles describe load balancing with NGINX Plus |
e33858baaecd
Links to the nginx reference docs and lb blogposts added.
Maxim Konovalov <maxim@nginx.com>
parents:
1165
diff
changeset
|
307 in more detail: |
e33858baaecd
Links to the nginx reference docs and lb blogposts added.
Maxim Konovalov <maxim@nginx.com>
parents:
1165
diff
changeset
|
308 <list type="bullet" compact="no"> |
e33858baaecd
Links to the nginx reference docs and lb blogposts added.
Maxim Konovalov <maxim@nginx.com>
parents:
1165
diff
changeset
|
309 |
e33858baaecd
Links to the nginx reference docs and lb blogposts added.
Maxim Konovalov <maxim@nginx.com>
parents:
1165
diff
changeset
|
310 <listitem> |
1527
f9a138e5ab14
Updated links to nginx.com website.
Valentin Bartenev <vbart@nginx.com>
parents:
1194
diff
changeset
|
311 <link url="https://www.nginx.com/blog/load-balancing-with-nginx-plus/"> |
1177
e33858baaecd
Links to the nginx reference docs and lb blogposts added.
Maxim Konovalov <maxim@nginx.com>
parents:
1165
diff
changeset
|
312 Load Balancing with NGINX and NGINX Plus</link> |
e33858baaecd
Links to the nginx reference docs and lb blogposts added.
Maxim Konovalov <maxim@nginx.com>
parents:
1165
diff
changeset
|
313 </listitem> |
e33858baaecd
Links to the nginx reference docs and lb blogposts added.
Maxim Konovalov <maxim@nginx.com>
parents:
1165
diff
changeset
|
314 |
e33858baaecd
Links to the nginx reference docs and lb blogposts added.
Maxim Konovalov <maxim@nginx.com>
parents:
1165
diff
changeset
|
315 <listitem> |
1527
f9a138e5ab14
Updated links to nginx.com website.
Valentin Bartenev <vbart@nginx.com>
parents:
1194
diff
changeset
|
316 <link url="https://www.nginx.com/blog/load-balancing-with-nginx-plus-part2/"> |
1177
e33858baaecd
Links to the nginx reference docs and lb blogposts added.
Maxim Konovalov <maxim@nginx.com>
parents:
1165
diff
changeset
|
317 Load Balancing with NGINX and NGINX Plus part 2</link> |
e33858baaecd
Links to the nginx reference docs and lb blogposts added.
Maxim Konovalov <maxim@nginx.com>
parents:
1165
diff
changeset
|
318 </listitem> |
e33858baaecd
Links to the nginx reference docs and lb blogposts added.
Maxim Konovalov <maxim@nginx.com>
parents:
1165
diff
changeset
|
319 |
e33858baaecd
Links to the nginx reference docs and lb blogposts added.
Maxim Konovalov <maxim@nginx.com>
parents:
1165
diff
changeset
|
320 </list> |
e33858baaecd
Links to the nginx reference docs and lb blogposts added.
Maxim Konovalov <maxim@nginx.com>
parents:
1165
diff
changeset
|
321 </para> |
e33858baaecd
Links to the nginx reference docs and lb blogposts added.
Maxim Konovalov <maxim@nginx.com>
parents:
1165
diff
changeset
|
322 |
1076 | 323 </section> |
324 | |
325 </article> |