Rumah > pembangunan bahagian belakang > tutorial php > php nginx限制ip请求次数 以及并发次数

php nginx限制ip请求次数 以及并发次数

WBOY
Lepaskan: 2016-07-30 13:31:28
asal
1556 orang telah melayarinya

     如何设置能限制某个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>
Salin selepas log masuk

HttpLimitZoneModule 限制并发连接数实例

limit_zone只能定义在http作用域,limit_conn可以定义在http server location作用域

<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>
Salin selepas log masuk

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>
Salin selepas log masuk

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;

最后我们使用ab压php-fpm的方式,对上面的方法效果实际测试下

例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>
Salin selepas log masuk

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

118.144.94.193 - - [22/Dec/2012: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.php HTTP/1.0" 503 537 "-" "ApacheBench/2.3"
118.144.94.193 - - [22/Dec/2012:06:27:07 +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.php HTTP/1.0" 503 537 "-" "ApacheBench/2.3"

未设置brust和nodelay可以看到该配置只允许每秒访问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>
Salin selepas log masuk

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:07:01:00 +0000] "GET /test.php HTTP/1.0" 200 11000 "-" "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 "-" "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"

设置brust=1和nodelay后允许第1秒处理两个请求。

以上就介绍了php nginx限制ip请求次数 以及并发次数,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

Label berkaitan:
sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan