用nginx實現分布式限流(防DDOS攻擊)
1.前言
一般對外暴露的系統,在促銷或者黑客攻擊時會湧來大量的請求,為了保護系統不被瞬間到來的高並發流量給打垮, 就需要限流 .
本文主要闡述如何用nginx 來實現限流. 聽說 Hystrix 也可以, 各位有興趣可以去研究哈 .
2. 首先部署一個對外暴露接口的程序
我這裏部署的是一個spring boot 項目 裏面暴露了如下接口, 很簡單
package com.anuo.app.controller; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping("/test") public class TestController { Logger logger = LoggerFactory.getLogger(TestController.class); @RequestMapping(value = "/show/message", method = RequestMethod.GET) public String test() { return "hello world "; } }
暴露了一個 get 請求返回 hello world 的restful 接口.
將此程序部署到 linux 服務器上. 部署步奏不再贅述, 自行百度 spring boot 部署 即可.
3.創建一個名稱為 nginx.conf 的 nginx 配置文件
創建一個 名叫 nginx.conf 的配置文件, 完整內容如下
user nginx; worker_processes 1; error_log /var/log/nginx/error.log warn; pid /var/run/nginx.pid; events { worker_connections 1024; } http { include /etc/nginx/mime.types; default_type application/octet-stream; log_format main ‘$remote_addr - $remote_user [$time_local] "$request" ‘ ‘$status $body_bytes_sent "$http_referer" ‘ ‘"$http_user_agent" "$http_x_forwarded_for"‘; access_log /var/log/nginx/access.log main; sendfile on; #tcp_nopush on; keepalive_timeout 65; #gzip on; #限流配置 limit_req_zone $binary_remote_addr zone=perip:10m rate=1r/s; #負載均衡配置 upstream myserver { server 192.168.10.253:8090; } server { listen 80; location / { limit_req zone=perip; proxy_pass http://myserver; } } }
配置文件中限流部分解釋:
如上, nginx 的限流配置 , 只有兩行代碼.
第一行:
#限流配置 limit_req_zone $binary_remote_addr zone=perip:10m rate=1r/s;
limit_req_zone : 是限流聲明.
$binary_remote_addr: 表示根據客戶端 ip 來 限流, 比如 上面的限流配置 限制每個客戶端ip的請求頻率為一秒一次, 你如果耍流氓一秒兩次, 就會被限流 會返回一個http 503 錯誤給你.
zone=perip: 表示 用 perip 這個 名稱 來標識 這行限流配置, 待會 會通過 perip 這個名稱來引用這行限流配置(也就是說限流配置是可以定義為多個的)
10m: 表示存儲客戶端ip的空間為10MB, 1MB 大概存儲1萬多ip , 10 MB 大概 10多萬Ip , 參考解釋: http://www.ttlsa.com/nginx/nginx-limiting-the-number-of-requests-ngx_http_limit_req_module-module/ 在這篇文章中搜索 binary_remote_addr 即可定位相關解釋.
rate=1r/s: 表示頻率是 一秒一個請求.
第二行:
location / { limit_req zone=perip; proxy_pass http://myserver; }即這行:
limit_req zone=perip;
表示在 myserver 這個集群上, 使用 名稱為 perip 的限流配置
4.用docker 部署 nginx
將上一步創建的 nginx.conf 配置文件, 拷貝到linux 目錄 , /root/nginx/ 下 (目錄可以任意), 然後 一個docker 命令部署好 nginx 環境
docker run --name nginx -v /root/nginx/nginx.conf:/etc/nginx/nginx.conf:ro -p 8080:80 -d nginx
這裏暴露的是 8080 端口, 通過 8080 端口可以訪問到 nginx 配置中的負載均衡節點, 即 192.168.10.253:8090 ip端口, 這個 ip端口對應的就是 , 第一步創建部署的 hello world 程序.
5. 用代碼訪問 第一步定義的 helloworld 接口
package com.anuo.study.studydistributed; import com.anuo.app.common.util.HttpUtil; public class StudyNginx { public static void main(String[] args) throws InterruptedException { while (true) { Thread.sleep(100); String s= HttpUtil.sendGet("http://192.168.10.253:8080/test/show/message"); System.out.println(s); } } }我這裏是 一秒 執行 10次 get 請求, 已經大於了 nginx中配置的 rate=1r/s 一秒一次的請求, 所以會看到 503 報錯, 如下.
如果改哈代碼, 改為一秒執行一次get 請求, 就不會報錯, 各位可以去試一下
至此 nginx 的限流 已實現.
用nginx實現分布式限流(防DDOS攻擊)