首頁  >  文章  >  後端開發  >  設定Nginx實作簡單防禦cc攻擊方法分享

設定Nginx實作簡單防禦cc攻擊方法分享

小云云
小云云原創
2018-02-22 10:40:453659瀏覽

ddos攻擊:分散式拒絕服務攻擊,就是利用大量肉雞或偽造IP,發動大量的伺服器請求,最後導致伺服器癱瘓的攻擊。 cc攻擊:類似ddos攻擊,不過它的特點是主要是發動大量頁面請求,所以流量不大,但是卻能導致頁面訪問不了。

本文主要和介紹lua+Nginx下如何快速有效得防禦CC攻擊。至於如何安裝Nginx就不詳細介紹了,閒話少說,大家請看範例,希望能幫助大家。

使用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
';

}
}

是不是很簡單呢。


相關推薦:

防cc攻擊 PHP防CC攻擊實作程式碼

以上是設定Nginx實作簡單防禦cc攻擊方法分享的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn