配置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
';
}
}