# [Nginx] 流量限制 ###### tags: `server` `Nginx` 定義一個流量限制 ```Vim! limit_req_zone $binary_remote_addr zone=limit_name:10m rate=10r/s; limit_req_zone $binary_remote_addr zone=定義的名稱:幾MB rate=一秒幾次; limit_req zone=testname1 burst=5 nodelay; ``` location ```Vim! # 判斷ip需不需要限制 geo $limited{ default 1; IP 0; } # 依照limited值判斷 # 為 1 則將 limit 設定為 ip # 為 0 則空值 map $limited $limit { 0 ""; 1 $binary_remote_addr; } limit_req_zone $limit zone=testname1:7m rate=10r/s; limit_req_zone $limit zone=testname2:7m rate=10r/s; ``` ```Vim! location / { limit_req zone=testname1 burst=5 nodelay; } ``` * `$binary_remote_addr` 客戶端 IP * `zone=testname1:7m` 名稱叫 testname1 的 zone ,最多可使用 7MB 的記憶體來儲存各個請求次數 * `rate=10r/s` 處理速度為每秒最多 10 個請求,過量的請求會被拒絕 * `burst=15` 最多允許 15 個請求同時進來 server,並以 rate 的速度處理這 15 個請求 * `delay` / `nodelay` `/api/test1` 路徑 ```Vim! http { limit_req_zone $binary_remote_addr zone=testname1:10m rate=2r/m; limit_req_zone $binary_remote_addr zone=testname2:10m rate=1r/m; server { location /api/test1 { limit_req zone=testname1; proxy_pass http://test.1.com; } location /api/test2 { limit_req zone=testname2; proxy_pass http://test.2.com; } } } ``` --- 參考資料: [Day07-流量限制(二)-burst](https://ithelp.ithome.com.tw/articles/10269559) [Day09-流量限制(四)-delay](https://ithelp.ithome.com.tw/articles/10270993) [NGINX 設定密碼認證與限制可存取的 IP 位址,控制頻寬與連線數](https://blog.gtwang.org/linux/nginx-restricting-access-authenticated-user-ip-address-tutorial/2/)
×
Sign in
Email
Password
Forgot password
or
Sign in via Google
Sign in via Facebook
Sign in via X(Twitter)
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
Continue with a different method
New to HackMD?
Sign up
By signing in, you agree to our
terms of service
.