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 模块介绍的更多相关文章

  1. OpenResty之 limit.count 模块

    原文: lua-resty-limit-traffic/lib/resty/limit/count.md 1. 示例 http { lua_shared_dict my_limit_count_sto ...

  2. python模块介绍- multi-mechanize 性能测试工具

    python模块介绍- multi-mechanize 性能测试工具 2013-09-13 磁针石 #承接软件自动化实施与培训等gtalk:ouyangchongwu#gmail.comqq 3739 ...

  3. Python第五章__模块介绍,常用内置模块

    Python第五章__模块介绍,常用内置模块 欢迎加入Linux_Python学习群  群号:478616847 目录: 模块与导入介绍 包的介绍 time &datetime模块 rando ...

  4. [Node.js与数据库]node-mysql 模块介绍

    [Node.js与数据库]node-mysql 模块介绍   转载至:https://itbilu.com/nodejs/npm/NyPG8LhlW.html#multiple-statement-q ...

  5. Python编程中 re正则表达式模块 介绍与使用教程

    Python编程中 re正则表达式模块 介绍与使用教程 一.前言: 这篇文章是因为昨天写了一篇 shell script 的文章,在文章中俺大量调用多媒体素材与网址引用.这样就会有一个问题就是:随着俺 ...

  6. 简学Python第五章__模块介绍,常用内置模块

    Python第五章__模块介绍,常用内置模块 欢迎加入Linux_Python学习群  群号:478616847 目录: 模块与导入介绍 包的介绍 time &datetime模块 rando ...

  7. ASP.NET Core模块化前后端分离快速开发框架介绍之3、数据访问模块介绍

    源码 GitHub:https://github.com/iamoldli/NetModular 演示地址 地址:https://nm.iamoldli.com 账户:admin 密码:admin 前 ...

  8. Nginx核心流程及模块介绍

    Nginx核心流程及模块介绍 1. Nginx简介以及特点 Nginx简介: Nginx (engine x) 是一个高性能的web服务器和反向代理服务器,也是一个IMAP/POP3/SMTP服务器 ...

  9. openresty开发系列21--lua的模块

    openresty开发系列21--lua的模块 从lua5.1开始,Lua 加入了标准的模块管理机制,Lua 的模块是由变量.函数等已知元素组成的 table, 因此创建一个模块很简单,就是创建一个 ...

  10. openresty开发系列10--openresty的简单介绍及安装

    openresty开发系列10--openresty的简单介绍及安装 一.Nginx优点 十几年前,互联网没有这么火,软件外包开发,信息化建设,帮助企业做无纸化办公,收银系统,工厂erp,c/s架构偏 ...

随机推荐

  1. LaTeX 交叉引用的四次编译

    编译包含交叉引用的 LaTeX 文件需要编译四次(pdflatex + bibtex + pdflatex * 2),一直对这四次编译都干了什么事很好奇.这次就来看一下每一步具体都干了些什么. 源文件 ...

  2. dataX是阿里开源的离线数据库同步工具

    dataX是阿里开源的离线数据库同步工具的使用 DataX介绍: DataX 是阿里开源的一个异构数据源离线同步工具,致力于实现包括关系型数据库(MySQL.Oracle等).HDFS.Hive.OD ...

  3. Android Camera2Video整合到自己项目里

    背景: Android项目里调用摄像头拍摄视频,原本使用的 MediaStore.ACTION_VIDEO_CAPTURE, 后来因项目需要,改成了camera2 1.Camera2Video 官方d ...

  4. win10环境下 jdk8安装点击下一步没反应解决办法

    win10 安装 jdk8 这个界面点下一步,每次总是没反应. jdk8官网下载的,要是第三方下载的铁定要怀疑是不是安装包问题了 解决方法:将输入法切换到系统自带的.(我一开始默认的是百度输入法) 这 ...

  5. UDT(一):概览

    1. 参考链接 官网 https://udt.sourceforge.io/ 谷博士对UDT的简单介绍 https://udt.sourceforge.io/doc/udt-2009.ppt 获取UD ...

  6. lxml官方入门教程(The lxml.etree Tutorial)翻译

    lxml官方入门教程(The lxml.etree Tutorial)翻译 说明: 首次发表日期:2024-09-05 官方教程链接: https://lxml.de/tutorial.html 使用 ...

  7. MDC – Work with Framework & Customize

    前言 在 MDC – Material Design, Angular Material, MDC, MWC, Lit 的关系 中, 我有提到基于 MDC 的 Framework 生态有多糟糕. 但它 ...

  8. AntDesign-Vue Table 查询与分页

    前言 之前的增删改查小 Demo 已经快要进行到最后一步了,这节的任务是将请求数据的方式改为 分页,并且增加 分页条件查询 的功能. 页面布局 <a-table :data-source=&qu ...

  9. dotnet实现多态的三种方法

    虚方法 virual 抽象方法 abstract 不能 new  不带方法体: 接口 Interface

  10. element设置table某个列的样式

    <el-table style="width: 100%;" height="250" :data="tableData" borde ...