网站首页 > 技术教程 正文
Nginx来处理访问控制的方法有多种,实现的效果也有多种,访问IP段,访问内容限制,访问频率限制等。 |
1. 需求分析
1. Nginx来处理访问控制的方法有多种,实现的效果也有多种,访问IP段,访问内容限制,访问频率限制等。
2. 用Nginx+Lua+Redis来做访问限制主要是考虑到高并发环境下快速访问控制的需求。
3. 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部分内容如下:
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吧:
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:
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的值是否超标:
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:
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.linuxprobe.com/lua-access-control-redis.html
猜你喜欢
- 2024-10-09 openresty代替nginx并使用lua扩展功能
- 2024-10-09 Redis弱事务性与Lua脚本原子性分析
- 2024-09-08 基于Nginx+lua的蓝绿发布系统(lvs与nginx)
- 2024-09-08 Go Web 框架 Gin 实践17—用 Nginx 部署 Go 应用
- 2024-09-08 一文看懂灰度发布——基于Nginx+Lua+Redis
- 2024-09-08 Lua 基础入门(lua经典详细入门教程中文pdf)
- 2024-09-08 Redis中使用Lua脚本来实现并发下的原子操作
- 2024-09-08 运维篇—基于Nginx+Lua实现的灰度发布
- 2024-09-08 Nginx+Lua+Redis实现高性能缓存数据读取
- 2024-09-08 OpenResty实战-Lua入门-Lua模块(openresty教程)
你 发表评论:
欢迎- 最近发表
-
- Win11学院:如何在Windows 11上使用WSL安装Ubuntu
- linux移植(Linux移植freemodbus)
- 独家解读:Win10预览版9879为何无法识别硬盘
- 基于Linux系统的本地Yum源搭建与配置(ISO方式、RPM方式)
- Docker镜像瘦身(docker 减小镜像大小)
- 在linux上安装ollama(linux安装locale)
- 渗透测试系统Kali推出Docker镜像(kali linux渗透测试技术详解pdf)
- Linux环境中部署Harbor私有镜像仓库
- linux之间传文件命令之Rsync傻瓜式教程
- 解决ollama在linux中安装或升级时,通过国内镜像缩短安装时长
- 标签列表
-
- 下划线是什么 (87)
- 精美网站 (58)
- qq登录界面 (90)
- nginx 命令 (82)
- nginx .http (73)
- nginx lua (70)
- nginx 重定向 (68)
- Nginx超时 (65)
- nginx 监控 (57)
- odbc (59)
- rar密码破解工具 (62)
- annotation (71)
- 红黑树 (57)
- 智力题 (62)
- php空间申请 (61)
- 按键精灵 注册码 (69)
- 软件测试报告 (59)
- ntcreatefile (64)
- 闪动文字 (56)
- guid (66)
- abap (63)
- mpeg 2 (65)
- column (63)
- dreamweaver教程 (57)
- excel行列转换 (56)
本文暂时没有评论,来添加一个吧(●'◡'●)