OpenResty(nginx扩展)实现防cc攻击
?? LuaJit ??
发布日期:2025-04-29 04:28:17
浏览次数:13
分类:精选文章
本文共 3022 字,大约阅读时间需要 10 分钟。
OpenResty + Nginx Lua ????
OpenResty ????? Nginx ? Lua ??? Web ?????? Nginx ????? Lua ???????? Web ???????? OpenResty ?????????
??????
??????????????????
RHEL/Centos
yum install readline-devel pcre-devel openssl-devel
Ubuntu
sudo apt-get install libreadline-dev libncurses5-dev libpcre3-dev libssl-dev perl
LuaJit ??
OpenResty ?? LuaJit ??? Lua ??????????????
cd /tmp/git clone http://luajit.org/git/luajit-2.0.gitcd luajit-2.0/make && make install
- ?????
- ?????
- ?????
- ??????
- Lua????
- 1-12: ?? IP ????
- 14-48: ?? IP ????
- 24: 3600??????
- 3600: 1???????
- ????????
- ?? JSKey ??
- ?? URL ?????
- ?? JS ????? bug
- ?? IP ????
ln -sf /usr/local/bin/luajitln -sf /usr/local/lib/libluajit-5.1.so.2 /usr/lib/
OpenResty ??
?? OpenResty ??????
cd /tmp/wget http://agentzh.org/misc/nginx/ngx_openresty-1.2.4.13.tar.gztar xzf ngx_openresty-1.2.4.13.tar.gz
cd ngx_openresty-1.2.4.13/./configure --prefix=/usr/local/openresty --with-luajitmake && make install
Nginx ??
? OpenResty ??? Nginx?
http { lua_shared_dict limit 10m; lua_shared_dict jsjump 10m; server { listen 80; server_name www.centos.bz; location / { default_type text/html; content_by_lua_file "/usr/local/openresty/nginx/conf/lua"; } location @cc { internal; root html; index index.html index.htm; } }} local ip = ngx.var.binary_remote_addrlocal limit = ngx.shared.limitlocal req, _ = limit:get(ip)if req then if req > 20 then ngx.exit(503) else limit:incr(ip, 1) endelse limit:set(ip, 1, 10)endlocal jsjump = ngx.shared.jsjumplocal uri = ngx.var.request_urilocal jspara, flags = jsjump:get(ip)local args = ngx.req.get_uri_args()if jspara then if flags then ngx.exec("@cc") else local p_jskey = '' if args["jskey"] and type(args["jskey"]) == 'table' then p_jskey = args["jskey"][table.getn(args["jskey"])] else p_jskey = args["jskey"] end if p_jskey and p_jskey == tostring(jspara) then jsjump:set(ip, jspara, 3600, 1) ngx.exec("@cc") else local url = '' if ngx.var.args then url = ngx.var.scheme .. "://" .. ngx.var.host .. uri .. "&jskey=" .. jspara else url = ngx.var.scheme .. "://" .. ngx.var.host .. uri .. "?jskey=" .. jspara end local jscode = "window.location.href='" .. url .. ';'" ngx.say(jscode) end endelse math.randomseed(os.time()) local random = math.random(100000, 999999) jsjump:set(ip, random, 60) local url = '' if ngx.var.args then url = ngx.var.scheme .. "://" .. ngx.var.host .. uri .. "&jskey=" .. random else url = ngx.var.scheme .. "://" .. ngx.var.host .. uri .. "?jskey=" .. random end local jscode = "window.location.href='" .. url .. ';'" ngx.say(jscode)end Lua ????
1. rate limiting
2. JS jump
????
2013.5.26
????????????? OpenResty + Nginx ???? Web ??????????
发表评论
最新留言
留言是一种美德,欢迎回访!
[***.207.175.100]2026年05月27日 19时22分19秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
php模板引擎smarty
2023-03-01
php正则表达式模式
2023-03-01
php正则表达式的特殊字符含义
2023-03-01
PHP正则表达式获取武汉市的实时pm2.5数据并邮件发送phpmailer
2023-03-01
RabbitMQ + JMeter组合,优化你的中间件处理方式!
2023-03-01
PHP水仙花问题解法之一
2023-03-01
php没有解析是怎么回事,linux下php文件没有被剖析怎么办?_后端开发
2023-03-01
php注册页面实现注册后跳转页面
2023-03-01
PHP消息队列的实现方式与详解,值得一看
2023-03-01
PHP混合Go协程并发
2023-03-01
php源码中如何添加滚动公告,给WordPress网站添加滚动公告的方法
2023-03-01
PHP源码安装后如何新增模块
2023-03-01
php源码详细安装步骤,linux下php源码安装步骤
2023-03-01
php漏洞tips
2023-03-01
php版Zencoding之 phpstorm
2023-03-01
PHP版本升级5.4手记
2023-03-01
php版本升级总结
2023-03-01
php版本微信公众号开发
2023-03-01
php版的微信公众号开发演示
2023-03-01
php生成html文件的多种方法介绍
2023-03-01