首先講解兩個算髮:html
算法思想是:node
--------------------------------------------------------------------------------------------------------------------------------------python
漏桶算法:nginx
算法思想是:算法
從做用上來講,漏桶和令牌桶算法最明顯的區別就是是否容許突發流量(burst)的處理,漏桶算法可以強行限制數據的實時傳輸(處理)速率,對突發流量不作額外處理;而令牌桶算法可以在限制數據的平均傳輸速率的同時容許某種程度的突發傳輸。apache
Nginx按請求速率限速模塊使用的是漏桶算法,即可以強行保證請求的實時處理速度不會超過設置的閾值。緩存
------------------------------------------------------------------------------------------------------------------------------------服務器
官方限制ip併發鏈接和請求有兩個模塊,不須要從新編譯安裝,nginx默認已經集成。併發
limit_req_zone : 限制請求數spa
limit_conn_zone :限制併發鏈接數
limit_req_zone 參數配置 limit_req_zone $binary_remote_addr zone=one:10m rate=10r/s;
參數講解:
$binary_remote_addr:遠程的訪問地址,此處以二進制的形式記錄
zone:=one:10m :設置一個名字爲one,大小爲10M的緩存空間
rate=10r/s: 限制訪問速率,此處設置爲每秒接受10個請求(nging裏是按ms及時的,此處爲s)
limit_req zone=one burst=5 nodelay;
參數講解:
zone=one:指定使用名字爲one的這個緩存空間,若沒有設置burst參數,結合上文,此處的配置表示爲每秒接受請求10個
burst=5:由於咱們的流量並非向漏桶同樣每時每刻都是勻速的,因此爲了不某一時刻出現大規模的流量出現,因此咱們添加burst參數,此處配置表示爲,設置一個大小爲5的緩衝區,當有大量請求(爆發)過來時,訪問超過了上面的限制能夠先放到緩衝區內。
nodelay:通常是和burst一塊兒使用的,若是設置了nodelay,當訪問超過了頻次並且緩衝區也滿的狀況下會直接返回503,若是設置了,則全部大的請求會等待排隊。
limit_conn_log_level error; #定義當服務器因爲limint被限制或緩存時,配置寫入日誌。延遲的記錄比拒絕的記錄低一個級別。例子:延遲的的基本是info。 limit_conn_status 589; #當客戶端配置得併發數超過了nginx限制的數量後會返回的狀態值 limit_conn_zone $binary_remote_addr zone=one:10m; limit_conn_zone $server_name zone=perserver:10m; limit_req_zone $binary_remote_addr zone=allips:100m rate=20r/s; server { listen 8888; access_log /var/log/nginx/example_http.log; location /status { stub_status on; access_log off; allow 127.0.0.1; allow 10.0.17.27; allow 10.0.1.142; deny all; } location / { limit_conn one 5; #限制每一個用戶鏈接到服務器的數量 limit_conn perserver 2000;#限制鏈接到服務器的總數 limit_req zone=allips burst=200 nodelay; proxy_http_version 1.1; proxy_set_header Connection ""; proxy_pass http://test; #Proxy Settings proxy_redirect off; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; #proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-For $http_x_forwarded_for; proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504; proxy_ignore_client_abort on; proxy_max_temp_file_size 0; proxy_connect_timeout 90; proxy_send_timeout 90; proxy_read_timeout 90; proxy_buffer_size 4k; proxy_buffers 4 32k; proxy_busy_buffers_size 64k; proxy_temp_file_write_size 64k; } }limit_req_log_level notice
總結與心得:
1.在設置完上面的參數後,使用jmeter進行壓測時會發現,neginx的日誌顯示沒秒的請求數永遠是20(前段設置的rate是每秒處理20個),發現burst的參數並無起做用,後來我把burst參數設置爲2000,發現前幾秒tps能夠達到四五百,可是後面依舊恢復到20.並不向我想的同樣,會一直超過20個tps運行先去,因此這個burst的時間也是有限制的, 並非大流量下一直有用,因此在生產配置的時候必定要想好rate的參數值,由於burst只適用突發的以小段時間。
2.第二次我啓用了兩個客戶端去壓測,發現nginx的tps的值達到了40,所以得出結論,此處限制只是針對單個ip,並非全局配置。兩個客戶端的壓測時間我故意間隔了幾分鐘,發現出現了兩次四五百的tps,後面同樣迴歸到40tps不變。所以burst也是針對ip有限制的。
3,使用了ab進行壓測,ab -n 40 -c 20 http://IP/index.html 發現我rate設置的值無論是多少永遠只有一個是失敗的,貌似rate沒有起做用,是一個大坑。
root@in-yeerqianghe:/# ab -n 50 -c 20 http://10.0.18.128/index.html This is ApacheBench, Version 2.3 <$Revision: 1706008 $> Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/ Licensed to The Apache Software Foundation, http://www.apache.org/ Benchmarking 10.0.18.128 (be patient).....done Server Software: nginx/1.12.2 Server Hostname: 10.0.18.128 Server Port: 80 Document Path: /index.html Document Length: 14 bytes Concurrency Level: 20 Time taken for tests: 0.006 seconds Complete requests: 50 Failed requests: 49 (Connect: 0, Receive: 0, Length: 49, Exceptions: 0) Non-2xx responses: 49 Total transferred: 36063 bytes HTML transferred: 26327 bytes Requests per second: 8579.27 [#/sec] (mean) Time per request: 2.331 [ms] (mean) Time per request: 0.117 [ms] (mean, across all concurrent requests) Transfer rate: 6042.86 [Kbytes/sec] received Connection Times (ms) min mean[+/-sd] median max Connect: 0 1 0.2 1 1 Processing: 1 1 0.5 1 2 Waiting: 1 1 0.5 1 2 Total: 1 2 0.3 2 3 Percentage of the requests served within a certain time (ms) 50% 2 66% 2 75% 2 80% 2 90% 3 95% 3 98% 3 99% 3 100% 3 (longest request)