Nginx模块 ngx_http_limit_req_module 限制请求速率
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
- http {
- limit_req_zone $binary_remote_addr zone=one:10m rate=1r/s;
- ...
- server {
- ...
- location /search/ {
- limit_req zone=one burst=5;
- }
Directives指令:
- Syntax: limit_req zone=name [burst=number] [nodelay];
- Default: —
- 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。例如,以下指令配置:
- limit_req_zone $binary_remote_addr zone=one:10m rate=1r/s;
- server {
- location /search/ {
- limit_req zone=one burst=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状态码。
- 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的访问速率进行限制:
- limit_req_zone $binary_remote_addr zone=perip:10m rate=1r/s;
- limit_req_zone $server_name zone=perserver:10m rate=10r/s;
- server {
- ...
- limit_req zone=perip burst=5 nodelay;
- limit_req zone=perserver burst=10;
- }
These directives are inherited from the previous level if and only if there are no limit_req directives on the current level.
这些指令作用继承上级,如果当前级别中没有配置,会继承上一级的指令。
- Syntax: limit_req_log_level info | notice | warn | error;
- Default:
- limit_req_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 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” 是通知级别,那么,延迟请求只是信息的日志级别。
- Syntax: limit_req_status code;
- Default:
- limit_req_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返回状态码。
- Syntax: limit_req_zone key zone=name:size rate=rate;
- Default: —
- 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:
- 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 限制请求速率的更多相关文章
- Nginx优化防爬虫 限制http请求方法 CDN网页加速 架构优化 监牢模式 控制并发量以及客户端请求速率
Nginx防爬虫优化 Robots协议(也称为爬虫协议,机器人协议等)的全称是“网络爬虫排除标准”(Robots Exclusion Protocol),网站通过Robots协议告诉搜索引擎哪些页面可 ...
- Nginx模块之http.md
ngx_http_access_module ngx_http_access_module模块允许限制对某些客户端地址的访问. 访问也可以通过密码,子请求的结果或JWT来限制. 通过地址和密码的同时访 ...
- nginx限制请求之三:Nginx+Lua+Redis 对请求进行限制
相关文章: <高可用服务设计之二:Rate limiting 限流与降级> <nginx限制请求之一:(ngx_http_limit_conn_module)模块> <n ...
- nginx 模块简介
nginx模块一般分为5类:核心模块.标准http模块.可选http模块.邮件服务模块.第三方模块. 其中核心模块和标准http模块在快速编译之后就包含在了nginx中. 主要看一下核心模块.标准ht ...
- OpenResty / Nginx模块,Lua库和相关资源的列表
OpenResty / Nginx模块,Lua库和相关资源的列表 什么是OpenResty OpenResty是一个成熟的网络平台,它集成了标准的Nginx核心,LuaJIT,许多精心编写的Lua库, ...
- Nginx模块说明
一.Nginx内置模块 -–prefix= #指向安装目录 -–sbin-path #指向(执行)程序文件(nginx) -–conf-path= #指向配置文件(nginx.conf) -–erro ...
- 微信内嵌浏览器sessionid丢失问题,nginx ip_hash将所有请求转发到一台机器
现象微信中打开网页,图形验证码填写后,经常提示错误,即使填写正确也会提示错误,并且是间歇性出现. 系统前期,用户使用主要集中在pc浏览器中,一直没有出现这样的问题.近期有部分用户是在微信中访问的,才出 ...
- 结合源码看nginx-1.4.0之nginx模块组织结构详解
目录 0. 摘要 1. nginx模块组织结构 2. nginx模块数据结构 3. nginx模块初始化 4. 一个简单的http模块 5. 小结 6. 参考资料 0. 摘要 nginx有五大优点:模 ...
- 【转】Nginx模块开发入门
转自: http://kb.cnblogs.com/page/98352/ 结论:对Nginx模块开发入门做了一个helloworld的示例,简单易懂.也有一定的深度.值得一看. Nginx模块开发入 ...
随机推荐
- Transaction check error: file /etc/rpm/macros.ghc-srpm from install of redhat-rpm-config-9.1.0-80.el7.centos.noarch conflicts with file from package epel-release-6-8.noarch Error Summary ----------
./certbot-auto certonly 报错: Transaction check error: file /etc/rpm/macros.ghc-srpm from install of ...
- hihoCoder #1770 : 单调数(数位dp)
题面 我们定义一个数是单调数,当且仅当构成这个数每一个数位都是单调不降或不增的. 例如 \(123\) 和 \(321\) 和 \(221\) 和 \(111\) 是单调的,而 \(312\) 不是单 ...
- AtCoder Grand Contest 012
AtCoder Grand Contest 012 A - AtCoder Group Contest 翻译 有\(3n\)个人,每一个人有一个强大值(看我的假翻译),每三个人可以分成一组,一组的强大 ...
- BSGS与扩展BSGS
BSGS \(BSGS\)算法又称大步小步\((Baby-Step-Giant-Step)\)算法 \(BSGS\)算法主要用于解以下同余方程 \[A^x\equiv B(mod\ p)\]其中\(( ...
- Atcoder Tenka1 Programmer Contest 2019
C 签到题,f[i][0/1]表示以i结尾最后一个为白/黑的最小值,转移显然. #include<bits/stdc++.h> using namespace std; ; ]; char ...
- Dubbo2.6.5入门——简单的HelloWorld
建立父工程 打开idea,新建一个空的maven工程,作为整个项目的父工程. <?xml version="1.0" encoding="UTF-8"?& ...
- prometheus 标签使用
标签的配置使用 考虑到要明智地使用标签,我们需要给事物重新命名.在一个集中的.复杂的监视环境中,我们有时无法控制正在监视的所有资源以及它们公开的监视数据.重新标记允许在自己的环境中控制.管理和潜在地标 ...
- 关于ddl(新增字段)对数据库锁表|读写操作的影响_资料
1.对一个表执行ddl(新增字段)会不会阻塞表,影响读写? 在一次项目升级之前需要执行一个新增字段的脚本(alter table...),表的数据量是260多万,执行时间是72秒,感觉略长,不知道会不 ...
- idea设置java内存
-Xms1024m -Xmx10240m -XX:MaxPermSize=512m 设置环境变量JAVA_OPTS="-server -Xms800m -Xmx800m -XX:PermSi ...
- HDU 1049(蠕虫爬井 **)
题意是一只虫子在深度为 n 的井中,每分钟向上爬 u 单位,下一分钟会下滑 d 单位,问几分钟能爬出井. 本人是直接模拟的,这篇博客的分析比较好一些,应当学习这种分析问题的思路:http://www. ...