OpenResty之resty.limit.count 模块介绍
resty.limit.count 模块介绍:
resty.limit.count 模块就是限制接口单位时间的请求数,
This module depends on lua-resty-core模块,所以要在openresty 的http标签端添加
nginx
init_by_lua_block {
require “resty.core”
}
同时resty.limit.count模块是在OpenResty 1.13.6.1+ 引入的
openresty下开启resty.limit.count此模块的流程如下:
1.要开启resty.core openresty内核模块
[root@VM_82_178_centos ~]# grep -C 1 'resty.core' /usr/local/openresty/nginx/conf/nginx.conf
init_by_lua_block {
require "resty.core"
}
注意:resty.core 此模块只能放到openresty的http标签,且只允许存在一个
2.nginx vhost配置文件:
[root@VM_82_178_centos vhost]# cat /usr/local/openresty/nginx/conf/vhost/limit_count.conf
server {
listen 80;
server_name 01limit.count.com;
index index.html index.htm index.php;
root /data/www/test;
location / {
access_by_lua_file /usr/local/openresty/nginx/conf/limit_lua/limit.count.lua;
default_type 'text/html';
#content_by_lua 'ngx.say("hello world")';
access_log /data/wwwlog/01ip_access.log ;
}
}
**3.配置resty.limit.count 模块的lua脚本: **
[root@VM_82_178_centos ~]# cat /usr/local/openresty/nginx/conf/limit_lua/limit.count.lua
local limit_count = require "resty.limit.count"
-- rate: 100 requests per 1s
local lim, err = limit_count.new("my_limit_count_store", 100, 1)
if not lim then
ngx.log(ngx.ERR, "failed to instantiate a resty.limit.count object: ", err)
return ngx.exit(500)
end
-- use the Authorization header as the limiting key
local key = ngx.req.get_headers()["Authorization"] or "public"
local delay, err = lim:incoming(key, true) if not delay then
if err == "rejected" then
ngx.header["X-RateLimit-Limit"] = "100"
ngx.header["X-RateLimit-Remaining"] = 0
--每秒请求超过100次的就报错403
return ngx.exit(403)
end
ngx.log(ngx.ERR, "failed to limit count: ", err) return ngx.exit(500)
end
local remaining = err ngx.header["X-RateLimit-Limit"] = "100"
ngx.header["X-RateLimit-Remaining"] = remaining
4.ab简单压测
采用ab软件简单压测如下:
ab -c 2 -n 50 -t 1 ‘http://01limit.count.com/2.html’
19.29.97.11 - - [04/Aug/2019:18:14:14 +0800] "GET /2.html HTTP/1.0" 200 9 "-" "ApacheBench/2.3"
19.29.97.11 - - [04/Aug/2019:18:14:14 +0800] "GET /2.html HTTP/1.0" 200 9 "-" "ApacheBench/2.3"
19.29.97.11 - - [04/Aug/2019:18:14:14 +0800] "GET /2.html HTTP/1.0" 200 9 "-" "ApacheBench/2.3"
19.29.97.11 - - [04/Aug/2019:18:14:14 +0800] "GET /2.html HTTP/1.0" 403 150 "-" "ApacheBench/2.3"
19.29.97.11 - - [04/Aug/2019:18:14:14 +0800] "GET /2.html HTTP/1.0" 403 150 "-" "ApacheBench/2.3"
19.29.97.11 - - [04/Aug/2019:18:14:14 +0800] "GET /2.html HTTP/1.0" 403 150 "-" "ApacheBench/2.3"
到此处resty.limit.count模块演示完成
OpenResty之resty.limit.count 模块介绍的更多相关文章
- OpenResty之 limit.count 模块
原文: lua-resty-limit-traffic/lib/resty/limit/count.md 1. 示例 http { lua_shared_dict my_limit_count_sto ...
- python模块介绍- multi-mechanize 性能测试工具
python模块介绍- multi-mechanize 性能测试工具 2013-09-13 磁针石 #承接软件自动化实施与培训等gtalk:ouyangchongwu#gmail.comqq 3739 ...
- Python第五章__模块介绍,常用内置模块
Python第五章__模块介绍,常用内置模块 欢迎加入Linux_Python学习群 群号:478616847 目录: 模块与导入介绍 包的介绍 time &datetime模块 rando ...
- [Node.js与数据库]node-mysql 模块介绍
[Node.js与数据库]node-mysql 模块介绍 转载至:https://itbilu.com/nodejs/npm/NyPG8LhlW.html#multiple-statement-q ...
- Python编程中 re正则表达式模块 介绍与使用教程
Python编程中 re正则表达式模块 介绍与使用教程 一.前言: 这篇文章是因为昨天写了一篇 shell script 的文章,在文章中俺大量调用多媒体素材与网址引用.这样就会有一个问题就是:随着俺 ...
- 简学Python第五章__模块介绍,常用内置模块
Python第五章__模块介绍,常用内置模块 欢迎加入Linux_Python学习群 群号:478616847 目录: 模块与导入介绍 包的介绍 time &datetime模块 rando ...
- ASP.NET Core模块化前后端分离快速开发框架介绍之3、数据访问模块介绍
源码 GitHub:https://github.com/iamoldli/NetModular 演示地址 地址:https://nm.iamoldli.com 账户:admin 密码:admin 前 ...
- Nginx核心流程及模块介绍
Nginx核心流程及模块介绍 1. Nginx简介以及特点 Nginx简介: Nginx (engine x) 是一个高性能的web服务器和反向代理服务器,也是一个IMAP/POP3/SMTP服务器 ...
- openresty开发系列21--lua的模块
openresty开发系列21--lua的模块 从lua5.1开始,Lua 加入了标准的模块管理机制,Lua 的模块是由变量.函数等已知元素组成的 table, 因此创建一个模块很简单,就是创建一个 ...
- openresty开发系列10--openresty的简单介绍及安装
openresty开发系列10--openresty的简单介绍及安装 一.Nginx优点 十几年前,互联网没有这么火,软件外包开发,信息化建设,帮助企业做无纸化办公,收银系统,工厂erp,c/s架构偏 ...
随机推荐
- 使用广播星历计算卫星坐标(Python)
前言 本代码为GNSS课程设计代码,仅供参考,使用的计算方法与公式均来源于王坚主编的<卫星定位原理与应用(第二版)>. 本代码计算结果可以通过下载精密星历进行比照,误差在1-10m左右. ...
- .NET 6.0 + WPF 使用 Prism 框架实现导航
前言 Prism 一个开源的框架,专门用于开发可扩展.模块化和可测试的企业级 XAML 应用程序,适用于 WPF(Windows Presentation Foundation)和 Xamarin F ...
- DOM – Web Animation API
前言 以前写过相关的文章 angular2 学习笔记 ( animation 动画 ).但在项目种很少用到 Web Animation. 体会不到它的精髓,目前的感觉是,它对比 CSS Animati ...
- SpringMVC——SSM整合——表现层数据封装
表现层数据封装 设置统一数据返回结果类 注意:Result类中的字段并不是固定的,可以根据需要自行增减提供若干个构造方法,方便操作 返回结果类 package com.cqupt.controller ...
- HTML——简介-入门
W3C标准:网页主要由三部分组成 结构:HTML 表现:CSS 行为:JavaScript HTML快速入门 1.新建文本文件,后缀改为 .html 2.编写HTML结构标签(不区分大小写) ...
- web架构-nginx负载均衡
nginx的负载均衡 Nginx 是一个广泛使用的反向代理服务器,能够高效地实现负载均衡.负载均衡的核心作用是将来自客户端的请求分发到多个后端服务器上,从而平衡每台服务器的压力.通过Nginx,我们可 ...
- 解锁Java线程池:实战技巧与陷阱规避
专业在线打字练习网站-巧手打字通,只输出有价值的知识. 一 前言 线程池作为初学者常感困惑的一个领域,本次"巧手打字通课堂"将深入剖析其中几个最为普遍的误区.为了更清晰地阐述这些知 ...
- 课时04:了解HTTP网络协议
什么是HTTP协议 HTTP(HyperText Transfer Protocol)叫超文本传输协议,它是web服务器和客户端直接进行数据传输的规则,是一个无状态的应用层协议. HTTP协议工作原理 ...
- eUSB是什么/可以干什么?
eUSB总结 1.什么是eUSB 1.1 eUSB概念 eUSB是原USB物理层上的补充,为了解决USB_phy低压需求的问题而出现的,eUSB可以将信号电平降至1.2V甚至更低,与此同时可以优化电源 ...
- NET Core 基础 - 删除字符串最后一个字符的七大类N种实现方式
今天想通过和大家分享如何删除字符串最后一个字符的N种实现方法,来回顾一些基础知识点. 01.第一类.字符串方式 这类方法是通过string类型自身方法直接实现. 1.Substring方法 相信大多数 ...