如果我们自己有网站的话我们一定不希望有人恶意的疯狂请求数据,这会给网站带来压力,同时浪费咱们的流量(穷人.jpg)。这时就需要运用Nginx的限制请求频率功能了。
请输入图片描述

限制分类

      Nginx限制请求共有两种模式:

连接频率限制:ngx_http_limit_conn_module
请求频率限制:ngx_http_limit_req_module

      注意:需要知道的是一次请求和一次连接是不同的,一次连接要经历多次请求(TCP的一次连接需要经历三次握手请求),连接上后如果不断开则可以一直发送请求。

HTTP协议版本 连接关系
http 1.0 默认一次tcp连接进行一次http请求之后就会断开(由于资源浪费,有些服务器支持通过配置支持多次)
http 1.1 请求头配置:Connection:keep-alive = true,只要tcp连接不断开(默认2小时),一直可以进行http请求,但是一个tcp连接同一时间只支持一个http请求 Connection:keep-alive = false,只能发一次http请求
http 2.0 多路复用技术Multiplexing,一个tcp可以并发多个http请求(理论无上限,但是一般浏览器会有tcp并发数的限制)

连接限制

      Nginx的ngx_http_limit_conn_module模块用于限制每个已定义的key的连接数,特别是来自单个IP地址的连接数。并非所有连接都被计数。仅当具有服务器正在处理的请求并且已经读取了整个请求标头时,才对连接进行计数。

配置格式

语法: limit_conn_zone key zone=name:size;
默认: —
上下文: http

语法: limit_conn zone number;
默认: —
上下文: http, server, location
  • key: 根据什么进行限制,比如IP($binary_remote_addr)。
  • name: zone的名字,随意指定
  • size: 我们需要对连接进行计数,那么就需要空间存储,该值设置存储空间大小

示例

limit_conn_zone $binary_remote_addr zone=myname:1m;

server {
    location / {
        limit_conn myname 1;
    }
}

      以上配置了一个针对IP的连接限制,zone名称为myname,空间为1m,将其配置在了/下,限制连接限制为1。
      我们可以使用压测工具来进行测试,这里使用的是ab【了解ab】。使用命令ab -n 50 -c 20 http://server/index.html。测试报告如下所示:

This is ApacheBench, Version 2.3 <$Revision: 1430300 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking 123.56.45.209 (be patient).....done


Server Software:        nginx/1.16.1
Server Hostname:        123.56.45.209
Server Port:            80

Document Path:          /index.html
Document Length:        12 bytes

Concurrency Level:      20
Time taken for tests:   0.168 seconds
Complete requests:      50
Failed requests:        49
   (Connect: 0, Receive: 0, Length: 49, Exceptions: 0)
Write errors:           0
Non-2xx responses:      49
Total transferred:      34872 bytes
HTML transferred:       24218 bytes
Requests per second:    298.00 [#/sec] (mean)
Time per request:       67.115 [ms] (mean)
Time per request:       3.356 [ms] (mean, across all concurrent requests)
Transfer rate:          202.96 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:       20   21   0.5     21      22
Processing:    20   21   0.5     21      22
Waiting:       20   21   0.5     21      22
Total:         40   42   1.0     42      44

      根据Complete requests:50Failed requests:49可知连接限制配置成功。查看nginx错误日志:

2020/06/16 18:00:29 [error] 21051#21051: *8111 limiting requests, excess: 0.876 by zone "req_zone", client: 202.200.201.7, server: localhost, request: "GET /index.html HTTP/1.0", host: "xxx"
2020/06/16 18:00:29 [error] 21051#21051: *8112 limiting requests, excess: 0.876 by zone "req_zone", client: 202.200.201.7, server: localhost, request: "GET /index.html HTTP/1.0", host: "xxx"
2020/06/16 18:00:29 [error] 21051#21051: *8114 limiting requests, excess: 0.875 by zone "req_zone", client: 202.200.201.7, server: localhost, request: "GET /index.html HTTP/1.0", host: "xxx"

请求限制

      Nginx的ngx_http_limit_req_module模块用于限制每一个定义的的请求的处理速率,特别是从一个单一的IP地址的请求的处理速率。使用"漏斗"方法完成限制。

配置格式

语法: limit_req_zone key zone=name:size rate=rate;
默认: —
上下文: http

语法: limit_req zone=name [burst=number] [nodelay];
默认: —
上下文: http, server, location
  • delay参数指定一个限制,在该限制下,过多的请求将被延迟。默认值为零,即所有多余的请求都会延迟。

示例

limit_req_zone $binary_remote_addr zone=req_zone:1m rate=1r/s;

server {
    location / {
        limit_req zone=req_zone burst=3 nodelay;
    }
}
Last modification:October 31st, 2020 at 09:12 pm
如果觉得我的文章对你有用,请随意赞赏