配置Nginx防御cc攻击的方法:1、下载lua,并使用Nginx进行编译;2、编译完成后,对相关文件进行修改配置,即可完成配置防御cc攻击。
具体内容如下:
1、Nginx编译支持lua
下载lua-nginx-module
编译
#./configure\--user=nginx\
--group=nginx\
--prefix=/usr/local/gacp/nginx\
--error-log-path=/data/logs/nginx/error/error.log\
--http-log-path=/data/logs/nginx/access/access.log\
--pid-path=/usr/local/gacp/nginx/conf/nginx.pid\
--lock-path=/var/lock/nginx.lock\
--with-http_flv_module\
--with-http_stub_status_module\
--with-http_ssl_module\
--with-pcre\
--with-http_realip_module\
--with-http_gzip_static_module\
--with-google_perftools_module\
--with-file-aio\
--add-module=../ngx_cache_purge-2.3\
--add-module=../lua-nginx-module-master
#make&&makeinstall
2、配置
http{.....
limit_req_zone$cookie_tokenzone=session_limit:3mrate=1r/s;
limit_req_zone$binary_remote_addr$urizone=auth_limit:3mrate=1r/m;
}
server{
listen80;
server_namelocalhost;
access_log/data/logs/nginx/access/localhost.access.logmain;
error_log/data/logs/nginx/error/localhost.error.log;
charsetutf-8;
client_max_body_size75M;
root/data/www;
location/{
limit_reqzone=session_limitburst=5;
rewrite_by_lua'
localrandom=ngx.var.cookie_random
if(random==nil)then
returnngx.redirect("/auth?url="..ngx.var.request_uri)
end
localtoken=ngx.md5("opencdn"..ngx.var.remote_addr..random)
if(ngx.var.cookie_token~=token)then
returnngx.redirect("/auth?url="..ngx.var.request_uri)
end
';
}
location/auth{
limit_reqzone=auth_limitburst=1;
if($arg_url=""){
return403;
}
access_by_lua'
localrandom=math.random(9999)
localtoken=ngx.md5("opencdn"..ngx.var.remote_addr..random)
if(ngx.var.cookie_token~=token)then
ngx.header["Set-Cookie"]={"token="..token,"random="..random}
returnngx.redirect(ngx.var.arg_url)
end
';
}
}