comparison src/http/modules/ngx_http_limit_req_module.c @ 416:b4f69f2ef02c NGINX_0_7_20

nginx 0.7.20 *) Changes in the ngx_http_gzip_filter_module. *) Feature: the ngx_http_limit_req_module. *) Bugfix: worker processes might exit on a SIGBUS signal on sparc and ppc platforms; the bug had appeared in 0.7.3. Thanks to Maxim Dounin. *) Bugfix: the "proxy_pass http://host/some:uri" directives did not work; the bug had appeared in 0.7.12. *) Bugfix: in HTTPS mode requests might fail with the "bad write retry" error. *) Bugfix: the ngx_http_secure_link_module did not work inside locations, whose names are less than 3 characters. *) Bugfix: $server_addr variable might have no value.
author Igor Sysoev <http://sysoev.ru>
date Mon, 10 Nov 2008 00:00:00 +0300
parents
children ff86d646f9df
comparison
equal deleted inserted replaced
415:5410f1e19796 416:b4f69f2ef02c
1
2 /*
3 * Copyright (C) Igor Sysoev
4 */
5
6
7 #include <ngx_config.h>
8 #include <ngx_core.h>
9 #include <ngx_http.h>
10
11
12 typedef struct {
13 u_char color;
14 u_char dummy;
15 u_short len;
16 ngx_queue_t queue;
17 ngx_msec_t last;
18 float rate;
19 u_char data[1];
20 } ngx_http_limit_req_node_t;
21
22
23 typedef struct {
24 ngx_rbtree_t *rbtree;
25 ngx_queue_t *queue;
26 ngx_slab_pool_t *shpool;
27 float rate;
28 ngx_int_t index;
29 ngx_str_t var;
30 } ngx_http_limit_req_ctx_t;
31
32
33 typedef struct {
34 ngx_shm_zone_t *shm_zone;
35 float burst;
36 ngx_msec_t delay;
37 } ngx_http_limit_req_conf_t;
38
39
40 static void ngx_http_limit_req_delay(ngx_http_request_t *r);
41 static ngx_int_t ngx_http_limit_req_lookup(ngx_http_limit_req_conf_t *lzcf,
42 ngx_uint_t hash, u_char *data, size_t len, ngx_http_limit_req_node_t **lzp);
43 static void ngx_http_limit_req_expire(ngx_http_limit_req_ctx_t *ctx,
44 ngx_uint_t n);
45
46 static void *ngx_http_limit_req_create_conf(ngx_conf_t *cf);
47 static char *ngx_http_limit_req_merge_conf(ngx_conf_t *cf, void *parent,
48 void *child);
49 static char *ngx_http_limit_req_zone(ngx_conf_t *cf, ngx_command_t *cmd,
50 void *conf);
51 static char *ngx_http_limit_req(ngx_conf_t *cf, ngx_command_t *cmd,
52 void *conf);
53 static ngx_int_t ngx_http_limit_req_init(ngx_conf_t *cf);
54
55
56 static ngx_command_t ngx_http_limit_req_commands[] = {
57
58 { ngx_string("limit_req_zone"),
59 NGX_HTTP_MAIN_CONF|NGX_CONF_TAKE3,
60 ngx_http_limit_req_zone,
61 0,
62 0,
63 NULL },
64
65 { ngx_string("limit_req"),
66 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE123,
67 ngx_http_limit_req,
68 NGX_HTTP_LOC_CONF_OFFSET,
69 0,
70 NULL },
71
72 ngx_null_command
73 };
74
75
76 static ngx_http_module_t ngx_http_limit_req_module_ctx = {
77 NULL, /* preconfiguration */
78 ngx_http_limit_req_init, /* postconfiguration */
79
80 NULL, /* create main configuration */
81 NULL, /* init main configuration */
82
83 NULL, /* create server configuration */
84 NULL, /* merge server configuration */
85
86 ngx_http_limit_req_create_conf, /* create location configration */
87 ngx_http_limit_req_merge_conf /* merge location configration */
88 };
89
90
91 ngx_module_t ngx_http_limit_req_module = {
92 NGX_MODULE_V1,
93 &ngx_http_limit_req_module_ctx, /* module context */
94 ngx_http_limit_req_commands, /* module directives */
95 NGX_HTTP_MODULE, /* module type */
96 NULL, /* init master */
97 NULL, /* init module */
98 NULL, /* init process */
99 NULL, /* init thread */
100 NULL, /* exit thread */
101 NULL, /* exit process */
102 NULL, /* exit master */
103 NGX_MODULE_V1_PADDING
104 };
105
106
107 static ngx_int_t
108 ngx_http_limit_req_handler(ngx_http_request_t *r)
109 {
110 float rate;
111 size_t len, n;
112 uint32_t hash;
113 ngx_int_t rc;
114 ngx_time_t *tp;
115 ngx_rbtree_node_t *node;
116 ngx_http_variable_value_t *vv;
117 ngx_http_limit_req_ctx_t *ctx;
118 ngx_http_limit_req_node_t *lz;
119 ngx_http_limit_req_conf_t *lzcf;
120
121 if (r->main->limit_req_set) {
122 return NGX_DECLINED;
123 }
124
125 lzcf = ngx_http_get_module_loc_conf(r, ngx_http_limit_req_module);
126
127 if (lzcf->shm_zone == NULL) {
128 return NGX_DECLINED;
129 }
130
131 ctx = lzcf->shm_zone->data;
132
133 vv = ngx_http_get_indexed_variable(r, ctx->index);
134
135 if (vv == NULL || vv->not_found) {
136 return NGX_DECLINED;
137 }
138
139 len = vv->len;
140
141 if (len == 0) {
142 return NGX_DECLINED;
143 }
144
145 if (len > 65535) {
146 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
147 "the value of the \"%V\" variable "
148 "is more than 65535 bytes: \"%v\"",
149 &ctx->var, vv);
150 return NGX_DECLINED;
151 }
152
153 r->main->limit_req_set = 1;
154
155 hash = ngx_crc32_short(vv->data, len);
156
157 ngx_shmtx_lock(&ctx->shpool->mutex);
158
159 ngx_http_limit_req_expire(ctx, 1);
160
161 rc = ngx_http_limit_req_lookup(lzcf, hash, vv->data, len, &lz);
162
163 if (lz) {
164 ngx_queue_remove(&lz->queue);
165
166 ngx_queue_insert_head(ctx->queue, &lz->queue);
167
168 rate = lz->rate;
169
170 } else {
171 rate = 0.0;
172 }
173
174 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
175 "limit_req: %i %.3f", rc, rate);
176
177 if (rc == NGX_BUSY) {
178 ngx_shmtx_unlock(&ctx->shpool->mutex);
179
180 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
181 "limiting requests, %.3f r/s", rate);
182
183 return NGX_HTTP_SERVICE_UNAVAILABLE;
184 }
185
186 if (rc == NGX_AGAIN) {
187 ngx_shmtx_unlock(&ctx->shpool->mutex);
188
189 if (lzcf->delay) {
190 ngx_log_error(NGX_LOG_WARN, r->connection->log, 0,
191 "delaying requests, %.3f r/s", rate);
192
193 if (ngx_handle_read_event(r->connection->read, 0) != NGX_OK) {
194 return NGX_HTTP_INTERNAL_SERVER_ERROR;
195 }
196
197 r->read_event_handler = ngx_http_test_reading;
198 r->write_event_handler = ngx_http_limit_req_delay;
199 ngx_add_timer(r->connection->write, lzcf->delay);
200
201 return NGX_AGAIN;
202 }
203
204 return NGX_DECLINED;
205 }
206
207 if (rc == NGX_OK) {
208 goto done;
209 }
210
211 /* rc == NGX_DECLINED */
212
213 n = offsetof(ngx_rbtree_node_t, color)
214 + offsetof(ngx_http_limit_req_node_t, data)
215 + len;
216
217 node = ngx_slab_alloc_locked(ctx->shpool, n);
218 if (node == NULL) {
219
220 ngx_http_limit_req_expire(ctx, 0);
221
222 node = ngx_slab_alloc_locked(ctx->shpool, n);
223 if (node == NULL) {
224 ngx_shmtx_unlock(&ctx->shpool->mutex);
225 return NGX_HTTP_SERVICE_UNAVAILABLE;
226 }
227 }
228
229 lz = (ngx_http_limit_req_node_t *) &node->color;
230
231 node->key = hash;
232 lz->len = (u_char) len;
233
234 tp = ngx_timeofday();
235 lz->last = (ngx_msec_t) (tp->sec * 1000 + tp->msec);
236
237 lz->rate = 0.0;
238 ngx_memcpy(lz->data, vv->data, len);
239
240 ngx_rbtree_insert(ctx->rbtree, node);
241
242 ngx_queue_insert_head(ctx->queue, &lz->queue);
243
244 done:
245
246 ngx_shmtx_unlock(&ctx->shpool->mutex);
247
248 return NGX_DECLINED;
249 }
250
251
252 static void
253 ngx_http_limit_req_delay(ngx_http_request_t *r)
254 {
255 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
256 "limit_req delay");
257
258 if (ngx_handle_read_event(r->connection->read, 0) != NGX_OK) {
259 ngx_http_finalize_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
260 return;
261 }
262
263 r->read_event_handler = ngx_http_block_reading;
264 r->write_event_handler = ngx_http_core_run_phases;
265
266 ngx_http_core_run_phases(r);
267 }
268
269
270 static void
271 ngx_http_limit_req_rbtree_insert_value(ngx_rbtree_node_t *temp,
272 ngx_rbtree_node_t *node, ngx_rbtree_node_t *sentinel)
273 {
274 ngx_rbtree_node_t **p;
275 ngx_http_limit_req_node_t *lzn, *lznt;
276
277 for ( ;; ) {
278
279 if (node->key < temp->key) {
280
281 p = &temp->left;
282
283 } else if (node->key > temp->key) {
284
285 p = &temp->right;
286
287 } else { /* node->key == temp->key */
288
289 lzn = (ngx_http_limit_req_node_t *) &node->color;
290 lznt = (ngx_http_limit_req_node_t *) &temp->color;
291
292 p = (ngx_memn2cmp(lzn->data, lznt->data, lzn->len, lznt->len) < 0)
293 ? &temp->left : &temp->right;
294 }
295
296 if (*p == sentinel) {
297 break;
298 }
299
300 temp = *p;
301 }
302
303 *p = node;
304 node->parent = temp;
305 node->left = sentinel;
306 node->right = sentinel;
307 ngx_rbt_red(node);
308 }
309
310
311 static ngx_int_t
312 ngx_http_limit_req_lookup(ngx_http_limit_req_conf_t *lzcf, ngx_uint_t hash,
313 u_char *data, size_t len, ngx_http_limit_req_node_t **lzp)
314 {
315 ngx_int_t rc;
316 ngx_time_t *tp;
317 ngx_msec_t now;
318 ngx_msec_int_t ms;
319 ngx_rbtree_node_t *node, *sentinel;
320 ngx_http_limit_req_ctx_t *ctx;
321 ngx_http_limit_req_node_t *lz;
322
323 ctx = lzcf->shm_zone->data;
324
325 node = ctx->rbtree->root;
326 sentinel = ctx->rbtree->sentinel;
327
328 while (node != sentinel) {
329
330 if (hash < node->key) {
331 node = node->left;
332 continue;
333 }
334
335 if (hash > node->key) {
336 node = node->right;
337 continue;
338 }
339
340 /* hash == node->key */
341
342 do {
343 lz = (ngx_http_limit_req_node_t *) &node->color;
344
345 rc = ngx_memn2cmp(data, lz->data, len, (size_t) lz->len);
346
347 if (rc == 0) {
348
349 tp = ngx_timeofday();
350
351 now = (ngx_msec_t) (tp->sec * 1000 + tp->msec);
352 ms = (ngx_msec_int_t) (now - lz->last);
353
354 lz->rate = lz->rate - ctx->rate * ngx_abs(ms) / 1000 + 1;
355
356 if (lz->rate < 0.0) {
357 lz->rate = 0.0;
358 }
359
360 lz->last = now;
361
362 *lzp = lz;
363
364 if (lz->rate > lzcf->burst) {
365 return NGX_BUSY;
366 }
367
368 if (lz->rate > ctx->rate) {
369 return NGX_AGAIN;
370 }
371
372 return NGX_OK;
373 }
374
375 node = (rc < 0) ? node->left : node->right;
376
377 } while (node != sentinel && hash == node->key);
378
379 break;
380 }
381
382 *lzp = NULL;
383
384 return NGX_DECLINED;
385 }
386
387
388 static void
389 ngx_http_limit_req_expire(ngx_http_limit_req_ctx_t *ctx, ngx_uint_t n)
390 {
391 float rate;
392 ngx_time_t *tp;
393 ngx_msec_t now;
394 ngx_queue_t *q;
395 ngx_msec_int_t ms;
396 ngx_rbtree_node_t *node;
397 ngx_http_limit_req_node_t *lz;
398
399 tp = ngx_timeofday();
400
401 now = (ngx_msec_t) (tp->sec * 1000 + tp->msec);
402
403 /*
404 * n == 1 deletes one or two zero rate entries
405 * n == 0 deletes oldest entry by force
406 * and one or two zero rate entries
407 */
408
409 while (n < 3) {
410
411 if (ngx_queue_empty(ctx->queue)) {
412 return;
413 }
414
415 q = ngx_queue_last(ctx->queue);
416
417 lz = ngx_queue_data(q, ngx_http_limit_req_node_t, queue);
418
419 if (n++ != 0) {
420
421 ms = (ngx_msec_int_t) (now - lz->last);
422 ms = ngx_abs(ms);
423
424 if (ms < 60000) {
425 return;
426 }
427
428 rate = lz->rate - ctx->rate * ms / 1000;
429
430 if (rate > 0.0) {
431 return;
432 }
433 }
434
435 ngx_queue_remove(q);
436
437 node = (ngx_rbtree_node_t *)
438 ((u_char *) lz - offsetof(ngx_rbtree_node_t, color));
439
440 ngx_rbtree_delete(ctx->rbtree, node);
441
442 ngx_slab_free_locked(ctx->shpool, node);
443 }
444 }
445
446
447 static ngx_int_t
448 ngx_http_limit_req_init_zone(ngx_shm_zone_t *shm_zone, void *data)
449 {
450 ngx_http_limit_req_ctx_t *octx = data;
451
452 ngx_rbtree_node_t *sentinel;
453 ngx_http_limit_req_ctx_t *ctx;
454
455 ctx = shm_zone->data;
456
457 if (octx) {
458 if (ngx_strcmp(ctx->var.data, octx->var.data) != 0) {
459 ngx_log_error(NGX_LOG_EMERG, shm_zone->shm.log, 0,
460 "limit_req \"%V\" uses the \"%V\" variable "
461 "while previously it used the \"%V\" variable",
462 &shm_zone->name, &ctx->var, &octx->var);
463 return NGX_ERROR;
464 }
465
466 ctx->rbtree = octx->rbtree;
467 ctx->queue = octx->queue;
468 ctx->shpool = octx->shpool;
469
470 return NGX_OK;
471 }
472
473 ctx->shpool = (ngx_slab_pool_t *) shm_zone->shm.addr;
474
475 ctx->rbtree = ngx_slab_alloc(ctx->shpool, sizeof(ngx_rbtree_t));
476 if (ctx->rbtree == NULL) {
477 return NGX_ERROR;
478 }
479
480 sentinel = ngx_slab_alloc(ctx->shpool, sizeof(ngx_rbtree_node_t));
481 if (sentinel == NULL) {
482 return NGX_ERROR;
483 }
484
485 ngx_rbtree_init(ctx->rbtree, sentinel,
486 ngx_http_limit_req_rbtree_insert_value);
487
488 ctx->queue = ngx_slab_alloc(ctx->shpool, sizeof(ngx_queue_t));
489 if (ctx->queue == NULL) {
490 return NGX_ERROR;
491 }
492
493 ngx_queue_init(ctx->queue);
494
495 return NGX_OK;
496 }
497
498
499 static void *
500 ngx_http_limit_req_create_conf(ngx_conf_t *cf)
501 {
502 ngx_http_limit_req_conf_t *conf;
503
504 conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_limit_req_conf_t));
505 if (conf == NULL) {
506 return NGX_CONF_ERROR;
507 }
508
509 /*
510 * set by ngx_pcalloc():
511 *
512 * conf->shm_zone = NULL;
513 * conf->burst = 0.0;
514 * conf->delay = 0;
515 */
516
517 return conf;
518 }
519
520
521 static char *
522 ngx_http_limit_req_merge_conf(ngx_conf_t *cf, void *parent, void *child)
523 {
524 ngx_http_limit_req_conf_t *prev = parent;
525 ngx_http_limit_req_conf_t *conf = child;
526
527 if (conf->shm_zone == NULL) {
528 *conf = *prev;
529 }
530
531 return NGX_CONF_OK;
532 }
533
534
535 static char *
536 ngx_http_limit_req_zone(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
537 {
538 u_char *p;
539 size_t size, len;
540 ngx_str_t *value, name, s;
541 ngx_int_t rate, scale;
542 ngx_uint_t i;
543 ngx_shm_zone_t *shm_zone;
544 ngx_http_limit_req_ctx_t *ctx;
545
546 value = cf->args->elts;
547
548 ctx = NULL;
549 size = 0;
550 rate = 1;
551 scale = 1;
552 name.len = 0;
553
554 for (i = 1; i < cf->args->nelts; i++) {
555
556 if (ngx_strncmp(value[i].data, "zone=", 5) == 0) {
557
558 name.data = value[i].data + 5;
559
560 p = (u_char *) ngx_strchr(name.data, ':');
561
562 if (p) {
563 name.len = p - name.data;
564
565 p++;
566
567 s.len = value[i].data + value[i].len - p;
568 s.data = p;
569
570 size = ngx_parse_size(&s);
571 if (size > 8191) {
572 continue;
573 }
574 }
575
576 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
577 "invalid zone size \"%V\"", &value[i]);
578 return NGX_CONF_ERROR;
579 }
580
581 if (ngx_strncmp(value[i].data, "rate=", 5) == 0) {
582
583 len = value[i].len;
584 p = value[i].data + len - 3;
585
586 if (ngx_strncmp(p, "r/s", 3) == 0) {
587 scale = 1;
588 len -= 3;
589
590 } else if (ngx_strncmp(p, "r/m", 3) == 0) {
591 scale = 60;
592 len -= 3;
593 }
594
595 rate = ngx_atoi(value[i].data + 5, len - 5);
596 if (rate <= NGX_ERROR) {
597 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
598 "invalid rate \"%V\"", &value[i]);
599 return NGX_CONF_ERROR;
600 }
601
602 continue;
603 }
604
605 if (value[i].data[0] == '$') {
606
607 value[i].len--;
608 value[i].data++;
609
610 ctx = ngx_pcalloc(cf->pool, sizeof(ngx_http_limit_req_ctx_t));
611 if (ctx == NULL) {
612 return NGX_CONF_ERROR;
613 }
614
615 ctx->index = ngx_http_get_variable_index(cf, &value[i]);
616 if (ctx->index == NGX_ERROR) {
617 return NGX_CONF_ERROR;
618 }
619
620 ctx->var = value[i];
621
622 continue;
623 }
624
625 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
626 "invalid parameter \"%V\"", &value[i]);
627 return NGX_CONF_ERROR;
628 }
629
630 if (name.len == 0 || size == 0) {
631 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
632 "\"%V\" must have \"zone\" parameter",
633 &cmd->name);
634 return NGX_CONF_ERROR;
635 }
636
637 if (ctx == NULL) {
638 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
639 "no variable is defined for limit_req_zone \"%V\"",
640 &cmd->name);
641 return NGX_CONF_ERROR;
642 }
643
644 ctx->rate = (float) rate / scale;
645
646 shm_zone = ngx_shared_memory_add(cf, &name, size,
647 &ngx_http_limit_req_module);
648 if (shm_zone == NULL) {
649 return NGX_CONF_ERROR;
650 }
651
652 if (shm_zone->data) {
653 ctx = shm_zone->data;
654
655 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
656 "limit_req_zone \"%V\" is already bound to variable \"%V\"",
657 &value[1], &ctx->var);
658 return NGX_CONF_ERROR;
659 }
660
661 shm_zone->init = ngx_http_limit_req_init_zone;
662 shm_zone->data = ctx;
663
664 return NGX_CONF_OK;
665 }
666
667
668 static char *
669 ngx_http_limit_req(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
670 {
671 ngx_http_limit_req_conf_t *lzcf = conf;
672
673 u_char *p;
674 size_t len;
675 ngx_int_t burst, scale, delay;
676 ngx_str_t *value, s;
677 ngx_uint_t i;
678 ngx_http_limit_req_ctx_t *ctx;
679
680 if (lzcf->shm_zone) {
681 return "is duplicate";
682 }
683
684 value = cf->args->elts;
685
686 burst = 0;
687 scale = 1;
688 delay = 0;
689
690 for (i = 1; i < cf->args->nelts; i++) {
691
692 if (ngx_strncmp(value[i].data, "zone=", 5) == 0) {
693
694 s.len = value[i].len - 5;
695 s.data = value[i].data + 5;
696
697 lzcf->shm_zone = ngx_shared_memory_add(cf, &s, 0,
698 &ngx_http_limit_req_module);
699 if (lzcf->shm_zone == NULL) {
700 return NGX_CONF_ERROR;
701 }
702
703 continue;
704 }
705
706 if (ngx_strncmp(value[i].data, "burst=", 6) == 0) {
707
708 len = value[i].len;
709 p = value[i].data + len - 3;
710
711 if (ngx_strncmp(p, "r/s", 3) == 0) {
712 scale = 1;
713 len -= 3;
714
715 } else if (ngx_strncmp(p, "r/m", 3) == 0) {
716 scale = 60;
717 len -= 3;
718 }
719
720 burst = ngx_atoi(value[i].data + 6, len - 6);
721 if (burst <= 0) {
722 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
723 "invalid burst rate \"%V\"", &value[i]);
724 return NGX_CONF_ERROR;
725 }
726
727 continue;
728 }
729
730 if (ngx_strncmp(value[i].data, "delay=", 6) == 0) {
731
732 s.len = value[i].len - 6;
733 s.data = value[i].data + 6;
734
735 delay = ngx_parse_time(&s, 0);
736 if (delay < 0) {
737 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
738 "invalid clean_time value \"%V\"", &value[i]);
739 return NGX_CONF_ERROR;
740 }
741
742 continue;
743 }
744
745 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
746 "invalid parameter \"%V\"", &value[i]);
747 return NGX_CONF_ERROR;
748 }
749
750 if (lzcf->shm_zone == NULL) {
751 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
752 "\"%V\" must have \"zone\" parameter",
753 &cmd->name);
754 return NGX_CONF_ERROR;
755 }
756
757 if (lzcf->shm_zone->data == NULL) {
758 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
759 "unknown limit_req_zone \"%V\"",
760 &lzcf->shm_zone->name);
761 return NGX_CONF_ERROR;
762 }
763
764 if (burst) {
765 lzcf->burst = (float) burst / scale;
766
767 } else {
768 ctx = lzcf->shm_zone->data;
769 lzcf->burst = ctx->rate;
770 }
771
772 lzcf->delay = (ngx_msec_t) delay;
773
774 return NGX_CONF_OK;
775 }
776
777
778 static ngx_int_t
779 ngx_http_limit_req_init(ngx_conf_t *cf)
780 {
781 ngx_http_handler_pt *h;
782 ngx_http_core_main_conf_t *cmcf;
783
784 cmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_core_module);
785
786 h = ngx_array_push(&cmcf->phases[NGX_HTTP_PREACCESS_PHASE].handlers);
787 if (h == NULL) {
788 return NGX_ERROR;
789 }
790
791 *h = ngx_http_limit_req_handler;
792
793 return NGX_OK;
794 }