關於nginx的限速模塊

nginx 使用 ngx_http_limit_req_module和ngx_http_limit_conn_module 來限制對資源的請求php

這種方法,對於CC攻擊(Challenge Collapsar)or DDOS(分佈式拒絕服務)有必定的用處html

一、HttpLimitReqModule

限制request 事實上就是 the processing rate of requests coming from a single IP address,使用的是漏桶算法(Leaky Bucket)node

Leaky Bucket有兩種處理方式,具體能夠看wiki

Traffic Shaping和Traffic Policing
在桶滿水以後,常見的兩種處理方式爲:
1)暫時攔截住上方水的向下流動,等待桶中的一部分水漏走後,再放行上方水
2)溢出的上方水直接拋棄
將水看做網絡通訊中數據包的抽象,則方式1起到的效果稱爲Traffic Shaping,方式2起到的效果稱爲Traffic Policing
因而可知,Traffic Shaping的核心理念是"等待",Traffic Policing的核心理念是"丟棄"。它們是兩種常見的流速控制方法nginx

Syntax:	limit_req zone=name [burst=number] [nodelay];
Default:	—
Context:	http, server, location

示例web

limit_req_zone $binary_remote_addr zone=one:10m rate=1r/s;

server {
    location /search/ {
        limit_req zone=one burst=5 nodelay;
    }

第一段配置

第一個參數:$binary_remote_addr 表示經過remote_addr這個標識來作限制,「binary_」的目的是縮寫內存佔用量,是限制同一客戶端ip地址
第二個參數:zone=one:10m表示生成一個大小爲10M,名字爲one的內存區域,用來存儲訪問的頻次信息
第三個參數:rate=1r/s表示容許相同標識的客戶端的訪問頻次,這裏限制的是每秒1次,還能夠有好比30r/m的算法

第二段配置

第一個參數:zone=one 設置使用哪一個配置區域來作限制,與上面limit_req_zone 裏的name對應
第二個參數:burst=5,重點說明一下這個配置,burst爆發的意思,這個配置的意思是設置一個大小爲5的緩衝區當有大量請求(爆發)過來時,超過了訪問頻次限制的請求能夠先放到這個緩衝區內
第三個參數:nodelay,若是設置,超過訪問頻次並且緩衝區也滿了的時候就會直接返回503,若是沒有設置,則全部請求會等待排隊api

 

下面這個配置能夠限制特定UA(好比搜索引擎)的訪問網絡

limit_req_zone  $anti_spider  zone=one:10m   rate=10r/s;
limit_req zone=one burst=100 nodelay;
if ($http_user_agent ~* "googlebot|bingbot|Feedfetcher-Google") {
    set $anti_spider $http_user_agent;
} 

 

二、ngx_http_limit_conn_module

這個模塊就是 limit  the number of connections from a single IP address併發

Not all connections are counted. A connection is counted only if it has a request processed by the server and the whole request header has already been read分佈式

Syntax:	limit_conn zone number;
Default:	—
Context:	http, server, location

示例

http {
    limit_conn_zone $binary_remote_addr zone=addr:10m;
    ...
    server {
        ...
        location /download/ {
            limit_conn addr 1;
            #帶寬限制,對單個鏈接限數,若是一個ip兩個鏈接,就是500x2k
            limit_rate 100k;   
        }
    }
}

Sets the shared memory zone and the maximum allowed number of connections for a given key value. When this limit is exceeded, the server will return the 503 (Service Temporarily Unavailable) error in reply to a request

  • $binary_remote_addr是限制同一客戶端ip地址
  • $server是限制同一server最大併發數
  • limit_conn爲限制併發鏈接數,nginx 1.18之後用limit_conn_zone替換了limit_conn

配置完以後,咱們可使用ab或者webbench來測試一下

ab -n 5 -c 1 http://www.test.org/test.php

正常狀況下能夠這樣來配置

map $remote_addr $rt_filtered_ip {
        default $binary_remote_addr;
        1.2.3.4 "";
        4.4.4.4 "";
}

or

geo $rt_filtered_ip {
    default        $binary_remote_addr;

    127.0.0.1      "";
    192.168.1.0/24 "";
    10.1.0.0/16    "";

    ::1            "";
    2001:0db8::/32 "";

    1.2.3.4        ""
}

limit_conn_zone $binary_remote_addr zone=perip:10m;
limit_conn_zone $server_name zone=perserver:10m;
limit_conn_zone $host$uri zone=peruri:10m;
limit_req_zone $rt_filtered_ip zone=qps:10m rate=1r/s;

server {


        location = /wp-login.php {
            limit_req zone=qps burst=5 nodelay;
            limit_conn perip 10;
            limit_conn perserver 100;
            limit_rate 500k;
            include fastcgi_params;
            fastcgi_pass 127.0.0.1:9000;
        }
}

ab -n 100 -c 10 example.com/wp-login.php

$binary_remote_addr是限制同一客戶端ip地址;
$server_name是限制同一server最大併發數;
limit_conn爲限制併發鏈接數;
limit_rate爲限制下載速度;

 

參考

https://en.wikipedia.org/wiki/Leaky_bucket

http://nginx.org/en/docs/http/ngx_http_limit_conn_module.html

http://nginx.org/en/docs/http/ngx_http_limit_req_module.html

https://rtcamp.com/tutorials/nginx/block-wp-login-php-bruteforce-attack/

相關文章
相關標籤/搜索