| 您的当前位置:首页 --> CentOS入门 |
| OpenResty(nginx扩展)实现防cc攻击 |
| 浏览次数:1839 关键词 ( OpenResty nginx cc ) |
|
OpenResty(nginx扩展)实现防cc攻击 导读 OpenResty 通过汇聚各种设计精良的 Nginx 模块(主要由 OpenResty 团队自主开发),从而将 Nginx 有效地变成一个强大的通用 Web 应用平台。这样,Web 开发人员和系统工程师可以使用 Lua 脚本语言调动 Nginx 支持的各种 C 以及 Lua 模块,快速构造出足以胜任 10K 乃至 1000K 以上单机并发连接的高性能 Web 应用系统 流程图
本文介绍使用openresty来实现防cc攻击的功能。openresty官网http://openresty.org/cn/index.html。下面是防cc攻击的流程图。 RHEL/Centos: yum install readline-devel pcre-devel openssl-devel ubuntu: apt-get install libreadline-dev libncurses5-dev libpcre3-dev libssl-dev perlluajit安装
cd /tmp/
git clone http://luajit.org/git/luajit-2.0.git
cd luajit-2.0/
make && make install
ln -sf luajit-2.0.0-beta10 /usr/local/bin/luajit
ln -sf /usr/local/lib/libluajit-5.1.so.2 /usr/lib/
openresty安装
cd /tmp
wget http://agentzh.org/misc/nginx/ngx_openresty-1.2.4.13.tar.gz
tar xzf ngx_openresty-1.2.4.13.tar.gz
cd ngx_openresty-1.2.4.13/
./configure --prefix=/usr/local/openresty --with-luajit
make && make install
nginx配置
nginx.conf:
http{
[......]
lua_shared_dict limit 10m;
lua_shared_dict jsjump 10m;
server {
#lua_code_cache off;
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;
}
}
}
/usr/local/openresty/nginx/conf/lua文件:
local ip = ngx.var.binary_remote_addr
local limit = ngx.shared.limit
local req,_=limit:get(ip)
if req then
if req > 20 then
ngx.exit(503)
else
limit:incr(ip,1)
end
else
limit:set(ip,1,10)
end
local jsjump = ngx.shared.jsjump
local uri = ngx.var.request_uri
local 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
end
else
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代码部分解释:
update: 2013.5.26
导读 OpenResty 通过汇聚各种设计精良的 Nginx 模块(主要由 OpenResty 团队自主开发),从而将 Nginx 有效地变成一个强大的通用 Web 应用平台。这样,Web 开发人员和系统工程师可以使用 Lua 脚本语言调动 Nginx 支持的各种 C 以及 Lua 模块,快速构造出足以胜任 10K 乃至 1000K 以上单机并发连接的高性能 Web 应用系统 流程图
本文介绍使用openresty来实现防cc攻击的功能。openresty官网http://openresty.org/cn/index.html。下面是防cc攻击的流程图。 RHEL/Centos: yum install readline-devel pcre-devel openssl-devel ubuntu: apt-get install libreadline-dev libncurses5-dev libpcre3-dev libssl-dev perlluajit安装
cd /tmp/
git clone http://luajit.org/git/luajit-2.0.git
cd luajit-2.0/
make && make install
ln -sf luajit-2.0.0-beta10 /usr/local/bin/luajit
ln -sf /usr/local/lib/libluajit-5.1.so.2 /usr/lib/
openresty安装
cd /tmp
wget http://agentzh.org/misc/nginx/ngx_openresty-1.2.4.13.tar.gz
tar xzf ngx_openresty-1.2.4.13.tar.gz
cd ngx_openresty-1.2.4.13/
./configure --prefix=/usr/local/openresty --with-luajit
make && make install
nginx配置
nginx.conf:
http{
[......]
lua_shared_dict limit 10m;
lua_shared_dict jsjump 10m;
server {
#lua_code_cache off;
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;
}
}
}
/usr/local/openresty/nginx/conf/lua文件:
local ip = ngx.var.binary_remote_addr
local limit = ngx.shared.limit
local req,_=limit:get(ip)
if req then
if req > 20 then
ngx.exit(503)
else
limit:incr(ip,1)
end
else
limit:set(ip,1,10)
end
local jsjump = ngx.shared.jsjump
local uri = ngx.var.request_uri
local 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
end
else
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代码部分解释:
update: 2013.5.26
|
| 下载次数:30 |
| 下载地址:点击下载 |
| 本资源为程序自动采集,如有侵权请联系我们移除 admin#80vps.com 来信请将#替换为@ |
| 下一条 centos系统中网络配置相关 上一条 DNS 原理入门 |