微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何配置Nginx实现防御cc攻击

这篇文章给大家分享的是有关如何配置Nginx实现防御cc攻击的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。

ddos攻击:分布式拒绝服务攻击,就是利用大量肉鸡或伪造IP,发起大量的服务器请求,最后导致服务器瘫痪的攻击。

cc攻击:类似于ddos攻击,不过它的特点是主要是发起大量页面请求,所以流量不大,但是却能导致页面访问不了。

使用Nginx的配置对cc攻击进行简单防御
===================================================================

主要是通过Nginx和lua来配合,达到防御的目的。

一、Nginx编译支持lua
------------------------------

1. 下载lua-Nginx-module

wget https://github.com/openresty/lua-Nginx-module/archive/master.zip
unzip master.zip

2. 编译

#./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 && make install

二、配置

http {
.....
limit_req_zone $cookie_token zone=session_limit:3m rate=1r/s;
limit_req_zone $binary_remote_addr $uri zone=auth_limit:3m rate=1r/m;


}

server {
listen 80;
server_name localhost;
access_log /data/logs/Nginx/access/localhost.access.log main;
error_log /data/logs/Nginx/error/localhost.error.log;
charset utf-8;
client_max_body_size 75M;
root /data/www;

location / {

limit_req zone=session_limit burst=5;

rewrite_by_lua '
local random = ngx.var.cookie_random
if(random == nil) then
return ngx.redirect("/auth?url=" .. ngx.var.request_uri)
end

local token = ngx.md5("opencdn" .. ngx.var.remote_addr .. random)
if(ngx.var.cookie_token ~= token) then
return ngx.redirect("/auth?url=" .. ngx.var.request_uri)
end
';
}

location /auth {
limit_req zone=auth_limit burst=1;

if ($arg_url = "") {
return 403;
}

access_by_lua '
local random = math.random(9999)
local token = ngx.md5("opencdn" .. ngx.var.remote_addr .. random)
if(ngx.var.cookie_token ~= token) then
ngx.header["Set-Cookie"] = {"token=" .. token, "random=" .. random}
return ngx.redirect(ngx.var.arg_url)
end
';

}
}

感谢各位的阅读!关于“如何配置Nginx实现防御cc攻击”这篇文章分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