nginx lua redis 访问频率限制(转)

1. 需求分析

Nginx来处理访问控制的方法有多种,实现的效果也有多种,访问IP段,访问内容限制,访问频率限制等。

用Nginx+Lua+Redis来做访问限制主要是考虑到高并发环境下快速访问控制的需求。

Nginx处理请求的过程一共划分为11个阶段,分别是:

post-read、server-rewrite、find-config、rewrite、post-rewrite、 preaccess、access、post-access、try-files、content、log.

在openresty中,可以找到:

set_by_lua,access_by_lua,content_by_lua,rewrite_by_lua等方法。

那么访问控制应该是,access阶段。

解决方案

按照正常的逻辑思维,我们会想到的访问控制方案如下:

1.检测是否被forbidden?
=》是,forbidden是否到期:是,清除记录,返回200,正常访问;否,返回403;
=》否,返回200,正常访问

2.每次访问,访问用户的访问频率+1处理

3.检测访问频率是否超过限制,超过即添加forbidden记录,返回403

这是简单地方案,还可以添加点枝枝叶叶,访问禁止时间通过算法导入,每次凹曲线增加。

实现方法

首先为nginx添加vhost配置文件,vhost.conf部分内容如下:

 

1

2

3

4

5

6

7

8

9

10

11

12

13

14

lua_package_path "/usr/local/openresty/lualib/?.lua;;";#告诉openresty库地址

lua_package_cpath "/usr/local/openresty/lualib/?.so;;";

error_log /usr/local/openresty/nginx/logs/openresty.debug.log debug;

 

server {

    listen 8080 default;

    server_name www.ttlsa.com;    

    root  /www/openresty;

 

    location /login {

        default_type 'text/html';

        access_by_lua_file "/usr/local/openresty/nginx/lua/access_by_redis.lua";#通过lua来处理访问控制

    }

}

 

Access_by_redis.lua

参考了下v2ex.com的做法,redis存储方案只做简单地string存储就足够了。key分别是:

用户登录记录:user:127.0.0.1:time(unix时间戳)
访问限制:block:127.0.0.1

先连接Redis吧:

 

1

2

3

4

5

6

7

8

local red = redis:new()

function M:redis()

red:set_timeout(1000)

local ok, err = red:connect("127.0.0.1", 6379)

if not ok then

ngx.exit(ngx.HTTP_INTERNAL_SERVER_ERROR)

end

end

按照我们的逻辑方案,第二步是,检测是否forbidden,下面我们就检测block:127.0.0.1,如果搜索到数据,检测时间是否过期,未过期返回403,否则直接返回200:

 

1

2

3

4

5

6

7

8

9

10

11

12

13

function M:check1()

local time=os.time() --system time

local res, err = red:get("block:"..ngx.var.remote_addr)

if not res then -- redis error

ngx.exit(ngx.HTTP_INTERNAL_SERVER_ERROR) --redis get data error end

 

if type(res) == "string" then --if red not null then type(red)==string

if tonumber(res) >= tonumber(time) then  --check if forbidden expired

ngx.exit(ngx.HTTP_FORBIDDEN)

--ngx.say("forbidden")

end

end

}

接下来会做检测,是否访问频率过高,如果过高,要拉到黑名单的,

实现的方法是,检测user:127.0.0.1:time的值是否超标:

 

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

function M:check2()

local time=os.time() --system time

local res, err = red:get("user:"..ngx.var.remote_addr..":"..time)

if not res then -- redis error

ngx.exit(ngx.HTTP_INTERNAL_SERVER_ERROR) --redis get data error

end

 

if type(res) == "string" then

if tonumber(res) >= 10 then -- attack, 10 times request/s

red:del("block:"..self.ip)

red:set("block:"..self.ip, tonumber(time)+5*60 ) --set block time

ngx.exit(ngx.HTTP_FORBIDDEN)

end

end

end

最后呢,还要记得,把每次访问时间做一个自增长,user:127.0.0.1:time

 

1

2

3

4

5

6

7

function M:add()

local time=os.time() --system time

ok, err = red:incr("user:"..ngx.var.remote_addr..":"..time)

if not ok then

ngx.exit(ngx.HTTP_INTERNAL_SERVER_ERROR) --redis get data error

end

end

那么,测试,强刷几次浏览器,发现过一会,返回了403,ok,搞定。

https://www.ttlsa.com/nginx/nginx-lua-redis-access-frequency-limit/

时间: 2024-08-30 20:33:32

nginx lua redis 访问频率限制(转)的相关文章

nginx lua redis 访问频率限制的两个例子

