OpenResty之resty.limit.count 模块介绍

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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+lua实现一级缓存

OpenResty之resty.limit.count 模块介绍

OpenResty之ngx.ssl

Nginx扩展之Openresty

Openresty之resty.limit.conn模块介绍

OpenResty之replace-filter-nginx-module