服务器 
首页 > 服务器 > 浏览文章

配置Nginx实现简单防御cc攻击

(编辑:jimmy 日期: 2024/5/9 浏览:3 次 )

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" .. 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" .. 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
';

}
}

是不是很简单呢。

上一篇:Windows下ftp服务器实现匿名访问的配置方法
下一篇:Linux中Centos7搭建Hadoop服务步骤
一句话新闻
高通与谷歌联手!首款骁龙PC优化Chrome浏览器发布
高通和谷歌日前宣布,推出首次面向搭载骁龙的Windows PC的优化版Chrome浏览器。
在对骁龙X Elite参考设计的初步测试中,全新的Chrome浏览器在Speedometer 2.1基准测试中实现了显著的性能提升。
预计在2024年年中之前,搭载骁龙X Elite计算平台的PC将面世。该浏览器的提前问世,有助于骁龙PC问世就获得满血表现。
谷歌高级副总裁Hiroshi Lockheimer表示,此次与高通的合作将有助于确保Chrome用户在当前ARM兼容的PC上获得最佳的浏览体验。
友情链接:杰晶网络 DDR爱好者之家 南强小屋 黑松山资源网 白云城资源网