nginx 资源限制
限制连接速率
conf
http {
limit_conn_zone $binary_remote_addr zone=addr:10m;
server {
listen 80;
limit_conn addr 5; # 限制每个IP地址的并发连接数为5
location / {
...
}
}
}
限制请求速率
conf
http {
limit_req_zone $binary_remote_addr zone=one:10m rate=1r/s;
server {
listen 80;
location / {
limit_req zone=one burst=5; # 每秒允许的请求速率为1个,最大突发请求数为5
...
}
}
}
limit_rate
# 语法配置
Syntax: limit_rate rate;
Default: limit_rate 0;
Context: http, server, location, if in location
# 示例
limit_rate 4k;
提醒
限速指令的生效范围是根据每个连接确定的,例如上面限定每个连接的速率为4k,也就是当客户端发起两个连接的时候,速率就可以变为8k
limit_rate_after
超过设定值才限速
# 语法配置
Syntax: limit_rate_after size;
Default: limit_rate_after 0;
Context: http, server, location, if in location
This directive appeared in version 0.8.0.
# 示例
location /flv/ {
flv;
limit_rate_after 500k;
limit_rate 50k;
}