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. <Android基础> (五) 广播机制

    1)接收系统广播:a.动态注册监听网络变化 b.静态注册实现开机启动 2)发送自定义广播:a.发送标准广播 b.发送有序广播 3)使用本地广播 第五章 5.1 广播机制 Android中的每个程序都可 ...

  2. django restframework permission

    与 authentication 和 throttling 一起,permission 决定是应该接受还是拒绝访问请求.权限检查总是在视图的最开始处运行,在任何其他代码被允许进行之前.权限检查通常会使 ...

  3. BZOJ2870 最长道路

    题意:给定树,有点权.求一条路径使得最小点权 * 总点数最大.只需输出这个最大值.5w. 解:树上路径问题,点分治. 考虑合并两个子树的时候,答案的形式是val1 * (d1 + d2),当1是新插入 ...

  4. jvm学习笔记二(减少GC开销的建议)

    一:触发主GC(Garbage Collector)的条件 JVM进行次GC的频率很高,但因为这种GC占用时间极短,所以对系统产生的影响不大.更值得关注的是主GC的触发条件,因为它对系统影响很明显.总 ...

  5. Access-Control-Allow-Origin跨域请求处理

    今天在看新项目的时候,发现很多的   Controller 中都有一个 response.setHeader("Access-Control-Allow-Origin"," ...

  6. 网页三剑客之JS

    1.javascrapt介绍 js概述 JavaScript是运行在浏览器端的脚步语言,JavaScript主要解决的是前端与用户交互的问题,包括使用交互与数据交互. JavaScript是浏览器解释 ...

  7. Slow ReadProcessor&amp;Error Slow BlockReceiver错误日志分析(转)

    1.总结 "Slow ReadProcessor" 和"Slow BlockReceiver"往往是因为集群负载比较高或者某些节点不健康导致的,本文主要是帮助你 ...

  8. I/O模型之三:两种高性能 I/O 设计模式 Reactor 和 Proactor

    目录: <I/O模型之一:Unix的五种I/O模型> <I/O模型之二:Linux IO模式及 select.poll.epoll详解> <I/O模型之三:两种高性能 I ...

  9. JavaScript 基础六 'use strict'严格模式下的规则

    why 严格模式 [1] 消除js语法的一些不合理.不严谨.不安全问题,减少怪异行为并保证代码运行安全 [2] 提高编译器效率,增加运行速度 使用 [1]整个脚本启用严格模式,在顶部执行:" ...

  10. [物理学与PDEs]第3章第2节 磁流体力学方程组 2.3 磁流体力学方程组

    1.  磁流体力学方程组 $$\beex \bea \cfrac{\p {\bf H}}{\p t} &-\rot({\bf u}\times{\bf H})=\cfrac{1}{\sigma ...