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;
}
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 = "") {
return403;
}
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
';
}
代碼中的 limit_req_zone $binary_remote_addr $uri zone=auth_limit:3m rate=1r/m;這條語(yǔ)句是正確的嗎,確定要加入$uri?(更新一下 limit_req_zone 支持多個(gè)變量 所以$binary_remote_addr $uri是正確的)
如果想要應(yīng)用到我的nginx中除了這段代碼還要做哪些工作?
附上不知道是不是原帖的原帖地址:http://www.92csz.com/30/1255....
這個(gè)思路很粗糙,也并不合理。原理上類(lèi)似于不輸入賬號(hào)密碼就不讓訪問(wèn)。
但是這個(gè)現(xiàn)實(shí)嗎?
對(duì)于一個(gè)新用戶(hù)來(lái)說(shuō),怎么可能知道自己的賬號(hào)密碼。除非預(yù)先分配,但是預(yù)先分配又如何能夠避免分配給攻擊者?
問(wèn)題又回到了原點(diǎn)。
所以說(shuō)這個(gè)東西并沒(méi)有什么卵用,而且只是在一定程度上防止了CC
,但不能防止DDOS
。
為什么說(shuō)只是一定程度上呢?因?yàn)檫@個(gè)cookies總要是給用戶(hù)的,cc攻擊一樣能拿到。
如果你真的要嘗試的話,需要在安裝nginx_lua
模塊,或者使用openresty
.