2012-09-06 27 views
6

在我的服務器上有一個http洪水,沒有太多的查詢,但無論如何。查詢日誌配置nginx來過濾http洪水

95.55.237.3 - - [06/Sep/2012:14:38:23 +0400]「GET/HTTP/1.0」200 35551「 - 」「Mozilla/5.0(Windows; U; Windows NT 6.1; en-US)「」 - 「| 「 - 」 93.78.44.25 - - [06/Sep/2012:14:38:23 +0400]「GET/HTTP/1.0」200 36051「 - 」「Mozilla/5.0(Windows; U; Windows NT 6.1; en -US)「」 - 「| 「 - 」 46.118.112.3 - - [06/Sep/2012:14:38:23 +0400]「GET/HTTP/1.0」200 35551「 - 」「Mozilla/5.0(Windows; U; Windows NT 6.1; en -US)「」 - 「| 「 - 」

我想這個過濾器nginx的配置

server { 
    ..... 
    set $add 1; 
    set $ban ''; 

###### Rule 1 ######## 
if ($http_referer = '-') { 
    set $ban $ban$add; 
} 
if ($request_uri = '/') { 
    set $ban $ban$add; 
} 

if ($http_user_agent = 'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US)') { 
    set $ban $ban$add; 
} 

if ($ban = 111) { 
    return 444; 
} 
###################### 
...... 
} 

但還是老樣子BOT查詢得到200 OK。有人可以幫忙嗎?

回答

31

嘗試添加類似下面的指令,以你的配置,以防止HTTP洪水:

http { 
    limit_conn_zone $binary_remote_addr zone=conn_limit_per_ip:10m; 
    limit_req_zone $binary_remote_addr zone=req_limit_per_ip:10m rate=5r/s; 

    server { 
    limit_conn conn_limit_per_ip 10; 
    limit_req zone=req_limit_per_ip burst=10 nodelay; 
    } 
} 

更多信息,請參見http://nginx.org/en/docs/http/ngx_http_limit_conn_module.htmlhttp://nginx.org/en/docs/http/ngx_http_limit_req_module.html

有以下所有指令http://nginx.org/en/docs/http/ngx_http_core_module.html#limit_rate

注:http://www.botsvsbrowsers.com/details/504401/index.html說上面的用戶代理不是一個已知的機器人

+0

這可能會對同一局域網上的很多計算機不利,共享一個IP地址 – brauliobo

+3

@brauliobo現在有多少人在同一局域網中瀏覽,如果不是多人遊戲。你甚至可以在設定的時間內調整允許的ips數量...... – 2014-07-05 09:52:23

+0

@stupidtroll你知道路由器嗎?它們用於每個家庭和企業,將所有用戶置於一個或幾個互聯網IP下 – brauliobo

3

您也可以阻止特定的IP,作爲附加措施。

http{ 
    deny 127.45.4.1; 
    ... 
} 

或把封鎖IP地址在不同的文件

http{ 
    include blockedips.conf 
    ... 
} 

blockedips.conf

deny 1.12.4.5; 
3

你也可以封鎖特定國家

http{ 
    geoip_country /usr/share/GeoIP/GeoIP.dat; 
    map $geoip_country_code $allowed_country { 
     default yes; 
     FK no; 
     FM no; 
     EH no; 
    } 
} 

GeoIP.dat可以下載從http://dev.maxmind.com/geoip/geoip2/geolite2/(我不隸屬於maxmind)