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.

ngx_http_limit_req_module 模块用于限制对每个定义键的请求处理速率,例如,单客户端IP的每秒请求数。实现的原理是使用“漏桶”原理。

Example Configuration:

[plain] view plain copy

  1. http {
  2. limit_req_zone $binary_remote_addr zone=one:10m rate=1r/s;
  3. ...
  4. server {
  5. ...
  6. location /search/ {
  7. limit_req zone=one burst=5;
  8. }

Directives指令:

  1. Syntax: limit_req zone=name [burst=number] [nodelay];
  2. Default:    —
  3. 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 503 (Service Temporarily Unavailable). By default, the maximum burst size is equal to zero. For example, the directives
设置共享内存区,以及请求最大数。当请求的速率大于配置的速率,那么这些请求将会被延迟处理。如果,有过多的请求被延迟,超过了最大的限制,服务器将返回503状态码。默认情况下,最大限制为0。例如,以下指令配置:

  1. limit_req_zone $binary_remote_addr zone=one:10m rate=1r/s;
  2. server {
  3. location /search/ {
  4. limit_req zone=one burst=5;
  5. }

allow not more than 1 request per second at an average, with bursts not exceeding 5 requests.
允许1秒钟不超过1个请求,最大延迟请求数量不大于5.

If delaying of excessive requests while requests are being limited is not desired, the parameter nodelay should be used:
如果请求不需要被延迟,添加nodelay参数,服务器会立刻返回503状态码。

  1. limit_req zone=one burst=5 nodelay;

There could be several limit_req directives. For example, the following configuration will limit the processing rate of requests coming from a single IP address and, at the same time, the request processing rate by the virtual server:
这里列出以下limit_req的指令。例如,接下来的配置,会对当前站点单个IP的访问速率进行限制:

  1. limit_req_zone $binary_remote_addr zone=perip:10m rate=1r/s;
  2. limit_req_zone $server_name zone=perserver:10m rate=10r/s;
  3. server {
  4. ...
  5. limit_req zone=perip burst=5 nodelay;
  6. limit_req zone=perserver burst=10;
  7. }

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

  1. Syntax: limit_req_log_level info | notice | warn | error;
  2. Default:
  3. limit_req_log_level error;
  4. Context:    http, server, location
  5. This directive appeared in version 0.8.18.

Sets the desired logging level for cases when the server refuses to process requests due to rate exceeding, or delays request processing. Logging level for delays is one point less than for refusals; for example, if “limit_req_log_level notice” is specified, delays are logged with the info level.
设置服务器拒绝请求速率,或者延迟处理的情况下的日志级别。对于延迟的请求来说,日志的级别比拒绝请求要低。例如,“limit_req_log_level notice” 是通知级别,那么,延迟请求只是信息的日志级别。

  1. Syntax: limit_req_status code;
  2. Default:
  3. limit_req_status 503;
  4. Context:    http, server, location
  5. This directive appeared in version 1.3.15.

Sets the status code to return in response to rejected requests.
设置拒绝请求时候的http返回状态码。

  1. Syntax: limit_req_zone key zone=name:size rate=rate;
  2. Default:    —
  3. Context:    http

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

Usage example:

  1. limit_req_zone $binary_remote_addr zone=one:10m rate=1r/s;

Here, the states are kept in a 10 megabyte zone “one”, and an average request processing rate for this zone cannot exceed 1 request per second.
以上的配置,设置了名为“one”的存储区,大小为10兆字节,请求速率为每个客户端IP每秒1个请求。

A client IP address serves as a key. Note that instead of $remote_addr, the $binary_remote_addr variable is used here, that allows decreasing the state size down to 64 bytes. One megabyte zone can keep 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状态码。

The rate is specified in requests per second (r/s). If a rate of less than one request per second is desired, it is specified in request per minute (r/m). For example, half-request per second is 30r/m.
速率是指每秒的请求指定速率,如果要设置每秒请求低于一个请求的速率,可以设置为r/每分钟,例如30r/m

Nginx模块 ngx_http_limit_req_module 限制请求速率的更多相关文章

  1. Nginx优化防爬虫 限制http请求方法 CDN网页加速 架构优化 监牢模式 控制并发量以及客户端请求速率

    Nginx防爬虫优化 Robots协议(也称为爬虫协议,机器人协议等)的全称是“网络爬虫排除标准”(Robots Exclusion Protocol),网站通过Robots协议告诉搜索引擎哪些页面可 ...

  2. Nginx模块之http.md

    ngx_http_access_module ngx_http_access_module模块允许限制对某些客户端地址的访问. 访问也可以通过密码,子请求的结果或JWT来限制. 通过地址和密码的同时访 ...

  3. nginx限制请求之三:Nginx+Lua+Redis 对请求进行限制

    相关文章: <高可用服务设计之二:Rate limiting 限流与降级> <nginx限制请求之一:(ngx_http_limit_conn_module)模块> <n ...

  4. nginx 模块简介

    nginx模块一般分为5类:核心模块.标准http模块.可选http模块.邮件服务模块.第三方模块. 其中核心模块和标准http模块在快速编译之后就包含在了nginx中. 主要看一下核心模块.标准ht ...

  5. OpenResty / Nginx模块,Lua库和相关资源的列表

    OpenResty / Nginx模块,Lua库和相关资源的列表 什么是OpenResty OpenResty是一个成熟的网络平台,它集成了标准的Nginx核心,LuaJIT,许多精心编写的Lua库, ...

  6. Nginx模块说明

    一.Nginx内置模块 -–prefix= #指向安装目录 -–sbin-path #指向(执行)程序文件(nginx) -–conf-path= #指向配置文件(nginx.conf) -–erro ...

  7. 微信内嵌浏览器sessionid丢失问题,nginx ip_hash将所有请求转发到一台机器

    现象微信中打开网页,图形验证码填写后,经常提示错误,即使填写正确也会提示错误,并且是间歇性出现. 系统前期,用户使用主要集中在pc浏览器中,一直没有出现这样的问题.近期有部分用户是在微信中访问的,才出 ...

  8. 结合源码看nginx-1.4.0之nginx模块组织结构详解

    目录 0. 摘要 1. nginx模块组织结构 2. nginx模块数据结构 3. nginx模块初始化 4. 一个简单的http模块 5. 小结 6. 参考资料 0. 摘要 nginx有五大优点:模 ...

  9. 【转】Nginx模块开发入门

    转自: http://kb.cnblogs.com/page/98352/ 结论:对Nginx模块开发入门做了一个helloworld的示例,简单易懂.也有一定的深度.值得一看. Nginx模块开发入 ...

随机推荐

  1. 题解-洛谷P1601 A+B Problem(高精)

    https://www.luogu.org/problemnew/show/P1601(题目传送) 显然数据范围超过了long long类型,故不能简单的用两个长整型存起来相加.这里用到大数据的高精度 ...

  2. null引用,有时候是实现了父类的方法,方法体没写任何实现

    null引用,有时候是实现了父类的方法,方法体没写任何实现 /* @Override public void attachView(MonthListContract.View view) { } * ...

  3. Tensorflow基本语法

    一.tf.Variables() import tensorflow as tf Weights = tf.Variable(tf.random_uniform([1], -1.0, 1.0)) se ...

  4. Hadoop生态圈-zookeeper完全分布式部署

    Hadoop生态圈-zookeeper完全分布式部署 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 本篇博客部署是建立在Hadoop高可用基础之上的,关于Hadoop高可用部署请参 ...

  5. TCP/IP教程

    一.TCP/IP 简介 TCP/IP 是用于因特网的通信协议. 通信协议是对计算机必须遵守的规则的描述,只有遵守这些规则,计算机之间才能进行通信. 什么是 TCP/IP? TCP/IP 是供已连接因特 ...

  6. sessionStorage:写入记事本功能[内容写入sessionStorage中,读取,删除]

    知识点: 1.设置sessionStorage----setItem:sessionStorage.setItem(key,data); 存储数据使用key是唯一,不可重复,每触发都生成:如用一个固定 ...

  7. Linux 踩过的坑系列-01

    关于默认网关的添加.记得楼主之前有一次,无意之间,也不知道做了什么删除文件里面内容的操作,配置好静态IP之后上不了外网.翻阅个各种资料都是没有找到问题.最后发现问题在于配置的虚拟机网卡文件的网关打错了 ...

  8. Go 环境配置

    1.下载SDK 官网:https://golang.google.cn/ go中文在线文档:https://studygolang.com/pkgdoc 2.配置环境变量 windows使用.msi一 ...

  9. Problem B: Battle Royale(简单几何)

     题目链接: B - Battle Royale  Gym - 102021B 题目大意:给你两个坐标,表示起点和终点,然后给你两个圆,第一个圆包含两个圆,然后问你起点到终点的最短距离(不经过第二个圆 ...

  10. Django跨域请求

    一.jsonp方式 同源策略会阻止ajaxa请求,但不阻止src. jsonp方式其实是利用了<script>标签可以直接跨域的性质,在body中生成一个<script>标签, ...