Nginx模块 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.

ngx_http_limit_conn_module 模块用于限制每个定义的键的连接数,特别是每个IP的连接数。

Not all connections are counted. A connection is counted only if it has a request processed by the server and the whole request header has already been read.
对于每个http请求,只有当请求的header被完全读取的时候,才被计算作1个链接数。


Syntax:	limit_conn zone number;
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 503 (Service Temporarily Unavailable) error in reply to a request. For example, the directives
设置zone的共享内存大小,以及连接数的最大允许值。当超过这个限制的时候,服务器将返回503状态值。

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.
允许每个IP每个时间段内只能通过1个请求。

There could be several limit_conn directives. For example, the following configuration will limit the number of connections to the server per a client IP and, at the same time, the total number of connections to the virtual server:
这里有几个limit_conn的命令。例如,以下配置会限制每个IP的连接数,以及连接到服务器的连接总数。

limit_conn_zone $binary_remote_addr zone=perip:10m;
limit_conn_zone $server_name zone=perserver:10m;

server {
    ...
    limit_conn perip 10;
    limit_conn perserver 100;
}
These directives are inherited from the previous level if and only if there are no limit_conn directives on the current level.
这些指令作用继承上级,如果当前级别中没有配置,会继承上一级的指令。

Syntax:	limit_conn_log_level info | notice | warn | error;
Default:	
limit_conn_log_level error;
Context:	http, server, location
This directive appeared in version 0.8.18.
Sets the desired logging level for cases when the server limits the number of connections.
设置当前服务器限制连接数下所需的日志记录级别。

Syntax:	limit_conn_status code;
Default:	
limit_conn_status 503;
Context:	http, server, location
This directive appeared in version 1.3.15.
Sets the status code to return in response to rejected requests.
设置拒绝连接后响应的http状态码,默认是503.

Syntax:	limit_conn_zone key zone=name:size;
Default:	—
Context:	http
Sets parameters for a shared memory zone that will keep states for various keys. In particular, the state includes the current number of connections. The key can contain text, variables, and their combination. Requests with an empty key value are not accounted.
为各种定义的键配置共享内存大小,包括当前连接数。key可以是文本、变量、也可以是文本与变量结合,对于空的key请求,不会进行计数。

Usage example:
limit_conn_zone $binary_remote_addr zone=addr:10m;
Here, a client IP address serves as a key. Note that instead of $remote_addr, the $binary_remote_addr variable is used here. The $remote_addr variable’s size can vary from 7 to 15 bytes. The stored state occupies either 32 or 64 bytes of memory on 32-bit platforms and always 64 bytes on 64-bit platforms. The $binary_remote_addr variable’s size is always 4 bytes. The stored state always occupies 32 bytes on 32-bit platforms and 64 bytes on 64-bit platforms. One megabyte zone can keep about 32 thousand 32-byte states or about 16 thousand 64-byte states. If the zone storage is exhausted, the server will return the 503 (Service Temporarily Unavailable) error to all further requests.
以上的例子,客户端IP作为了一个键值。注意到这里使用了$binary_remote_addr,而不是$remote_addr。$remote_addr变量的大小在7到15个字节之间,存储占用32位平台上的32或64字节的内存,并且在64位平台上总是有64个字节内存。$binary_remote_addr变量总是占用4个字节内存,存储占用32位平台上的32字节的内存,并且在64位平台上总是有64个字节内存。一个存储区可以保存3200个32字节或1600个64字节。如果存储区满,服务器将对其他请求相应503状态码。

Syntax:	limit_zone name $variable size;
Default:	—
Context:	http
This directive was made obsolete in version 1.1.8 and was removed in version 1.7.6. An equivalent limit_conn_zone directive with a changed syntax should be used instead:
以下指令发布于版本1.1.8,并且在版本1.7.6中移除。等效limit_conn_zone 语法为:
limit_conn_zone $variable zone=name:size;









  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值