648
|
1
|
|
2 /*
|
|
3 * Copyright (C) Igor Sysoev
|
660
|
4 * Copyright (C) Nginx, Inc.
|
648
|
5 */
|
|
6
|
|
7
|
|
8 #include <ngx_config.h>
|
|
9 #include <ngx_core.h>
|
|
10 #include <ngx_http.h>
|
|
11
|
|
12
|
|
13 typedef struct {
|
|
14 u_char color;
|
|
15 u_char len;
|
|
16 u_short conn;
|
|
17 u_char data[1];
|
|
18 } ngx_http_limit_conn_node_t;
|
|
19
|
|
20
|
|
21 typedef struct {
|
|
22 ngx_shm_zone_t *shm_zone;
|
|
23 ngx_rbtree_node_t *node;
|
|
24 } ngx_http_limit_conn_cleanup_t;
|
|
25
|
|
26
|
|
27 typedef struct {
|
|
28 ngx_rbtree_t *rbtree;
|
|
29 ngx_int_t index;
|
|
30 ngx_str_t var;
|
|
31 } ngx_http_limit_conn_ctx_t;
|
|
32
|
|
33
|
|
34 typedef struct {
|
|
35 ngx_shm_zone_t *shm_zone;
|
|
36 ngx_uint_t conn;
|
|
37 } ngx_http_limit_conn_limit_t;
|
|
38
|
|
39
|
|
40 typedef struct {
|
|
41 ngx_array_t limits;
|
|
42 ngx_uint_t log_level;
|
|
43 } ngx_http_limit_conn_conf_t;
|
|
44
|
|
45
|
|
46 static ngx_rbtree_node_t *ngx_http_limit_conn_lookup(ngx_rbtree_t *rbtree,
|
|
47 ngx_http_variable_value_t *vv, uint32_t hash);
|
|
48 static void ngx_http_limit_conn_cleanup(void *data);
|
|
49 static ngx_inline void ngx_http_limit_conn_cleanup_all(ngx_pool_t *pool);
|
|
50
|
|
51 static void *ngx_http_limit_conn_create_conf(ngx_conf_t *cf);
|
|
52 static char *ngx_http_limit_conn_merge_conf(ngx_conf_t *cf, void *parent,
|
|
53 void *child);
|
|
54 static char *ngx_http_limit_conn_zone(ngx_conf_t *cf, ngx_command_t *cmd,
|
|
55 void *conf);
|
|
56 static char *ngx_http_limit_zone(ngx_conf_t *cf, ngx_command_t *cmd,
|
|
57 void *conf);
|
|
58 static char *ngx_http_limit_conn(ngx_conf_t *cf, ngx_command_t *cmd,
|
|
59 void *conf);
|
|
60 static ngx_int_t ngx_http_limit_conn_init(ngx_conf_t *cf);
|
|
61
|
|
62
|
|
63 static ngx_conf_deprecated_t ngx_conf_deprecated_limit_zone = {
|
|
64 ngx_conf_deprecated, "limit_zone", "limit_conn_zone"
|
|
65 };
|
|
66
|
|
67
|
|
68 static ngx_conf_enum_t ngx_http_limit_conn_log_levels[] = {
|
|
69 { ngx_string("info"), NGX_LOG_INFO },
|
|
70 { ngx_string("notice"), NGX_LOG_NOTICE },
|
|
71 { ngx_string("warn"), NGX_LOG_WARN },
|
|
72 { ngx_string("error"), NGX_LOG_ERR },
|
|
73 { ngx_null_string, 0 }
|
|
74 };
|
|
75
|
|
76
|
|
77 static ngx_command_t ngx_http_limit_conn_commands[] = {
|
|
78
|
|
79 { ngx_string("limit_conn_zone"),
|
|
80 NGX_HTTP_MAIN_CONF|NGX_CONF_TAKE2,
|
|
81 ngx_http_limit_conn_zone,
|
|
82 0,
|
|
83 0,
|
|
84 NULL },
|
|
85
|
|
86 { ngx_string("limit_zone"),
|
|
87 NGX_HTTP_MAIN_CONF|NGX_CONF_TAKE3,
|
|
88 ngx_http_limit_zone,
|
|
89 0,
|
|
90 0,
|
|
91 NULL },
|
|
92
|
|
93 { ngx_string("limit_conn"),
|
|
94 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE2,
|
|
95 ngx_http_limit_conn,
|
|
96 NGX_HTTP_LOC_CONF_OFFSET,
|
|
97 0,
|
|
98 NULL },
|
|
99
|
|
100 { ngx_string("limit_conn_log_level"),
|
|
101 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
|
|
102 ngx_conf_set_enum_slot,
|
|
103 NGX_HTTP_LOC_CONF_OFFSET,
|
|
104 offsetof(ngx_http_limit_conn_conf_t, log_level),
|
|
105 &ngx_http_limit_conn_log_levels },
|
|
106
|
|
107 ngx_null_command
|
|
108 };
|
|
109
|
|
110
|
|
111 static ngx_http_module_t ngx_http_limit_conn_module_ctx = {
|
|
112 NULL, /* preconfiguration */
|
|
113 ngx_http_limit_conn_init, /* postconfiguration */
|
|
114
|
|
115 NULL, /* create main configuration */
|
|
116 NULL, /* init main configuration */
|
|
117
|
|
118 NULL, /* create server configuration */
|
|
119 NULL, /* merge server configuration */
|
|
120
|
|
121 ngx_http_limit_conn_create_conf, /* create location configration */
|
|
122 ngx_http_limit_conn_merge_conf /* merge location configration */
|
|
123 };
|
|
124
|
|
125
|
|
126 ngx_module_t ngx_http_limit_conn_module = {
|
|
127 NGX_MODULE_V1,
|
|
128 &ngx_http_limit_conn_module_ctx, /* module context */
|
|
129 ngx_http_limit_conn_commands, /* module directives */
|
|
130 NGX_HTTP_MODULE, /* module type */
|
|
131 NULL, /* init master */
|
|
132 NULL, /* init module */
|
|
133 NULL, /* init process */
|
|
134 NULL, /* init thread */
|
|
135 NULL, /* exit thread */
|
|
136 NULL, /* exit process */
|
|
137 NULL, /* exit master */
|
|
138 NGX_MODULE_V1_PADDING
|
|
139 };
|
|
140
|
|
141
|
|
142 static ngx_int_t
|
|
143 ngx_http_limit_conn_handler(ngx_http_request_t *r)
|
|
144 {
|
|
145 size_t len, n;
|
|
146 uint32_t hash;
|
|
147 ngx_uint_t i;
|
|
148 ngx_slab_pool_t *shpool;
|
|
149 ngx_rbtree_node_t *node;
|
|
150 ngx_pool_cleanup_t *cln;
|
|
151 ngx_http_variable_value_t *vv;
|
|
152 ngx_http_limit_conn_ctx_t *ctx;
|
|
153 ngx_http_limit_conn_node_t *lc;
|
|
154 ngx_http_limit_conn_conf_t *lccf;
|
|
155 ngx_http_limit_conn_limit_t *limits;
|
|
156 ngx_http_limit_conn_cleanup_t *lccln;
|
|
157
|
|
158 if (r->main->limit_conn_set) {
|
|
159 return NGX_DECLINED;
|
|
160 }
|
|
161
|
|
162 r->main->limit_conn_set = 1;
|
|
163
|
|
164 lccf = ngx_http_get_module_loc_conf(r, ngx_http_limit_conn_module);
|
|
165 limits = lccf->limits.elts;
|
|
166
|
|
167 for (i = 0; i < lccf->limits.nelts; i++) {
|
|
168 ctx = limits[i].shm_zone->data;
|
|
169
|
|
170 vv = ngx_http_get_indexed_variable(r, ctx->index);
|
|
171
|
|
172 if (vv == NULL || vv->not_found) {
|
|
173 continue;
|
|
174 }
|
|
175
|
|
176 len = vv->len;
|
|
177
|
|
178 if (len == 0) {
|
|
179 continue;
|
|
180 }
|
|
181
|
|
182 if (len > 255) {
|
|
183 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
|
|
184 "the value of the \"%V\" variable "
|
|
185 "is more than 255 bytes: \"%v\"",
|
|
186 &ctx->var, vv);
|
|
187 continue;
|
|
188 }
|
|
189
|
|
190 hash = ngx_crc32_short(vv->data, len);
|
|
191
|
|
192 shpool = (ngx_slab_pool_t *) limits[i].shm_zone->shm.addr;
|
|
193
|
|
194 ngx_shmtx_lock(&shpool->mutex);
|
|
195
|
|
196 node = ngx_http_limit_conn_lookup(ctx->rbtree, vv, hash);
|
|
197
|
|
198 if (node == NULL) {
|
|
199
|
|
200 n = offsetof(ngx_rbtree_node_t, color)
|
|
201 + offsetof(ngx_http_limit_conn_node_t, data)
|
|
202 + len;
|
|
203
|
|
204 node = ngx_slab_alloc_locked(shpool, n);
|
|
205
|
|
206 if (node == NULL) {
|
|
207 ngx_shmtx_unlock(&shpool->mutex);
|
|
208 ngx_http_limit_conn_cleanup_all(r->pool);
|
|
209 return NGX_HTTP_SERVICE_UNAVAILABLE;
|
|
210 }
|
|
211
|
|
212 lc = (ngx_http_limit_conn_node_t *) &node->color;
|
|
213
|
|
214 node->key = hash;
|
|
215 lc->len = (u_char) len;
|
|
216 lc->conn = 1;
|
|
217 ngx_memcpy(lc->data, vv->data, len);
|
|
218
|
|
219 ngx_rbtree_insert(ctx->rbtree, node);
|
|
220
|
|
221 } else {
|
|
222
|
|
223 lc = (ngx_http_limit_conn_node_t *) &node->color;
|
|
224
|
|
225 if ((ngx_uint_t) lc->conn >= limits[i].conn) {
|
|
226
|
|
227 ngx_shmtx_unlock(&shpool->mutex);
|
|
228
|
|
229 ngx_log_error(lccf->log_level, r->connection->log, 0,
|
|
230 "limiting connections by zone \"%V\"",
|
|
231 &limits[i].shm_zone->shm.name);
|
|
232
|
|
233 ngx_http_limit_conn_cleanup_all(r->pool);
|
|
234 return NGX_HTTP_SERVICE_UNAVAILABLE;
|
|
235 }
|
|
236
|
|
237 lc->conn++;
|
|
238 }
|
|
239
|
|
240 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
241 "limit zone: %08XD %d", node->key, lc->conn);
|
|
242
|
|
243 ngx_shmtx_unlock(&shpool->mutex);
|
|
244
|
|
245 cln = ngx_pool_cleanup_add(r->pool,
|
|
246 sizeof(ngx_http_limit_conn_cleanup_t));
|
|
247 if (cln == NULL) {
|
|
248 return NGX_HTTP_INTERNAL_SERVER_ERROR;
|
|
249 }
|
|
250
|
|
251 cln->handler = ngx_http_limit_conn_cleanup;
|
|
252 lccln = cln->data;
|
|
253
|
|
254 lccln->shm_zone = limits[i].shm_zone;
|
|
255 lccln->node = node;
|
|
256 }
|
|
257
|
|
258 return NGX_DECLINED;
|
|
259 }
|
|
260
|
|
261
|
|
262 static void
|
|
263 ngx_http_limit_conn_rbtree_insert_value(ngx_rbtree_node_t *temp,
|
|
264 ngx_rbtree_node_t *node, ngx_rbtree_node_t *sentinel)
|
|
265 {
|
|
266 ngx_rbtree_node_t **p;
|
|
267 ngx_http_limit_conn_node_t *lcn, *lcnt;
|
|
268
|
|
269 for ( ;; ) {
|
|
270
|
|
271 if (node->key < temp->key) {
|
|
272
|
|
273 p = &temp->left;
|
|
274
|
|
275 } else if (node->key > temp->key) {
|
|
276
|
|
277 p = &temp->right;
|
|
278
|
|
279 } else { /* node->key == temp->key */
|
|
280
|
|
281 lcn = (ngx_http_limit_conn_node_t *) &node->color;
|
|
282 lcnt = (ngx_http_limit_conn_node_t *) &temp->color;
|
|
283
|
|
284 p = (ngx_memn2cmp(lcn->data, lcnt->data, lcn->len, lcnt->len) < 0)
|
|
285 ? &temp->left : &temp->right;
|
|
286 }
|
|
287
|
|
288 if (*p == sentinel) {
|
|
289 break;
|
|
290 }
|
|
291
|
|
292 temp = *p;
|
|
293 }
|
|
294
|
|
295 *p = node;
|
|
296 node->parent = temp;
|
|
297 node->left = sentinel;
|
|
298 node->right = sentinel;
|
|
299 ngx_rbt_red(node);
|
|
300 }
|
|
301
|
|
302
|
|
303 static ngx_rbtree_node_t *
|
|
304 ngx_http_limit_conn_lookup(ngx_rbtree_t *rbtree, ngx_http_variable_value_t *vv,
|
|
305 uint32_t hash)
|
|
306 {
|
|
307 ngx_int_t rc;
|
|
308 ngx_rbtree_node_t *node, *sentinel;
|
|
309 ngx_http_limit_conn_node_t *lcn;
|
|
310
|
|
311 node = rbtree->root;
|
|
312 sentinel = rbtree->sentinel;
|
|
313
|
|
314 while (node != sentinel) {
|
|
315
|
|
316 if (hash < node->key) {
|
|
317 node = node->left;
|
|
318 continue;
|
|
319 }
|
|
320
|
|
321 if (hash > node->key) {
|
|
322 node = node->right;
|
|
323 continue;
|
|
324 }
|
|
325
|
|
326 /* hash == node->key */
|
|
327
|
|
328 do {
|
|
329 lcn = (ngx_http_limit_conn_node_t *) &node->color;
|
|
330
|
|
331 rc = ngx_memn2cmp(vv->data, lcn->data,
|
|
332 (size_t) vv->len, (size_t) lcn->len);
|
|
333 if (rc == 0) {
|
|
334 return node;
|
|
335 }
|
|
336
|
|
337 node = (rc < 0) ? node->left : node->right;
|
|
338
|
|
339 } while (node != sentinel && hash == node->key);
|
|
340
|
|
341 break;
|
|
342 }
|
|
343
|
|
344 return NULL;
|
|
345 }
|
|
346
|
|
347
|
|
348 static void
|
|
349 ngx_http_limit_conn_cleanup(void *data)
|
|
350 {
|
|
351 ngx_http_limit_conn_cleanup_t *lccln = data;
|
|
352
|
|
353 ngx_slab_pool_t *shpool;
|
|
354 ngx_rbtree_node_t *node;
|
|
355 ngx_http_limit_conn_ctx_t *ctx;
|
|
356 ngx_http_limit_conn_node_t *lc;
|
|
357
|
|
358 ctx = lccln->shm_zone->data;
|
|
359 shpool = (ngx_slab_pool_t *) lccln->shm_zone->shm.addr;
|
|
360 node = lccln->node;
|
|
361 lc = (ngx_http_limit_conn_node_t *) &node->color;
|
|
362
|
|
363 ngx_shmtx_lock(&shpool->mutex);
|
|
364
|
|
365 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, lccln->shm_zone->shm.log, 0,
|
|
366 "limit zone cleanup: %08XD %d", node->key, lc->conn);
|
|
367
|
|
368 lc->conn--;
|
|
369
|
|
370 if (lc->conn == 0) {
|
|
371 ngx_rbtree_delete(ctx->rbtree, node);
|
|
372 ngx_slab_free_locked(shpool, node);
|
|
373 }
|
|
374
|
|
375 ngx_shmtx_unlock(&shpool->mutex);
|
|
376 }
|
|
377
|
|
378
|
|
379 static ngx_inline void
|
|
380 ngx_http_limit_conn_cleanup_all(ngx_pool_t *pool)
|
|
381 {
|
|
382 ngx_pool_cleanup_t *cln;
|
|
383
|
|
384 cln = pool->cleanup;
|
|
385
|
|
386 while (cln && cln->handler == ngx_http_limit_conn_cleanup) {
|
|
387 ngx_http_limit_conn_cleanup(cln->data);
|
|
388 cln = cln->next;
|
|
389 }
|
|
390
|
|
391 pool->cleanup = cln;
|
|
392 }
|
|
393
|
|
394
|
|
395 static ngx_int_t
|
|
396 ngx_http_limit_conn_init_zone(ngx_shm_zone_t *shm_zone, void *data)
|
|
397 {
|
|
398 ngx_http_limit_conn_ctx_t *octx = data;
|
|
399
|
|
400 size_t len;
|
|
401 ngx_slab_pool_t *shpool;
|
|
402 ngx_rbtree_node_t *sentinel;
|
|
403 ngx_http_limit_conn_ctx_t *ctx;
|
|
404
|
|
405 ctx = shm_zone->data;
|
|
406
|
|
407 if (octx) {
|
|
408 if (ngx_strcmp(ctx->var.data, octx->var.data) != 0) {
|
|
409 ngx_log_error(NGX_LOG_EMERG, shm_zone->shm.log, 0,
|
|
410 "limit_conn_zone \"%V\" uses the \"%V\" variable "
|
|
411 "while previously it used the \"%V\" variable",
|
|
412 &shm_zone->shm.name, &ctx->var, &octx->var);
|
|
413 return NGX_ERROR;
|
|
414 }
|
|
415
|
|
416 ctx->rbtree = octx->rbtree;
|
|
417
|
|
418 return NGX_OK;
|
|
419 }
|
|
420
|
|
421 shpool = (ngx_slab_pool_t *) shm_zone->shm.addr;
|
|
422
|
|
423 if (shm_zone->shm.exists) {
|
|
424 ctx->rbtree = shpool->data;
|
|
425
|
|
426 return NGX_OK;
|
|
427 }
|
|
428
|
|
429 ctx->rbtree = ngx_slab_alloc(shpool, sizeof(ngx_rbtree_t));
|
|
430 if (ctx->rbtree == NULL) {
|
|
431 return NGX_ERROR;
|
|
432 }
|
|
433
|
|
434 shpool->data = ctx->rbtree;
|
|
435
|
|
436 sentinel = ngx_slab_alloc(shpool, sizeof(ngx_rbtree_node_t));
|
|
437 if (sentinel == NULL) {
|
|
438 return NGX_ERROR;
|
|
439 }
|
|
440
|
|
441 ngx_rbtree_init(ctx->rbtree, sentinel,
|
|
442 ngx_http_limit_conn_rbtree_insert_value);
|
|
443
|
|
444 len = sizeof(" in limit_conn_zone \"\"") + shm_zone->shm.name.len;
|
|
445
|
|
446 shpool->log_ctx = ngx_slab_alloc(shpool, len);
|
|
447 if (shpool->log_ctx == NULL) {
|
|
448 return NGX_ERROR;
|
|
449 }
|
|
450
|
|
451 ngx_sprintf(shpool->log_ctx, " in limit_conn_zone \"%V\"%Z",
|
|
452 &shm_zone->shm.name);
|
|
453
|
|
454 return NGX_OK;
|
|
455 }
|
|
456
|
|
457
|
|
458 static void *
|
|
459 ngx_http_limit_conn_create_conf(ngx_conf_t *cf)
|
|
460 {
|
|
461 ngx_http_limit_conn_conf_t *conf;
|
|
462
|
|
463 conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_limit_conn_conf_t));
|
|
464 if (conf == NULL) {
|
|
465 return NULL;
|
|
466 }
|
|
467
|
|
468 /*
|
|
469 * set by ngx_pcalloc():
|
|
470 *
|
|
471 * conf->limits.elts = NULL;
|
|
472 */
|
|
473
|
|
474 conf->log_level = NGX_CONF_UNSET_UINT;
|
|
475
|
|
476 return conf;
|
|
477 }
|
|
478
|
|
479
|
|
480 static char *
|
|
481 ngx_http_limit_conn_merge_conf(ngx_conf_t *cf, void *parent, void *child)
|
|
482 {
|
|
483 ngx_http_limit_conn_conf_t *prev = parent;
|
|
484 ngx_http_limit_conn_conf_t *conf = child;
|
|
485
|
|
486 if (conf->limits.elts == NULL) {
|
656
|
487 conf->limits = prev->limits;
|
648
|
488 }
|
|
489
|
|
490 ngx_conf_merge_uint_value(conf->log_level, prev->log_level, NGX_LOG_ERR);
|
|
491
|
|
492 return NGX_CONF_OK;
|
|
493 }
|
|
494
|
|
495
|
|
496 static char *
|
|
497 ngx_http_limit_conn_zone(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
|
|
498 {
|
|
499 u_char *p;
|
|
500 ssize_t size;
|
|
501 ngx_str_t *value, name, s;
|
|
502 ngx_uint_t i;
|
|
503 ngx_shm_zone_t *shm_zone;
|
|
504 ngx_http_limit_conn_ctx_t *ctx;
|
|
505
|
|
506 value = cf->args->elts;
|
|
507
|
|
508 ctx = NULL;
|
|
509 size = 0;
|
|
510 name.len = 0;
|
|
511
|
|
512 for (i = 1; i < cf->args->nelts; i++) {
|
|
513
|
|
514 if (ngx_strncmp(value[i].data, "zone=", 5) == 0) {
|
|
515
|
|
516 name.data = value[i].data + 5;
|
|
517
|
|
518 p = (u_char *) ngx_strchr(name.data, ':');
|
|
519
|
|
520 if (p == NULL) {
|
|
521 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
522 "invalid zone size \"%V\"", &value[i]);
|
|
523 return NGX_CONF_ERROR;
|
|
524 }
|
|
525
|
|
526 name.len = p - name.data;
|
|
527
|
|
528 s.data = p + 1;
|
|
529 s.len = value[i].data + value[i].len - s.data;
|
|
530
|
|
531 size = ngx_parse_size(&s);
|
|
532
|
|
533 if (size == NGX_ERROR) {
|
|
534 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
535 "invalid zone size \"%V\"", &value[i]);
|
|
536 return NGX_CONF_ERROR;
|
|
537 }
|
|
538
|
|
539 if (size < (ssize_t) (8 * ngx_pagesize)) {
|
|
540 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
541 "zone \"%V\" is too small", &value[i]);
|
|
542 return NGX_CONF_ERROR;
|
|
543 }
|
|
544
|
|
545 continue;
|
|
546 }
|
|
547
|
|
548 if (value[i].data[0] == '$') {
|
|
549
|
|
550 value[i].len--;
|
|
551 value[i].data++;
|
|
552
|
|
553 ctx = ngx_pcalloc(cf->pool, sizeof(ngx_http_limit_conn_ctx_t));
|
|
554 if (ctx == NULL) {
|
|
555 return NGX_CONF_ERROR;
|
|
556 }
|
|
557
|
|
558 ctx->index = ngx_http_get_variable_index(cf, &value[i]);
|
|
559 if (ctx->index == NGX_ERROR) {
|
|
560 return NGX_CONF_ERROR;
|
|
561 }
|
|
562
|
|
563 ctx->var = value[i];
|
|
564
|
|
565 continue;
|
|
566 }
|
|
567
|
|
568 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
569 "invalid parameter \"%V\"", &value[i]);
|
|
570 return NGX_CONF_ERROR;
|
|
571 }
|
|
572
|
|
573 if (name.len == 0) {
|
|
574 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
575 "\"%V\" must have \"zone\" parameter",
|
|
576 &cmd->name);
|
|
577 return NGX_CONF_ERROR;
|
|
578 }
|
|
579
|
|
580 if (ctx == NULL) {
|
|
581 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
582 "no variable is defined for %V \"%V\"",
|
|
583 &cmd->name, &name);
|
|
584 return NGX_CONF_ERROR;
|
|
585 }
|
|
586
|
|
587 shm_zone = ngx_shared_memory_add(cf, &name, size,
|
|
588 &ngx_http_limit_conn_module);
|
|
589 if (shm_zone == NULL) {
|
|
590 return NGX_CONF_ERROR;
|
|
591 }
|
|
592
|
|
593 if (shm_zone->data) {
|
|
594 ctx = shm_zone->data;
|
|
595
|
|
596 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
597 "%V \"%V\" is already bound to variable \"%V\"",
|
|
598 &cmd->name, &name, &ctx->var);
|
|
599 return NGX_CONF_ERROR;
|
|
600 }
|
|
601
|
|
602 shm_zone->init = ngx_http_limit_conn_init_zone;
|
|
603 shm_zone->data = ctx;
|
|
604
|
|
605 return NGX_CONF_OK;
|
|
606 }
|
|
607
|
|
608
|
|
609 static char *
|
|
610 ngx_http_limit_zone(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
|
|
611 {
|
|
612 ssize_t n;
|
|
613 ngx_str_t *value;
|
|
614 ngx_shm_zone_t *shm_zone;
|
|
615 ngx_http_limit_conn_ctx_t *ctx;
|
|
616
|
|
617 ngx_conf_deprecated(cf, &ngx_conf_deprecated_limit_zone, NULL);
|
|
618
|
|
619 value = cf->args->elts;
|
|
620
|
|
621 if (value[2].data[0] != '$') {
|
|
622 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
623 "invalid variable name \"%V\"", &value[2]);
|
|
624 return NGX_CONF_ERROR;
|
|
625 }
|
|
626
|
|
627 value[2].len--;
|
|
628 value[2].data++;
|
|
629
|
|
630 ctx = ngx_pcalloc(cf->pool, sizeof(ngx_http_limit_conn_ctx_t));
|
|
631 if (ctx == NULL) {
|
|
632 return NGX_CONF_ERROR;
|
|
633 }
|
|
634
|
|
635 ctx->index = ngx_http_get_variable_index(cf, &value[2]);
|
|
636 if (ctx->index == NGX_ERROR) {
|
|
637 return NGX_CONF_ERROR;
|
|
638 }
|
|
639
|
|
640 ctx->var = value[2];
|
|
641
|
|
642 n = ngx_parse_size(&value[3]);
|
|
643
|
|
644 if (n == NGX_ERROR) {
|
|
645 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
646 "invalid size of limit_zone \"%V\"", &value[3]);
|
|
647 return NGX_CONF_ERROR;
|
|
648 }
|
|
649
|
|
650 if (n < (ngx_int_t) (8 * ngx_pagesize)) {
|
|
651 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
652 "limit_zone \"%V\" is too small", &value[1]);
|
|
653 return NGX_CONF_ERROR;
|
|
654 }
|
|
655
|
|
656
|
|
657 shm_zone = ngx_shared_memory_add(cf, &value[1], n,
|
|
658 &ngx_http_limit_conn_module);
|
|
659 if (shm_zone == NULL) {
|
|
660 return NGX_CONF_ERROR;
|
|
661 }
|
|
662
|
|
663 if (shm_zone->data) {
|
|
664 ctx = shm_zone->data;
|
|
665
|
|
666 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
667 "limit_zone \"%V\" is already bound to variable \"%V\"",
|
|
668 &value[1], &ctx->var);
|
|
669 return NGX_CONF_ERROR;
|
|
670 }
|
|
671
|
|
672 shm_zone->init = ngx_http_limit_conn_init_zone;
|
|
673 shm_zone->data = ctx;
|
|
674
|
|
675 return NGX_CONF_OK;
|
|
676 }
|
|
677
|
|
678
|
|
679 static char *
|
|
680 ngx_http_limit_conn(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
|
|
681 {
|
|
682 ngx_shm_zone_t *shm_zone;
|
|
683 ngx_http_limit_conn_conf_t *lccf = conf;
|
|
684 ngx_http_limit_conn_limit_t *limit, *limits;
|
|
685
|
|
686 ngx_str_t *value;
|
|
687 ngx_int_t n;
|
|
688 ngx_uint_t i;
|
|
689
|
|
690 value = cf->args->elts;
|
|
691
|
|
692 shm_zone = ngx_shared_memory_add(cf, &value[1], 0,
|
|
693 &ngx_http_limit_conn_module);
|
|
694 if (shm_zone == NULL) {
|
|
695 return NGX_CONF_ERROR;
|
|
696 }
|
|
697
|
|
698 limits = lccf->limits.elts;
|
|
699
|
|
700 if (limits == NULL) {
|
|
701 if (ngx_array_init(&lccf->limits, cf->pool, 1,
|
|
702 sizeof(ngx_http_limit_conn_limit_t))
|
|
703 != NGX_OK)
|
|
704 {
|
|
705 return NGX_CONF_ERROR;
|
|
706 }
|
|
707 }
|
|
708
|
|
709 for (i = 0; i < lccf->limits.nelts; i++) {
|
|
710 if (shm_zone == limits[i].shm_zone) {
|
|
711 return "is duplicate";
|
|
712 }
|
|
713 }
|
|
714
|
|
715 n = ngx_atoi(value[2].data, value[2].len);
|
|
716 if (n <= 0) {
|
|
717 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
718 "invalid number of connections \"%V\"", &value[2]);
|
|
719 return NGX_CONF_ERROR;
|
|
720 }
|
|
721
|
|
722 if (n > 65535) {
|
|
723 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
724 "connection limit must be less 65536");
|
|
725 return NGX_CONF_ERROR;
|
|
726 }
|
|
727
|
|
728 limit = ngx_array_push(&lccf->limits);
|
|
729 limit->conn = n;
|
|
730 limit->shm_zone = shm_zone;
|
|
731
|
|
732 return NGX_CONF_OK;
|
|
733 }
|
|
734
|
|
735
|
|
736 static ngx_int_t
|
|
737 ngx_http_limit_conn_init(ngx_conf_t *cf)
|
|
738 {
|
|
739 ngx_http_handler_pt *h;
|
|
740 ngx_http_core_main_conf_t *cmcf;
|
|
741
|
|
742 cmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_core_module);
|
|
743
|
|
744 h = ngx_array_push(&cmcf->phases[NGX_HTTP_PREACCESS_PHASE].handlers);
|
|
745 if (h == NULL) {
|
|
746 return NGX_ERROR;
|
|
747 }
|
|
748
|
|
749 *h = ngx_http_limit_conn_handler;
|
|
750
|
|
751 return NGX_OK;
|
|
752 }
|