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