首頁 >後端開發 >php教程 >php nginx限制ip請求次數 以及併發次數

php nginx限制ip請求次數 以及併發次數

WBOY
WBOY原創
2016-07-30 13:31:281560瀏覽

     如何設定能限制某個IP某一時段的造訪次數是一個讓人頭痛的問題,特別面對惡意的ddos攻擊的時候。其中CC攻擊(Challenge Collapsar)是DDOS(分散式拒絕服務)的一種,也是一種常見的網站攻擊方法,攻擊者透過代理伺服器或肉雞向受害主機不停地發大量資料包,造成對方伺服器資源耗盡,一直到宕機崩潰。 cc攻擊一般就是使用有限的ip數對伺服器頻繁發送資料來達到攻擊的目的,nginx可以透過HttpLimitReqModul和HttpLimitZoneModule配置來限制ip在同一時間段的訪問次數來防cc攻擊。 HttpLimitReqModul用來限制連單位時間內連接數的模組,使用limit_req_zone和limit_req指令搭配使用來達到限制。一旦並發連接超過指定數量,就會傳回503錯誤。 HttpLimitConnModul用來限制單一ip的並發連接數,使用limit_zone和limit_conn指令這兩個模組的區別前一個是對一段時間內的連接數限制,後者是對同一時刻的連接數限制。

    

HttpLimitReqModul 限制某一段時間內相同ip存取數實例

<span>http<span>{<span>    <span>...<span>
    <span>#定义一个名为allips的limit_req_zone用来存储session,大小是10M内存,<span>    <span>#以$binary_remote_addr 为key,限制平均每秒的请求为20个,<span>    <span>#1M能存储16000个状态,rete的值必须为整数,<span>    <span>#如果限制两秒钟一个请求,可以设置成30r/m<span>
    limit_req_zone $binary_remote_addr zone<span>=<span>allips<span>:<span>10m<span> rate<span>=<span>20r<span>/<span>s<span>;<span>    <span>...<span>    server<span>{<span>        <span>...<span>        location <span>{<span>            <span>...<span>
            <span>#限制每ip每秒不超过20个请求,漏桶数burst为5<span>            <span>#brust的意思就是,如果第1秒、2,3,4秒请求为19个,<span>            <span>#第5秒的请求为25个是被允许的。<span>            <span>#但是如果你第1秒就25个请求,第2秒超过20的请求返回503错误。<span>            <span>#nodelay,如果不设置该选项,严格使用平均速率限制请求数,<span>            <span>#第1秒25个请求时,5个请求放到第2秒执行,<span>            <span>#设置nodelay,25个请求将在第1秒执行。<span>
            limit_req zone<span>=<span>allips burst<span>=<span>5<span> nodelay<span>;<span>            <span>...<span>        <span>}<span>        <span>...<span>    <span>}<span>    <span>...<span><span>}</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span>

HttpLimitZoneModule 限制並發連接數個實例可以在連接數位作用域

<span>http<span>{<span>    <span>...<span>
    <span>#定义一个名为one的limit_zone,大小10M内存来存储session,<span>    <span>#以$binary_remote_addr 为key<span>    <span>#nginx 1.18以后用limit_conn_zone替换了limit_conn<span>    <span>#且只能放在http作用域<span>    limit_conn_zone   one  $binary_remote_addr  <span>10m<span>;<span>  
    <span>...<span>    server<span>{<span>        <span>...<span>        location <span>{<span>            <span>...<span>           limit_conn one <span>20<span>;<span>          <span>#连接数限制<span>
           <span>#带宽限制,对单个连接限数,如果一个ip两个连接,就是500x2k<span>           limit_rate <span>500k<span>;<span>            

            <span>...<span>        <span>}<span>        <span>...<span>    <span>}<span>    <span>...<span><span>}</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span>

nginx白名單設定

以上配置會對所有的ip都進行限制,有些時候我們不希望對搜尋引擎的蜘蛛或自己測試ip進行限制,對於特定的白名單ip我們可以藉助geo指令實現。

1.

