连接频率限制:limit_conn_module
请求频率限制:limit_req_module
HTTP协议的连接与请求:
HTTP请求建立在一次TCP连接基础上
一次TCP请求至少产生一次HTTP请求,也可以产生多次。
Syntax: limit_conn_zone key zone=name:size;
Default: ——
Context: http
Syntax: limit_conn zone number;
Default: ——
Context: http,server,location
Syntax: limit_req_zone key zone=name:size rate=rate;
Default: ——
Context:http
前面部分的语法和前面类似,rate表示的是请求的速率。
Syntax: limit_req zone=name [burst=number][nodelay];
Default:——
Context:http,server,location
测试一:
# 设置空间名为req_zone 大小为1m,速率为每秒1个,地址的二进制的客户端地址用于节省空间
limit_req_zone $binary_remote_addr zone=req_zone:1m rate=1r/s;
location / {
root /opt/app/code;
limit_req zone=req_zone;
index index.html index.htm;
}
测试二:
# 设置空间名为req_zone 大小为1m,速率为每秒1个,地址的二进制的客户端地址用于节省空间
limit_req_zone $binary_remote_addr zone=req_zone:1m rate=1r/s;
location / {
root /opt/app/code;
limit_req zone=req_zone burst=3 nodelay;
index index.html index.htm;
}
burst为3表示有三个延迟响应,起到缓冲作用,其他直接返回。
测试三:
limit_conn_zone $binary_remote_addr zone=conn_zone:1m;
location / {
root /opt/app/code;
limit_conn conn_zone 1;
index index.html index.htm;
}