nginx+lua 限制接口访问次数
2017-10-24 19:54
465 查看
最近看了一些nginx+lua 的东西,尝试实现了一下限流脚本,只包含最根本的功能。
代码如下
access_nginx_check.lua
对应nginx 配置
#声明变量 对应lua 脚本中 的ngx.shared.limit
lua_shared_dict limit 10m;
server{
listen 80;
server_name localhost.accesslua.com;
root /var/www/html/test;
charset utf-8;
location ~\.php{
#为了调试lua脚本,直接将信息抛出到浏览器,否则会自动下载
default_type 'text/html';
#嵌入的lua脚本
access_by_lua_file "/var/www/lua/access_nginx_check.lua";
#php 的正常调用
fastcgi_pass 192.168.33.11:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
#记录log
access_log /usr/local/var/log/lua.accesslua-php5.access.log main;
}
演示效果
代码如下
access_nginx_check.lua
-- 一个按照 url + 参数 进行 在固定时间内访问次数次数限制的lua 脚本, -- 此处仅仅实现 按照url 10s 内限制 2次访问,记录每次访问次数(无用仅仅用于核对代码逻辑可去除),获取参数函数已有,后续需要完善 --package.path = package.path ..';..\\?.lua'; --dofile ("./log.lua") --是否开启检测 local check = "on" --限制最大访问量 local count = 2 --限制时间范围 local seconds = 10 --获取参数的值 function getParam() if "GET" == request_method then args = ngx.req.get_uri_args() elseif "POST" == request_method then ngx.req.read_body() args = ngx.req.get_post_args() end return args end --写入日志 function writerLog(str) local time = os.time() local date = os.date("%Y%m%d",time) local file = io.open("/var/www/lua/logTest/"..tostring(date).."_log.log","a") --ngx.say("/var/www/lua/log/"..tostring(date).."_log.log") --assert(file) file:write(str.."\n") file:close(); end --对uri 进行限制主要逻辑 function access_user_uri_check() -- body if check=="on" then local access_uri = ngx.var.host..ngx.var.uri --local param = getParam() local key = access_uri --提前在nginx中声明的变量 local limit = ngx.shared.limit local req if limit then req ,_= limit:get(key) end if req then if req>=count then --超过限制之后返回错误 ngx.say("您在最近:"..seconds.."秒,访问:"..access_uri.."次数超过:"..count.."次,已经触发保护机制,请稍后访问") ngx.exit(403) end --对key对应的数据进行累加 limit:incr(key,1) else --没有值设置初始为1 limit:set(key,1,seconds) end --记录log writerLog(key..":"..limit:get(key)) end end --错误调试函数 function myerrorhandler( err ) ngx.say( "ERROR:", err ) end --测试代码,可以自动执行 status = xpcall( access_user_uri_check, myerrorhandler ) --access_user_uri_check()
对应nginx 配置
#声明变量 对应lua 脚本中 的ngx.shared.limit
lua_shared_dict limit 10m;
server{
listen 80;
server_name localhost.accesslua.com;
root /var/www/html/test;
charset utf-8;
location ~\.php{
#为了调试lua脚本,直接将信息抛出到浏览器,否则会自动下载
default_type 'text/html';
#嵌入的lua脚本
access_by_lua_file "/var/www/lua/access_nginx_check.lua";
#php 的正常调用
fastcgi_pass 192.168.33.11:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
#记录log
access_log /usr/local/var/log/lua.accesslua-php5.access.log main;
}
演示效果
相关文章推荐
- 在java项目中,如何限制每个用户访问接口的次数?
- cxf+spring开发(三)--- 限制固定Ip地址对接口的访问次数
- 使用lua脚本编写访问次数限制
- spring boot中的拦截器限制用户访问接口次数
- 限制每个用户访问接口的次数(Java)
- PHP中Yii2框架用redis实现限制接口访问次数
- 使用redis进行用户接口访问时间次数限制
- 接口访问次数限制
- Lua在Redis中的应用—分布式锁,限制访问次数
- Nginx中如何限制某个IP同一时间段的访问次数
- Nginx中如何限制某个IP同一时间段的访问次数
- 【Java笔试题】限制文件的访问次数
- SpringBoot中自定义注解实现控制器访问次数限制
- linkedin api 每天访问次数限制
- nginx限制某个IP同一时间段的访问次数 [转载]
- springboot和redis控制单位时间内同个ip访问同个接口的次数
- nginx限制某个IP同一时间段的访问次数
- 网路编程(TCP练习:上传文件、并发访问、限制登录次数)
- nginx怎么限制客户端访问频次与访问次数
- nginx中如何限制某个IP同一时间段的访问次数