comparison src/http/modules/ngx_http_limit_req_module.c @ 502:89dc5654117c NGINX_0_7_63

nginx 0.7.63 *) Security: now "/../" are disabled in "Destination" request header line. *) Change: minimum supported OpenSSL version is 0.9.7. *) Change: the "ask" parameter of the "ssl_verify_client" directive was changed to the "optional" parameter and now it checks a client certificate if it was offered. Thanks to Brice Figureau. *) Feature: now the "-V" switch shows TLS SNI support. *) Feature: the $ssl_client_verify variable. Thanks to Brice Figureau. *) Feature: the "ssl_crl" directive. Thanks to Brice Figureau. *) Bugfix: the $ssl_client_cert variable usage corrupted memory; the bug had appeared in 0.7.7. Thanks to Sergey Zhuravlev. *) Feature: now the start cache loader runs in a separate process; this should improve large caches handling. *) Feature: now temporary files and permanent storage area may reside at different file systems. *) Bugfix: nginx counted incorrectly disk cache size. *) Change: now directive "gzip_disable msie6" does not disable gzipping for MSIE 6.0 SV1. *) Bugfix: nginx always added "Vary: Accept-Encoding" response header line, if both "gzip_static" and "gzip_vary" were on. *) Feature: the "proxy" parameter of the "geo" directive. *) Feature: the ngx_http_geoip_module. *) Feature: the "limit_rate_after" directive. Thanks to Ivan Debnar. *) Feature: the "limit_req_log_level" and "limit_conn_log_level" directives. *) Bugfix: now "limit_req" directive conforms to the leaky bucket algorithm. Thanks to Maxim Dounin. *) Bugfix: in ngx_http_limit_req_module. Thanks to Maxim Dounin. *) Bugfix: now nginx allows underscores in a request method. *) Bugfix: "proxy_pass_header" and "fastcgi_pass_header" directives did not pass to a client the "X-Accel-Redirect", "X-Accel-Limit-Rate", "X-Accel-Buffering", and "X-Accel-Charset" lines from backend response header. Thanks to Maxim Dounin. *) Bugfix: in handling "Last-Modified" and "Accept-Ranges" backend response header lines; the bug had appeared in 0.7.44. Thanks to Maxim Dounin. *) Feature: the "image_filter_transparency" directive. *) Feature: the "image_filter" directive supports variables for setting size. *) Bugfix: in PNG alpha-channel support in the ngx_http_image_filter_module. *) Bugfix: in transparency support in the ngx_http_image_filter_module. *) Feature: now several "perl_modules" directives may be used. *) Bugfix: ngx_http_perl_module responses did not work in subrequests. *) Bugfix: nginx sent '\0' in a "Location" response header line on MKCOL request. Thanks to Xie Zhenye. *) Bugfix: an "error_page" directive did not redirect a 413 error; the bug had appeared in 0.6.10. *) Bugfix: in memory allocation error handling. Thanks to Maxim Dounin and Kirill A. Korinskiy.
author Igor Sysoev <http://sysoev.ru>
date Mon, 26 Oct 2009 00:00:00 +0300
parents 549994537f15
children
comparison
equal deleted inserted replaced
501:dc87c92181c7 502:89dc5654117c
40 40
41 typedef struct { 41 typedef struct {
42 ngx_shm_zone_t *shm_zone; 42 ngx_shm_zone_t *shm_zone;
43 /* integer value, 1 corresponds to 0.001 r/s */ 43 /* integer value, 1 corresponds to 0.001 r/s */
44 ngx_uint_t burst; 44 ngx_uint_t burst;
45 ngx_uint_t nodelay;/* unsigned nodelay:1 */ 45 ngx_uint_t limit_log_level;
46 ngx_uint_t delay_log_level;
47
48 ngx_uint_t nodelay; /* unsigned nodelay:1 */
46 } ngx_http_limit_req_conf_t; 49 } ngx_http_limit_req_conf_t;
47 50
48 51
49 static void ngx_http_limit_req_delay(ngx_http_request_t *r); 52 static void ngx_http_limit_req_delay(ngx_http_request_t *r);
50 static ngx_int_t ngx_http_limit_req_lookup(ngx_http_limit_req_conf_t *lrcf, 53 static ngx_int_t ngx_http_limit_req_lookup(ngx_http_limit_req_conf_t *lrcf,
60 static char *ngx_http_limit_req(ngx_conf_t *cf, ngx_command_t *cmd, 63 static char *ngx_http_limit_req(ngx_conf_t *cf, ngx_command_t *cmd,
61 void *conf); 64 void *conf);
62 static ngx_int_t ngx_http_limit_req_init(ngx_conf_t *cf); 65 static ngx_int_t ngx_http_limit_req_init(ngx_conf_t *cf);
63 66
64 67
68 static ngx_conf_enum_t ngx_http_limit_req_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
65 static ngx_command_t ngx_http_limit_req_commands[] = { 77 static ngx_command_t ngx_http_limit_req_commands[] = {
66 78
67 { ngx_string("limit_req_zone"), 79 { ngx_string("limit_req_zone"),
68 NGX_HTTP_MAIN_CONF|NGX_CONF_TAKE3, 80 NGX_HTTP_MAIN_CONF|NGX_CONF_TAKE3,
69 ngx_http_limit_req_zone, 81 ngx_http_limit_req_zone,
75 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE123, 87 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE123,
76 ngx_http_limit_req, 88 ngx_http_limit_req,
77 NGX_HTTP_LOC_CONF_OFFSET, 89 NGX_HTTP_LOC_CONF_OFFSET,
78 0, 90 0,
79 NULL }, 91 NULL },
92
93 { ngx_string("limit_req_log_level"),
94 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
95 ngx_conf_set_enum_slot,
96 NGX_HTTP_LOC_CONF_OFFSET,
97 offsetof(ngx_http_limit_req_conf_t, limit_log_level),
98 &ngx_http_limit_req_log_levels },
80 99
81 ngx_null_command 100 ngx_null_command
82 }; 101 };
83 102
84 103
179 } else { 198 } else {
180 excess = 0; 199 excess = 0;
181 } 200 }
182 201
183 ngx_log_debug3(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, 202 ngx_log_debug3(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
184 "limit_req: %i %ui.%03ui", rc, excess / 1000, excess % 1000); 203 "limit_req: %i %ui.%03ui", rc, excess / 1000, excess % 1000);
185 204
186 if (rc == NGX_BUSY) { 205 if (rc == NGX_BUSY) {
187 ngx_shmtx_unlock(&ctx->shpool->mutex); 206 ngx_shmtx_unlock(&ctx->shpool->mutex);
188 207
189 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, 208 ngx_log_error(lrcf->limit_log_level, r->connection->log, 0,
190 "limiting requests, excess: %ui.%03ui by zone \"%V\"", 209 "limiting requests, excess: %ui.%03ui by zone \"%V\"",
191 excess / 1000, excess % 1000, &lrcf->shm_zone->shm.name); 210 excess / 1000, excess % 1000, &lrcf->shm_zone->shm.name);
192 211
193 return NGX_HTTP_SERVICE_UNAVAILABLE; 212 return NGX_HTTP_SERVICE_UNAVAILABLE;
194 } 213 }
198 217
199 if (lrcf->nodelay) { 218 if (lrcf->nodelay) {
200 return NGX_DECLINED; 219 return NGX_DECLINED;
201 } 220 }
202 221
203 ngx_log_error(NGX_LOG_WARN, r->connection->log, 0, 222 ngx_log_error(lrcf->delay_log_level, r->connection->log, 0,
204 "delaying request, excess: %ui.%03ui, by zone \"%V\"", 223 "delaying request, excess: %ui.%03ui, by zone \"%V\"",
205 excess / 1000, excess % 1000, &lrcf->shm_zone->shm.name); 224 excess / 1000, excess % 1000, &lrcf->shm_zone->shm.name);
206 225
207 if (ngx_handle_read_event(r->connection->read, 0) != NGX_OK) { 226 if (ngx_handle_read_event(r->connection->read, 0) != NGX_OK) {
208 return NGX_HTTP_INTERNAL_SERVER_ERROR; 227 return NGX_HTTP_INTERNAL_SERVER_ERROR;
261 280
262 281
263 static void 282 static void
264 ngx_http_limit_req_delay(ngx_http_request_t *r) 283 ngx_http_limit_req_delay(ngx_http_request_t *r)
265 { 284 {
285 ngx_event_t *wev;
286
266 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, 287 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
267 "limit_req delay"); 288 "limit_req delay");
289
290 wev = r->connection->write;
291
292 if (!wev->timedout) {
293
294 if (ngx_handle_write_event(wev, 0) != NGX_OK) {
295 ngx_http_finalize_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
296 }
297
298 return;
299 }
300
301 wev->timedout = 0;
268 302
269 if (ngx_handle_read_event(r->connection->read, 0) != NGX_OK) { 303 if (ngx_handle_read_event(r->connection->read, 0) != NGX_OK) {
270 ngx_http_finalize_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR); 304 ngx_http_finalize_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
271 return; 305 return;
272 } 306 }
366 400
367 if (excess < 0) { 401 if (excess < 0) {
368 excess = 0; 402 excess = 0;
369 } 403 }
370 404
405 if ((ngx_uint_t) excess > lrcf->burst) {
406 *lrp = lr;
407 return NGX_BUSY;
408 }
409
371 lr->excess = excess; 410 lr->excess = excess;
372 lr->last = now; 411 lr->last = now;
373 412
374 *lrp = lr; 413 *lrp = lr;
375
376 if ((ngx_uint_t) excess > lrcf->burst) {
377 return NGX_BUSY;
378 }
379 414
380 if (excess) { 415 if (excess) {
381 return NGX_AGAIN; 416 return NGX_AGAIN;
382 } 417 }
383 418
520 { 555 {
521 ngx_http_limit_req_conf_t *conf; 556 ngx_http_limit_req_conf_t *conf;
522 557
523 conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_limit_req_conf_t)); 558 conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_limit_req_conf_t));
524 if (conf == NULL) { 559 if (conf == NULL) {
525 return NGX_CONF_ERROR; 560 return NULL;
526 } 561 }
527 562
528 /* 563 /*
529 * set by ngx_pcalloc(): 564 * set by ngx_pcalloc():
530 * 565 *
531 * conf->shm_zone = NULL; 566 * conf->shm_zone = NULL;
532 * conf->burst = 0; 567 * conf->burst = 0;
533 * conf->nodelay = 0; 568 * conf->nodelay = 0;
534 */ 569 */
535 570
571 conf->limit_log_level = NGX_CONF_UNSET_UINT;
572
536 return conf; 573 return conf;
537 } 574 }
538 575
539 576
540 static char * 577 static char *
544 ngx_http_limit_req_conf_t *conf = child; 581 ngx_http_limit_req_conf_t *conf = child;
545 582
546 if (conf->shm_zone == NULL) { 583 if (conf->shm_zone == NULL) {
547 *conf = *prev; 584 *conf = *prev;
548 } 585 }
586
587 ngx_conf_merge_uint_value(conf->limit_log_level, prev->limit_log_level,
588 NGX_LOG_ERR);
589
590 conf->delay_log_level = (conf->limit_log_level == NGX_LOG_INFO) ?
591 NGX_LOG_INFO : conf->limit_log_level + 1;
549 592
550 return NGX_CONF_OK; 593 return NGX_CONF_OK;
551 } 594 }
552 595
553 596