Nginx来处理访问控制的方法有多种,实现的效果也有多种,访问IP段,访问内容限制,访问频率限制等。html
用Nginx+Lua+Redis来作访问限制主要是考虑到高并发环境下快速访问控制的需求。nginx
Nginx处理请求的过程一共划分为11个阶段,分别是:redis
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阶段。ide
按照正常的逻辑思惟,咱们会想到的访问控制方案以下:高并发
1.检测是否被forbidden?
=》是,forbidden是否到期:是,清除记录,返回200,正常访问;否,返回403;
=》否,返回200,正常访问post
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来处理访问控制
}
}
|
参考了下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/