一、限制所有單個ip的訪問頻率
1、http中的配置
http { #$limit_conn_zone:限制並發連接數 limit_conn_zone $binary_remote_addr zone=one1:10m; #limit_req_zone:請求頻率 #$binary_remote_addr:以客戶端IP進行限制 #zone=one:10m:創建IP存儲區大小為10M,用來存儲訪問頻率 #rate=10r/s:表示客戶端的訪問評率為每秒10次 limit_req_zone $binary_remote_addr zone=one2:10m rate=10r/s; }
2、server配置
server { listen 80; server_name localhost; location / { #限制並發數2 limit_conn one1 2; #burst:如果請求的頻率超過了限制域配置的值,請求處理會被延遲 #nodelay:超過頻率限制的請求會被延遲,直到被延遲的請求數超過了定義的閾值,這個請求會被終止,並返回503 limit_req zone=one2 burst=10 nodelay; root html; index index.html index.htm; } }
二、訪問白名單的配置
1、http中的配置
http { # geo:指令定義了一個白名單$limited變量,默認值為1,如果客戶端ip在上面的范圍內,$limited的值為0 geo $limited{ default 1; 10.0.0.140 0; #把10.0.0.140設置為白名單 10.0.0.141 0; #白名單ip,可繼續添加 } #使用map指令映射搜索引擎客戶端的ip為空串,如果不是搜索引擎就顯示本身真是的ip #這樣搜索引擎ip就不能存到limit_req_zone內存session中,所以不會限制搜索引擎的ip訪問 map $limited $limit { 1 $binary_remote_addr; 0 ""; } limit_conn_zone $limit zone=one:20m; limit_req_zone $limit zone=one2:20m rate=10r/s; }
2、server配置
server { listen 80; server_name localhost; location / { limit_conn one 2; limit_req zone=one2 burst=10 nodelay; root html; index index.html index.htm; } }