1. 需求分析 Nginx来处理访问控制的方法有多种,实现的效果也有多种,访问IP段,访问内容限制,访问频率限制等. 用Nginx+Lua+Redis来做访问限制主要是考虑到高并发环境下快速访问控制的需求. Nginx处理请求的过程一共划分为11个阶段,分别是: post-read.server-rewrite.find-config.rewrite.post-rewrite. preaccess.access.post-access.try-files.content.log. 在openre

nginx+lua+redis构建高并发应用(转)

nginx+lua+redis构建高并发应用 ngx_lua将lua嵌入到nginx,让nginx执行lua脚本,高并发,非阻塞的处理各种请求. url请求nginx服务器,然后lua查询redis,返回json数据. 备注:centos或者redhat系统请跳转到nginx + ngx_lua安装测试 一.安装lua     1 2 3 # apt-get install lua5.1 # apt-get install liblua5.1-dev # apt-get install libl

Nginx+Lua+Redis构建高并发Web应用_nginx

本文介绍如何用Nginx+Lua+Redis来构建高并发Web应用,Curl请求Nginx,Nginx通过Lua查询Redis,返回json数据. 一.安装1.安装lua-redis-parser 复制代码 代码如下: #git clone https://github.com/agentzh/lua-redis-parser.git #export LUA_INCLUDE_DIR=/usr/include/lua5.1 #make CC=gcc #make install CC=gcc 2.安

【重要】Nginx模块Lua-Nginx-Module学习笔记(三)Nginx + Lua + Redis 已安装成功(非openresty 方式安装)

源码地址:https://github.com/Tinywan/Lua-Nginx-Redis 一. 目标 使用Redis做分布式缓存:使用lua API来访问redis缓存:使用nginx向客户端提供服务,ngx_lua将lua嵌入到nginx,让nginx执行lua脚本,高并发,非阻塞的处理各种请求.url请求nginx服务器,然后lua查询redis,返回json数据. 二.准备工作 系统环境:Ubuntu 14.0 (64位) Redis服务安装:apt-get install redi

nginx+lua+redis实现反向代理配置

背景 最近要进行IVR的重构, 我们现在系统接了三家IVR服务商, N个业务, 由于IVR这玩意一般只能外网回调, 而开发环境又不允许外网随便访问, 着实烦人. 所有我们打算重构一把, 封装多家IVR, 对业务透明, 同时回调可以针对多家IVR服务商的不同callid直接转发到当时请求的同学的 开发域名去. 而不同的IVR服务商的callid参数是不同的,有的是在url里面(call_id), 有的则是直接post的json数据(callid), 所以太扯了. 直接用lua处理下, 查下redi

Nginx + Lua + redis (一)(转)

使用 Lua 脚本语言操作 Redis. 由于大量的 Lua 代码写在 Nginx 中,会使配置文件显得很繁琐,所以这里使用 content_by_lua_file 来引入 Lua 脚本文件. 要使用 content_by_lua_file,需要安装 nginx_lua_module 模块. 安装介绍,猛击这里:nginx_lua_module 大神 章亦春 提供了一个很方便的开发包,如下:   [plain] view plain copy    print? git clone https:

在nginx中实现单位时间内限制访问频率的教程_nginx

首先说一下遇到这个问题是因为网站被攻击,阿里云报警,想到要限制一下访问频率,而不是限制ip(限制ip的方案稍后给出).nginx连接资源被吃空返回状态码是502,添加本方案限制后返回599,与正常状态码区别开. 步骤如下: 首先nginx.conf里面添加如下内容: map $http_x_forwarded_for $clientRealIp { "" $remote_addr; ~^(?P<firstAddr>[0-9\.]+),?.*$ $firstAddr; } #

Nginx服务器限制访问速度的配置方法_nginx

用Nginx建站的同学,常会有限速需求.开发测试阶段在本地限速模拟公网的环境,方便调试.投入运营会有限制附件下限速度,限制每个用户的访问速度,限制每个IP的链接速度等需求. 刚遇到一个Bug在网络很卡的情况下才能重现,本地调试访问本机速度太快,配置Nginx成功达到限速目的,在此分享出来. 配置简单,只需3行,打开"nginx根目录/conf/nginx.conf"配置文件修改如下: http{ -- limit_zone one $binary_remote_addr 10m; --

用Nginx + Lua(OpenResty)开发高性能Web应用

在互联网公司,Nginx可以说是标配组件,但是主要场景还是负载均衡.反向代理.代理缓存.限流等场景;而把Nginx作为一个Web容器使用的还不是那么广泛.Nginx的高性能是大家公认的,而Nginx开发主要是以C/C++模块的形式进行,整体学习和开发成本偏高;如果有一种简单的语言来实现Web应用的开发,那么Nginx绝对是把好的瑞士军刀;目前Nginx团队也开始意识到这个问题,开发了nginxScript:可以在Nginx中使用JavaScript进行动态配置一些变量和动态脚本执行;而目前市面上