Skip to content
Newer
Older
100644 504 lines (391 sloc) 15.2 KB
99ace64 First commit.
Evan Miller authored Oct 19, 2009
1 /*
2 * Hash a variable to choose an upstream server.
fdaa270 @evanmiller Support for servers marked "down".
authored Jun 3, 2010
3 *
99ace64 First commit.
Evan Miller authored Oct 19, 2009
4 * Copyright (C) Evan Miller
5 *
6 * This module can be distributed under the same terms as Nginx itself.
7 */
8
9
10 #include <ngx_config.h>
11 #include <ngx_core.h>
12 #include <ngx_http.h>
13
f149459 Skip "backup" servers; support health checks
Evan Miller authored Feb 29, 2012
14 #if (NGX_HTTP_HEALTHCHECK)
15 #include <ngx_http_healthcheck_module.h>
16 #endif
17
fdaa270 @evanmiller Support for servers marked "down".
authored Jun 3, 2010
18 #define ngx_bitvector_index(index) (index / (8 * sizeof(uintptr_t)))
19 #define ngx_bitvector_bit(index) ((uintptr_t) 1 << (index % (8 * sizeof(uintptr_t))))
99ace64 First commit.
Evan Miller authored Oct 19, 2009
20
21 typedef struct {
22 struct sockaddr *sockaddr;
23 socklen_t socklen;
24 ngx_str_t name;
2580b21 @evanmiller Support SSL backends
authored Aug 10, 2010
25 ngx_uint_t down;
139c385 @PiotrSikora Add weights support for the upstream servers.
PiotrSikora authored Oct 22, 2012
26 ngx_int_t weight;
f149459 Skip "backup" servers; support health checks
Evan Miller authored Feb 29, 2012
27 #if (NGX_HTTP_HEALTHCHECK)
28 ngx_int_t health_index;
29 #endif
2580b21 @evanmiller Support SSL backends
authored Aug 10, 2010
30 #if (NGX_HTTP_SSL)
31 ngx_ssl_session_t *ssl_session; /* local to a process */
32 #endif
99ace64 First commit.
Evan Miller authored Oct 19, 2009
33 } ngx_http_upstream_hash_peer_t;
34
35 typedef struct {
36 ngx_uint_t number;
139c385 @PiotrSikora Add weights support for the upstream servers.
PiotrSikora authored Oct 22, 2012
37 ngx_uint_t total_weight;
38 unsigned weighted:1;
99ace64 First commit.
Evan Miller authored Oct 19, 2009
39 ngx_http_upstream_hash_peer_t peer[0];
40 } ngx_http_upstream_hash_peers_t;
41
42 typedef struct {
43 ngx_http_upstream_hash_peers_t *peers;
434e763 @evanmiller Make the "hash" field 32 bits to save confusion.
authored Jun 3, 2010
44 uint32_t hash;
99ace64 First commit.
Evan Miller authored Oct 19, 2009
45 ngx_str_t current_key;
46 ngx_str_t original_key;
47 ngx_uint_t try_i;
48 uintptr_t tried[1];
49 } ngx_http_upstream_hash_peer_data_t;
50
51
139c385 @PiotrSikora Add weights support for the upstream servers.
PiotrSikora authored Oct 22, 2012
52 static ngx_uint_t ngx_http_upstream_get_hash_peer_index(
53 ngx_http_upstream_hash_peer_data_t *uhpd);
fdaa270 @evanmiller Support for servers marked "down".
authored Jun 3, 2010
54 static void ngx_http_upstream_hash_next_peer(ngx_http_upstream_hash_peer_data_t *uhpd,
55 ngx_uint_t *tries, ngx_log_t *log);
99ace64 First commit.
Evan Miller authored Oct 19, 2009
56 static ngx_int_t ngx_http_upstream_init_hash_peer(ngx_http_request_t *r,
57 ngx_http_upstream_srv_conf_t *us);
58 static ngx_int_t ngx_http_upstream_get_hash_peer(ngx_peer_connection_t *pc,
59 void *data);
60 static void ngx_http_upstream_free_hash_peer(ngx_peer_connection_t *pc,
61 void *data, ngx_uint_t state);
2580b21 @evanmiller Support SSL backends
authored Aug 10, 2010
62 #if (NGX_HTTP_SSL)
63 static ngx_int_t ngx_http_upstream_set_hash_peer_session(ngx_peer_connection_t *pc,
64 void *data);
65 static void ngx_http_upstream_save_hash_peer_session(ngx_peer_connection_t *pc,
66 void *data);
67 #endif
99ace64 First commit.
Evan Miller authored Oct 19, 2009
68 static char *ngx_http_upstream_hash(ngx_conf_t *cf, ngx_command_t *cmd,
69 void *conf);
fdaa270 @evanmiller Support for servers marked "down".
authored Jun 3, 2010
70 static char *ngx_http_upstream_hash_again(ngx_conf_t *cf, ngx_command_t *cmd,
99ace64 First commit.
Evan Miller authored Oct 19, 2009
71 void *conf);
fdaa270 @evanmiller Support for servers marked "down".
authored Jun 3, 2010
72 static ngx_int_t ngx_http_upstream_init_hash(ngx_conf_t *cf,
99ace64 First commit.
Evan Miller authored Oct 19, 2009
73 ngx_http_upstream_srv_conf_t *us);
74 static ngx_uint_t ngx_http_upstream_hash_crc32(u_char *keydata, size_t keylen);
75
76 static ngx_command_t ngx_http_upstream_hash_commands[] = {
77 { ngx_string("hash"),
78 NGX_HTTP_UPS_CONF|NGX_CONF_TAKE1,
79 ngx_http_upstream_hash,
80 0,
81 0,
82 NULL },
83
84 { ngx_string("hash_again"),
85 NGX_HTTP_UPS_CONF|NGX_CONF_TAKE1,
86 ngx_http_upstream_hash_again,
87 0,
88 0,
89 NULL },
90
91 ngx_null_command
92 };
93
94
95 static ngx_http_module_t ngx_http_upstream_hash_module_ctx = {
96 NULL, /* preconfiguration */
97 NULL, /* postconfiguration */
98
99 NULL, /* create main configuration */
100 NULL, /* init main configuration */
101
102 NULL, /* create server configuration */
103 NULL, /* merge server configuration */
104
105 NULL, /* create location configuration */
106 NULL /* merge location configuration */
107 };
108
109
110 ngx_module_t ngx_http_upstream_hash_module = {
111 NGX_MODULE_V1,
112 &ngx_http_upstream_hash_module_ctx, /* module context */
113 ngx_http_upstream_hash_commands, /* module directives */
114 NGX_HTTP_MODULE, /* module type */
115 NULL, /* init master */
116 NULL, /* init module */
117 NULL, /* init process */
118 NULL, /* init thread */
119 NULL, /* exit thread */
120 NULL, /* exit process */
121 NULL, /* exit master */
122 NGX_MODULE_V1_PADDING
123 };
124
125
139c385 @PiotrSikora Add weights support for the upstream servers.
PiotrSikora authored Oct 22, 2012
126 static ngx_uint_t
127 ngx_http_upstream_get_hash_peer_index(ngx_http_upstream_hash_peer_data_t *uhpd)
128 {
129 ngx_int_t w;
130 ngx_uint_t i;
131
132 if (!uhpd->peers->weighted) {
133 return uhpd->hash % uhpd->peers->number;
134 }
135
136 w = uhpd->hash % uhpd->peers->total_weight;
137
138 for (i = 0; i < uhpd->peers->number; i++) {
139 w -= uhpd->peers->peer[i].weight;
140 if (w < 0) {
141 return i;
142 }
143 }
144
145 /* unreachable */
146
147 return 0;
148 }
149
150
99ace64 First commit.
Evan Miller authored Oct 19, 2009
151 static ngx_int_t
152 ngx_http_upstream_init_hash(ngx_conf_t *cf, ngx_http_upstream_srv_conf_t *us)
153 {
139c385 @PiotrSikora Add weights support for the upstream servers.
PiotrSikora authored Oct 22, 2012
154 ngx_uint_t i, j, n, w;
99ace64 First commit.
Evan Miller authored Oct 19, 2009
155 ngx_http_upstream_server_t *server;
156 ngx_http_upstream_hash_peers_t *peers;
f149459 Skip "backup" servers; support health checks
Evan Miller authored Feb 29, 2012
157 #if (NGX_HTTP_HEALTHCHECK)
158 ngx_int_t health_index;
159 #endif
99ace64 First commit.
Evan Miller authored Oct 19, 2009
160 us->peer.init = ngx_http_upstream_init_hash_peer;
161
162 if (!us->servers) {
163 return NGX_ERROR;
164 }
165
166 server = us->servers->elts;
167
f0ae839 Fix spin-lock when all servers are down
Evan Miller authored Apr 24, 2012
168 n = 0;
139c385 @PiotrSikora Add weights support for the upstream servers.
PiotrSikora authored Oct 22, 2012
169 w = 0;
f0ae839 Fix spin-lock when all servers are down
Evan Miller authored Apr 25, 2012
170 for (i = 0; i < us->servers->nelts; i++) {
171 if (server[i].backup)
172 continue;
173
99ace64 First commit.
Evan Miller authored Oct 19, 2009
174 n += server[i].naddrs;
139c385 @PiotrSikora Add weights support for the upstream servers.
PiotrSikora authored Oct 22, 2012
175 w += server[i].naddrs * server[i].weight;
99ace64 First commit.
Evan Miller authored Oct 19, 2009
176 }
177
f0ae839 Fix spin-lock when all servers are down
Evan Miller authored Apr 25, 2012
178 if (n == 0) {
179 return NGX_ERROR;
180 }
181
99ace64 First commit.
Evan Miller authored Oct 19, 2009
182 peers = ngx_pcalloc(cf->pool, sizeof(ngx_http_upstream_hash_peers_t)
183 + sizeof(ngx_http_upstream_hash_peer_t) * n);
184
185 if (peers == NULL) {
186 return NGX_ERROR;
187 }
188
189 peers->number = n;
139c385 @PiotrSikora Add weights support for the upstream servers.
PiotrSikora authored Oct 22, 2012
190 peers->weighted = (w != n);
191 peers->total_weight = w;
99ace64 First commit.
Evan Miller authored Oct 19, 2009
192
f0ae839 Fix spin-lock when all servers are down
Evan Miller authored Apr 25, 2012
193 n = 0;
99ace64 First commit.
Evan Miller authored Oct 19, 2009
194 /* one hostname can have multiple IP addresses in DNS */
f0ae839 Fix spin-lock when all servers are down
Evan Miller authored Apr 25, 2012
195 for (i = 0; i < us->servers->nelts; i++) {
196 for (j = 0; j < server[i].naddrs; j++) {
f149459 Skip "backup" servers; support health checks
Evan Miller authored Feb 29, 2012
197 if (server[i].backup)
198 continue;
99ace64 First commit.
Evan Miller authored Oct 19, 2009
199 peers->peer[n].sockaddr = server[i].addrs[j].sockaddr;
200 peers->peer[n].socklen = server[i].addrs[j].socklen;
201 peers->peer[n].name = server[i].addrs[j].name;
fdaa270 @evanmiller Support for servers marked "down".
authored Jun 3, 2010
202 peers->peer[n].down = server[i].down;
139c385 @PiotrSikora Add weights support for the upstream servers.
PiotrSikora authored Oct 22, 2012
203 peers->peer[n].weight = server[i].weight;
f149459 Skip "backup" servers; support health checks
Evan Miller authored Feb 29, 2012
204 #if (NGX_HTTP_HEALTHCHECK)
205 if (!server[i].down) {
206 health_index =
207 ngx_http_healthcheck_add_peer(us,
208 &server[i].addrs[j], cf->pool);
209 if (health_index == NGX_ERROR) {
210 return NGX_ERROR;
211 }
212 peers->peer[n].health_index = health_index;
213 }
214 #endif
f0ae839 Fix spin-lock when all servers are down
Evan Miller authored Apr 25, 2012
215 n++;
99ace64 First commit.
Evan Miller authored Oct 19, 2009
216 }
217 }
218
219 us->peer.data = peers;
220
221 return NGX_OK;
222 }
223
224
225 static ngx_int_t
226 ngx_http_upstream_init_hash_peer(ngx_http_request_t *r,
227 ngx_http_upstream_srv_conf_t *us)
228 {
229 ngx_http_upstream_hash_peer_data_t *uhpd;
fdaa270 @evanmiller Support for servers marked "down".
authored Jun 3, 2010
230
99ace64 First commit.
Evan Miller authored Oct 19, 2009
231 ngx_str_t val;
232
233 if (ngx_http_script_run(r, &val, us->lengths, 0, us->values) == NULL) {
234 return NGX_ERROR;
235 }
236
237 uhpd = ngx_pcalloc(r->pool, sizeof(ngx_http_upstream_hash_peer_data_t)
fdaa270 @evanmiller Support for servers marked "down".
authored Jun 3, 2010
238 + sizeof(uintptr_t) *
239 ((ngx_http_upstream_hash_peers_t *)us->peer.data)->number /
99ace64 First commit.
Evan Miller authored Oct 19, 2009
240 (8 * sizeof(uintptr_t)));
241 if (uhpd == NULL) {
242 return NGX_ERROR;
243 }
244
245 r->upstream->peer.data = uhpd;
246
247 uhpd->peers = us->peer.data;
248
249 r->upstream->peer.free = ngx_http_upstream_free_hash_peer;
250 r->upstream->peer.get = ngx_http_upstream_get_hash_peer;
251 r->upstream->peer.tries = us->retries + 1;
2580b21 @evanmiller Support SSL backends
authored Aug 10, 2010
252 #if (NGX_HTTP_SSL)
253 r->upstream->peer.set_session = ngx_http_upstream_set_hash_peer_session;
254 r->upstream->peer.save_session = ngx_http_upstream_save_hash_peer_session;
255 #endif
99ace64 First commit.
Evan Miller authored Oct 19, 2009
256
257 /* must be big enough for the retry keys */
258 if ((uhpd->current_key.data = ngx_pcalloc(r->pool, NGX_ATOMIC_T_LEN + val.len)) == NULL) {
259 return NGX_ERROR;
260 }
261
262 ngx_memcpy(uhpd->current_key.data, val.data, val.len);
263 uhpd->current_key.len = val.len;
264 uhpd->original_key = val;
fdaa270 @evanmiller Support for servers marked "down".
authored Jun 3, 2010
265 uhpd->hash = ngx_http_upstream_hash_crc32(uhpd->current_key.data, uhpd->current_key.len);
266 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
139c385 @PiotrSikora Add weights support for the upstream servers.
PiotrSikora authored Oct 22, 2012
267 "upstream_hash: hashed \"%V\" to %ui", &uhpd->current_key,
268 ngx_http_upstream_get_hash_peer_index(uhpd));
fdaa270 @evanmiller Support for servers marked "down".
authored Jun 3, 2010
269 uhpd->try_i = 0;
270
271 /* In case this one is marked down */
272 ngx_http_upstream_hash_next_peer(uhpd, &r->upstream->peer.tries, r->connection->log);
f0ae839 Fix spin-lock when all servers are down
Evan Miller authored Apr 25, 2012
273 if ((ngx_int_t)r->upstream->peer.tries == -1) {
274 return NGX_ERROR;
275 }
99ace64 First commit.
Evan Miller authored Oct 19, 2009
276
277 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
139c385 @PiotrSikora Add weights support for the upstream servers.
PiotrSikora authored Oct 22, 2012
278 "upstream_hash: Starting with %ui",
279 ngx_http_upstream_get_hash_peer_index(uhpd));
99ace64 First commit.
Evan Miller authored Oct 19, 2009
280
281
282 return NGX_OK;
283 }
284
285
286 static ngx_int_t
287 ngx_http_upstream_get_hash_peer(ngx_peer_connection_t *pc, void *data)
288 {
289 ngx_http_upstream_hash_peer_data_t *uhpd = data;
290 ngx_http_upstream_hash_peer_t *peer;
291 ngx_uint_t peer_index;
292
293 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, pc->log, 0,
294 "upstream_hash: get upstream request hash peer try %ui", pc->tries);
295
296 pc->cached = 0;
297 pc->connection = NULL;
298
139c385 @PiotrSikora Add weights support for the upstream servers.
PiotrSikora authored Oct 22, 2012
299 peer_index = ngx_http_upstream_get_hash_peer_index(uhpd);
99ace64 First commit.
Evan Miller authored Oct 19, 2009
300
301 peer = &uhpd->peers->peer[peer_index];
302
fdaa270 @evanmiller Support for servers marked "down".
authored Jun 3, 2010
303
304 ngx_log_debug3(NGX_LOG_DEBUG_HTTP, pc->log, 0,
305 "upstream_hash: chose peer %ui w/ hash %ui for tries %ui", peer_index, uhpd->hash, pc->tries);
99ace64 First commit.
Evan Miller authored Oct 19, 2009
306
307 pc->sockaddr = peer->sockaddr;
308 pc->socklen = peer->socklen;
309 pc->name = &peer->name;
310
311 return NGX_OK;
312 }
313
314 /* retry implementation is PECL memcache compatible */
315 static void
316 ngx_http_upstream_free_hash_peer(ngx_peer_connection_t *pc, void *data,
317 ngx_uint_t state)
318 {
319 ngx_http_upstream_hash_peer_data_t *uhpd = data;
320 ngx_uint_t current;
321
fdaa270 @evanmiller Support for servers marked "down".
authored Jun 3, 2010
322 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, pc->log, 0,
99ace64 First commit.
Evan Miller authored Oct 19, 2009
323 "upstream_hash: free upstream hash peer try %ui", pc->tries);
324
325 if (state & (NGX_PEER_FAILED|NGX_PEER_NEXT)
fdaa270 @evanmiller Support for servers marked "down".
authored Jun 3, 2010
326 && pc->tries) {
139c385 @PiotrSikora Add weights support for the upstream servers.
PiotrSikora authored Oct 22, 2012
327 current = ngx_http_upstream_get_hash_peer_index(uhpd);
99ace64 First commit.
Evan Miller authored Oct 19, 2009
328
329 uhpd->tried[ngx_bitvector_index(current)] |= ngx_bitvector_bit(current);
fdaa270 @evanmiller Support for servers marked "down".
authored Jun 3, 2010
330 ngx_http_upstream_hash_next_peer(uhpd, &pc->tries, pc->log);
331 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, pc->log, 0,
139c385 @PiotrSikora Add weights support for the upstream servers.
PiotrSikora authored Oct 22, 2012
332 "upstream_hash: Using %ui because %ui failed",
333 ngx_http_upstream_get_hash_peer_index(uhpd), current);
fdaa270 @evanmiller Support for servers marked "down".
authored Jun 3, 2010
334 } else {
335 pc->tries = 0;
99ace64 First commit.
Evan Miller authored Oct 19, 2009
336 }
337 }
338
2580b21 @evanmiller Support SSL backends
authored Aug 10, 2010
339 #if (NGX_HTTP_SSL)
340 static ngx_int_t
341 ngx_http_upstream_set_hash_peer_session(ngx_peer_connection_t *pc, void *data) {
342 ngx_http_upstream_hash_peer_data_t *uhpd = data;
343
344 ngx_int_t rc;
345 ngx_ssl_session_t *ssl_session;
346 ngx_http_upstream_hash_peer_t *peer;
347 ngx_uint_t current;
348
139c385 @PiotrSikora Add weights support for the upstream servers.
PiotrSikora authored Oct 22, 2012
349 current = ngx_http_upstream_get_hash_peer_index(uhpd);
2580b21 @evanmiller Support SSL backends
authored Aug 10, 2010
350
351 peer = &uhpd->peers->peer[current];
352
353 /* TODO: threads only mutex */
354 /* ngx_lock_mutex(rrp->peers->mutex); */
355
356 ssl_session = peer->ssl_session;
357
358 rc = ngx_ssl_set_session(pc->connection, ssl_session);
359
360 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, pc->log, 0,
361 "set session: %p:%d",
362 ssl_session, ssl_session ? ssl_session->references : 0);
363
364 /* ngx_unlock_mutex(rrp->peers->mutex); */
365
366 return rc;
367 }
368
369 static void
370 ngx_http_upstream_save_hash_peer_session(ngx_peer_connection_t *pc, void *data) {
371 ngx_http_upstream_hash_peer_data_t *uhpd = data;
372 ngx_ssl_session_t *old_ssl_session, *ssl_session;
373 ngx_http_upstream_hash_peer_t *peer;
374 ngx_uint_t current;
375
376 ssl_session = ngx_ssl_get_session(pc->connection);
377
378 if (ssl_session == NULL) {
379 return;
380 }
381
382 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, pc->log, 0,
383 "save session: %p:%d", ssl_session, ssl_session->references);
384
139c385 @PiotrSikora Add weights support for the upstream servers.
PiotrSikora authored Oct 22, 2012
385 current = ngx_http_upstream_get_hash_peer_index(uhpd);
2580b21 @evanmiller Support SSL backends
authored Aug 10, 2010
386
387 peer = &uhpd->peers->peer[current];
388
389 /* TODO: threads only mutex */
390 /* ngx_lock_mutex(rrp->peers->mutex); */
391
392 old_ssl_session = peer->ssl_session;
393 peer->ssl_session = ssl_session;
394
395 /* ngx_unlock_mutex(rrp->peers->mutex); */
396
397 if (old_ssl_session) {
398
399 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, pc->log, 0,
400 "old session: %p:%d",
401 old_ssl_session, old_ssl_session->references);
402
403 /* TODO: may block */
404
405 ngx_ssl_free_session(old_ssl_session);
406 }
407 }
408 #endif
409
fdaa270 @evanmiller Support for servers marked "down".
authored Jun 3, 2010
410 static void ngx_http_upstream_hash_next_peer(ngx_http_upstream_hash_peer_data_t *uhpd,
411 ngx_uint_t *tries, ngx_log_t *log) {
412
413 ngx_uint_t current;
139c385 @PiotrSikora Add weights support for the upstream servers.
PiotrSikora authored Oct 22, 2012
414 current = ngx_http_upstream_get_hash_peer_index(uhpd);
fdaa270 @evanmiller Support for servers marked "down".
authored Jun 3, 2010
415 // Loop while there is a try left, we're on one we haven't tried, and
416 // the current peer isn't marked down
417 while ((*tries)-- && (
418 (uhpd->tried[ngx_bitvector_index(current)] & ngx_bitvector_bit(current))
f149459 Skip "backup" servers; support health checks
Evan Miller authored Feb 29, 2012
419 || uhpd->peers->peer[current].down
420 #if (NGX_HTTP_HEALTHCHECK)
421 || ngx_http_healthcheck_is_down(uhpd->peers->peer[current].health_index, log)
422 #endif
423 )) {
fdaa270 @evanmiller Support for servers marked "down".
authored Jun 3, 2010
424 uhpd->current_key.len = ngx_sprintf(uhpd->current_key.data, "%d%V",
425 ++uhpd->try_i, &uhpd->original_key) - uhpd->current_key.data;
426 uhpd->hash += ngx_http_upstream_hash_crc32(uhpd->current_key.data,
427 uhpd->current_key.len);
139c385 @PiotrSikora Add weights support for the upstream servers.
PiotrSikora authored Oct 22, 2012
428 current = ngx_http_upstream_get_hash_peer_index(uhpd);
fdaa270 @evanmiller Support for servers marked "down".
authored Jun 3, 2010
429 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, log, 0,
430 "upstream_hash: hashed \"%V\" to %ui", &uhpd->current_key, current);
431 }
432 }
433
99ace64 First commit.
Evan Miller authored Oct 19, 2009
434 /* bit-shift, bit-mask, and non-zero requirement are for libmemcache compatibility */
435 static ngx_uint_t
436 ngx_http_upstream_hash_crc32(u_char *keydata, size_t keylen)
437 {
438 ngx_uint_t crc32 = (ngx_crc32_short(keydata, keylen) >> 16) & 0x7fff;
439 return crc32 ? crc32 : 1;
440 }
441
442 static char *
443 ngx_http_upstream_hash(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
444 {
445 ngx_http_upstream_srv_conf_t *uscf;
446 ngx_http_script_compile_t sc;
447 ngx_str_t *value;
448 ngx_array_t *vars_lengths, *vars_values;
449
450 value = cf->args->elts;
451
452 ngx_memzero(&sc, sizeof(ngx_http_script_compile_t));
453
454 vars_lengths = NULL;
455 vars_values = NULL;
456
457 sc.cf = cf;
458 sc.source = &value[1];
459 sc.lengths = &vars_lengths;
460 sc.values = &vars_values;
461 sc.complete_lengths = 1;
462 sc.complete_values = 1;
463
464 if (ngx_http_script_compile(&sc) != NGX_OK) {
465 return NGX_CONF_ERROR;
466 }
467
468 uscf = ngx_http_conf_get_module_srv_conf(cf, ngx_http_upstream_module);
469
470 uscf->peer.init_upstream = ngx_http_upstream_init_hash;
471
139c385 @PiotrSikora Add weights support for the upstream servers.
PiotrSikora authored Oct 22, 2012
472 uscf->flags = NGX_HTTP_UPSTREAM_CREATE
473 |NGX_HTTP_UPSTREAM_WEIGHT
474 |NGX_HTTP_UPSTREAM_DOWN;
99ace64 First commit.
Evan Miller authored Oct 19, 2009
475
476 uscf->values = vars_values->elts;
477 uscf->lengths = vars_lengths->elts;
478
479 return NGX_CONF_OK;
480 }
481
482 static char *
483 ngx_http_upstream_hash_again(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
484 {
485 ngx_http_upstream_srv_conf_t *uscf;
486 ngx_int_t n;
487
488 ngx_str_t *value;
489
490 uscf = ngx_http_conf_get_module_srv_conf(cf, ngx_http_upstream_module);
491
492 value = cf->args->elts;
493
494 n = ngx_atoi(value[1].data, value[1].len);
495
496 if (n == NGX_ERROR || n < 0) {
497 return "invalid number";
498 }
499
500 uscf->retries = n;
501
502 return NGX_CONF_OK;
503 }
Something went wrong with that request. Please try again.