提升工作效率利器:
Mac App Store 上的“Whale - 任务管理、时间、卡片、高效率”
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,搞定。