<span>http<span>{<span>     geo $limited<span>{<span>        <span>default<span> <span>1<span>;<span>        <span>#google <span>        <span>64.233<span>.<span>160.0<span>/<span>19<span> <span>0<span>;<span>        <span>65.52<span>.<span>0.0<span>/<span>14<span> <span>0<span>;<span>        <span>66.102<span>.<span>0.0<span>/<span>20<span> <span>0<span>;<span>        <span>66.249<span>.<span>64.0<span>/<span>19<span> <span>0<span>;<span>        <span>72.14<span>.<span>192.0<span>/<span>18<span> <span>0<span>;<span>        <span>74.125<span>.<span>0.0<span>/<span>16<span> <span>0<span>;<span>        <span>209.85<span>.<span>128.0<span>/<span>17<span> <span>0<span>;<span>        <span>216.239<span>.<span>32.0<span>/<span>19<span> <span>0<span>;<span>        <span>#M$<span>        <span>64.4<span>.<span>0.0<span>/<span>18<span> <span>0<span>;<span>        <span>157.60<span>.<span>0.0<span>/<span>16<span> <span>0<span>;<span>        <span>157.54<span>.<span>0.0<span>/<span>15<span> <span>0<span>;<span>        <span>157.56<span>.<span>0.0<span>/<span>14<span> <span>0<span>;<span>        <span>207.46<span>.<span>0.0<span>/<span>16<span> <span>0<span>;<span>        <span>207.68<span>.<span>192.0<span>/<span>20<span> <span>0<span>;<span>        <span>207.68<span>.<span>128.0<span>/<span>18<span> <span>0<span>;<span>        <span>#yahoo<span>        <span>8.12<span>.<span>144.0<span>/<span>24<span> <span>0<span>;<span>        <span>66.196<span>.<span>64.0<span>/<span>18<span> <span>0<span>;<span>        <span>66.228<span>.<span>160.0<span>/<span>19<span> <span>0<span>;<span>        <span>67.195<span>.<span>0.0<span>/<span>16<span> <span>0<span>;<span>        <span>74.6<span>.<span>0.0<span>/<span>16<span> <span>0<span>;<span>        <span>68.142<span>.<span>192.0<span>/<span>18<span> <span>0<span>;<span>        <span>72.30<span>.<span>0.0<span>/<span>16<span> <span>0<span>;<span>        <span>209.191<span>.<span>64.0<span>/<span>18<span> <span>0<span>;<span>        <span>#My IPs<span>        <span>127.0<span>.<span>0.1<span>/<span>32<span> <span>0<span>;<span>        <span>123.456<span>.<span>0.0<span>/<span>28<span> <span>0<span>;<span> <span>#example for your server CIDR<span>    <span>}</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span>

geo指令定義了一個白名單$limited變量,預設值為1,如果客戶端ip在上面的範圍內,$limited的值為0
2.使用map ip為空串,如果不是搜尋引擎就顯示本身真是的ip,這樣搜尋引擎ip就不能存到limit_req_zone記憶體session中,所以不會限制搜尋引擎的ip存取

map $limited $limit {

1 $binary_remote_addr ;

0 "";

}

3.設定limit_req_zone和limit_req
limit_req_zone $limit z rate=10r/m;

limit_req z burst=5;=10r/m;

limit_req z burst=5;的方法效果實際測試下

例1:限制只允許一分鐘內只允許一個ip訪問60次配置,也就是平均每秒1次

首先我們準備一個php腳本放在根目錄下$document_root

test. php


nginx配置增加
limit_req_zone

 和 

limit_req

<span>http<span>{<span>    <span>...<span>    limit_req_zone $binary_remote_addr zone<span>=<span>allips<span>:<span>10m<span> rate<span>=<span>60r<span>/<span>m<span>;<span>    <span>...<span>    server<span>{<span>        <span>...<span>        location <span>{<span>            <span>...<span>            limit_req zone<span>=<span>allips<span>;<span>            <span>...<span>        <span>}<span>        <span>...<span>    <span>}<span>    <span>...<span><span>}</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span>
ab -n 5 -c 1 http://www.weizhang.org/test.ab -n 5 -c 1 http://www.weizhang.org/test.ab -n 5 -c 1 http://www.weizhang.org/test.

ab -n 5 -c 1 http://www.weizhang.org/test.

ab -n 5 -c 1 http://www.weizhang.org/test.

06:27:06 +0000] "GET /test.php HTTP/1.0" 200 11000 "-" "ApacheBench/2.3"
118.144.94.193 - - [22/Dec/2012:06:27:06 +0000] GET /test.php HTTP/1.0" 503 537 "-" "ApacheBench/2.3"
118.144.94.193 - - [22/Dec/2012:06:27:07 +0000] "GET /test. HTTP/1. 503 537 "-" "ApacheBench/2.3"
118.144.94.193 - - [22/Dec/2012:06:27:07 +0000] "GET /test.php HTTP/1.0" 503 537 "
118.144.94.193 - - [22/Dec/2012:06:27:07 +0000] "GET /test.php HTTP/1.0" 503 537 "-" "ApacheBench/2.3"

看到該配置只允許每秒訪問1次,超出的請求返回503錯誤

<span>http<span>{<span>    <span>...<span>    limit_req_zone $binary_remote_addr zone<span>=<span>allips<span>:<span>10m<span> rate<span>=<span>60r<span>/<span>m<span>;<span>    <span>...<span>    server<span>{<span>        <span>...<span>        location <span>{<span>            <span>...<span>            limit_req zone<span>=<span>allips burst<span>=<span>1<span> nodelay<span>;<span>            <span>...<span>        <span>}<span>        <span>...<span>    <span>}<span>    <span>...<span><span>}</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span>
ab -n 5 -c 1 http://www.weizhang.org/test.php

118.144.94.193 - - [22 /Dec/2012:07:01:00 +0000] "GET /test.php HTTP/1.0" 200 11000 "-" "ApacheBench/2.3"

118.144.94.193 - - [22/Dec/2012:011: 00 +0000] "GET /test.php HTTP/1.0" 200 11000 "-" "ApacheBench/2.3"
118.144.94.193 - - [22/Dec/2012:07:01:01 +0000] php HTTP/1.0" 503 537 "-" "ApacheBench/2.3"
118.144.94.193 - - [22/Dec/2012:07:01:01 +0000] "GET /test.php HTTP/1.0" 503 537 "- " "ApacheBench/2.3"
118.144.94.193 - - [22/Dec/2012:07:01:01 +0000] "GET /test.php HTTP/1.0" 503 537 "-" "ApacheB/test.php HTTP/1.0" 503 537 "-" "ApacheB/test.php HTTP/1.0" 503 537 "-" "ApacheB/233". brust=1和nodelay後允許第1秒處理兩個請求。

以上就介紹了php nginx限制ip請求次數 以及並發次數,包括了方面的內容,希望對PHP教程有興趣的朋友有所幫助。

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn