nginx 使用 ngx_http_limit_req_module和ngx_http_limit_conn_module 來限制對資源的請求
這種方法,對于CC攻擊(Challenge Collapsar)or DDOS(分布式拒絕服務)有一定的用處
限制request 事實上就是 the PRocessing rate of requests coming from a single ip address,使用的是漏桶算法(Leaky Bucket)
Traffic Shaping和Traffic Policing
在桶滿水之后,常見的兩種處理方式為:
1)暫時攔截住上方水的向下流動,等待桶中的一部分水漏走后,再放行上方水
2)溢出的上方水直接拋棄
將水看作網絡通信中數據包的抽象,則方式1起到的效果稱為Traffic Shaping,方式2起到的效果稱為Traffic Policing
由此可見,Traffic Shaping的核心理念是"等待",Traffic Policing的核心理念是"丟棄"。它們是兩種常見的流速控制方法
Syntax: limit_req zone=name [burst=number] [nodelay];Default: —Context: http, server, location
示例
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,如果沒有設置,則所有請求會等待排隊
下面這個配置可以限制特定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;}
這個模塊就是 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
配置完之后,我們可以使用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 "";}orgeo $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/
新聞熱點
疑難解答