淘宝服务平台核心人员出品:Nginx开发从入门到精通
Nginx的官方网站:Nginx的英文官方网站
还有一篇好的博文:Nginx限流模块初探
1、ngx_http_limit_conn_module
先看看这个模块
The ngx_http_limit_conn_module
module is used to limit the number of connections per the defined key, in particular, the number of connections from a single IP address.
Not all connections are counted. A connection is counted only if it has a request being processed by the server and the whole request header has already been read.
Example Configuration
http { limit_conn_zone $binary_remote_addr zone=addr:10m; ... server { ... location /download/ { limit_conn addr 1; }
Directives
Syntax: | limit_conn |
---|---|
Default: | — |
Context: | http , server , location |
Sets the shared memory zone and the maximum allowed number of connections for a given key value. When this limit is exceeded, the server will return the error in reply to a request. For example, the directives
limit_conn_zone $binary_remote_addr zone=addr:10m; server { location /download/ { limit_conn addr 1; }
allow only one connection per an IP address at a time.
2、ngx_http_limit_req_module
再看看模块
The ngx_http_limit_req_module
module (0.7.21) is used to limit the request processing rate per a defined key, in particular, the processing rate of requests coming from a single IP address. The limitation is done using the “leaky bucket” method.
Example Configuration
http { limit_req_zone $binary_remote_addr zone=one:10m rate=1r/s; ... server { ... location /search/ { limit_req zone=one burst=5; }
Directives
Syntax: | limit_req |
---|---|
Default: | — |
Context: | http , server , location |
Sets the shared memory zone and the maximum burst size of requests. If the requests rate exceeds the rate configured for a zone, their processing is delayed such that requests are processed at a defined rate. Excessive requests are delayed until their number exceeds the maximum burst size in which case the request is terminated with an error. By default, the maximum burst size is equal to zero. For example, the directives
limit_req_zone $binary_remote_addr zone=one:10m rate=1r/s; server { location /search/ { limit_req zone=one burst=5; }
allow not more than 1 request per second at an average, with bursts not exceeding 5 requests.