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模块开发入 ...
随机推荐
- 压测过程中,获取不到redis连接池,发现redis连接数高
说明:图片截得比较大,浏览器放大倍数看即可(涉及到隐私,打了码,请见谅,如果有疑问,欢迎骚扰). 最近在压测过程中,出现获取不到redis连接池的问题 xshell连接redis服务器,查看连接数,发 ...
- Win下端口占用问题:OSError: [WinError 10013] 以一种访问权限不允许的方式做了一个访问套接字的尝试
常见问题:https://www.cnblogs.com/dotnetcrazy/p/9192089.html netstat -ano|findstr 8080 如果不计较端口,换个即可 也可以查找 ...
- Python学习day9 函数Ⅰ(基础)
函数Ⅰ(基础) 三目运算 基本结构 v = 前面 if 条件 else 后面 #条件为真v=前面,条件为假v=后面.#等同于if 条件: v = '前面'else: v = '后面' ...
- secureCRT自动断开的解决方法
转: secureCRT自动断开的解决方法 secureCRT自动断开的解决方法 在secureCRT上登录时,一段时间不用的话会自动断开,必须重新连接,有点麻烦. 有时候服务器端的 /etc/pro ...
- 第三十七节、人脸检测MTCNN和人脸识别Facenet(附源码)
在说到人脸检测我们首先会想到利用Harr特征提取和Adaboost分类器进行人脸检测(有兴趣的可以去一看这篇博客第九节.人脸检测之Haar分类器),其检测效果也是不错的,但是目前人脸检测的应用场景逐渐 ...
- BIOS翻译
BIOS翻译 BIOS(Basic Input/Output System—基本输入输出系统).BIOS可以视为是一个永久地记录在ROM中的一个软件 Main主要信息 :main 主要信息 advan ...
- Python动态语言的特性
一.动态语言相关概念 1.1 动态语言 在运行时代码可以根据某些条件改变自身结构 可以在运行时引进新的函数.对象.甚至代码,可以删除已有的函数等其他结构上的变化 常见的动态语言:Object-C.C# ...
- Entity Framework入门教程(11)---EF6中的异步查询和异步保存
EF6中的异步查询和异步保存 在.NET4.5中介绍了异步操作,异步操作在EF中也很有用,在EF6中我们可以使用DbContext的实例进行异步查询和异步保存. 1.异步查询 下边是一个通过L2E语法 ...
- [物理学与PDEs]第2章习题9 粘性流体动能的衰减
设 $\Omega\subset {\bf R}^3$ 为有界域, ${\bf u}$ 为 Navier-Stokes 方程组 (3. 4)-(3. 5) 满足边界条件 (3. 7) 的解, 其中体积 ...
- ASP.NET Web API 2 之路由配置
Ø 简介 ASP.NET Web API 路由配置也是必须掌握的技术点之一,要真正的完全掌握和理解它也是需要一定的过程的.不过,在平常的开发过程中,对它有基本的了解就足够了.因为我们主要关注点并不在 ...