当前位置:   article > 正文

关于nginx的限速模块

关于nginx的限速模块

nginx 使用 ngx_http_limit_req_module和ngx_http_limit_conn_module 来限制对资源的请求

这种方法,对于CC攻击(Challenge Collapsar)or DDOS(分布式拒绝服务)有一定的用处

1、HttpLimitReqModule

限制request 事实上就是 the processing rate of requests coming from a single IP address,使用的是漏桶算法(Leaky Bucket)

Leaky Bucket有两种处理方式,具体可以看wiki

Traffic Shaping和Traffic Policing
在桶满水之后,常见的两种处理方式为:
1)暂时拦截住上方水的向下流动,等待桶中的一部分水漏走后,再放行上方水
2)溢出的上方水直接抛弃
将水看作网络通信中数据包的抽象,则方式1起到的效果称为Traffic Shaping,方式2起到的效果称为Traffic Policing
由此可见,Traffic Shaping的核心理念是"等待",Traffic Policing的核心理念是"丢弃"。它们是两种常见的流速控制方法

  1. Syntax: limit_req zone=name [burst=number] [nodelay];
  2. Default: —
  3. Context: http, server, location

示例

  1. limit_req_zone $binary_remote_addr zone=one:10m rate=1r/s;
  2. server {
  3. location /search/ {
  4. limit_req zone=one burst=5 nodelay;
  5. }
第一段配置

第一个参数:$binary_remote_addr 表示通过remote_addr这个标识来做限制,“binary_”的目的是缩写内存占用量,是限制同一客户端ip地址
第二个参数:zone=one:10m表示生成一个大小为10M,名字为one的内存区域,用来存储访问的频次信息
第三个参数:rate=1r/s表示允许相同标识的客户端的访问频次,这里限制的是每秒1次,还可以有比如30r/m的

第二段配置

第一个参数:zone=one 设置使用哪个配置区域来做限制,与上面limit_req_zone 里的name对应
第二个参数:burst=5,重点说明一下这个配置,burst爆发的意思,这个配置的意思是设置一个大小为5的缓冲区当有大量请求(爆发)过来时,超过了访问频次限制的请求可以先放到这个缓冲区内
第三个参数:nodelay,如果设置,超过访问频次而且缓冲区也满了的时候就会直接返回503,如果没有设置,则所有请求会等待排队

 

下面这个配置可以限制特定UA(比如搜索引擎)的访问

  1. limit_req_zone $anti_spider zone=one:10m rate=10r/s;
  2. limit_req zone=one burst=100 nodelay;
  3. if ($http_user_agent ~* "googlebot|bingbot|Feedfetcher-Google") {
  4. set $anti_spider $http_user_agent;
  5. }

 

2、ngx_http_limit_conn_module

这个模块就是 limit  the number of connections from a single IP address

Not all connections are counted. A connection is counted only if it has a request processed by the server and the whole request header has already been read

  1. Syntax: limit_conn zone number;
  2. Default: —
  3. Context: http, server, location

示例

  1. http {
  2. limit_conn_zone $binary_remote_addr zone=addr:10m;
  3. ...
  4. server {
  5. ...
  6. location /download/ {
  7. limit_conn addr 1;
  8. #带宽限制,对单个连接限数,如果一个ip两个连接,就是500x2k
  9. limit_rate 100k;
  10. }
  11. }
  12. }

Sets the shared memory zone and the maximum allowed number of connections for a given key value. When this limit is exceeded, the server will return the 503 (Service Temporarily Unavailable) error in reply to a request

  • $binary_remote_addr是限制同一客户端ip地址
  • $server是限制同一server最大并发数
  • limit_conn为限制并发连接数,nginx 1.18以后用limit_conn_zone替换了limit_conn

配置完之后,我们可以使用ab或者webbench来测试一下

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

正常情况下可以这样来配置

  1. map $remote_addr $rt_filtered_ip {
  2. default $binary_remote_addr;
  3. 1.2.3.4 "";
  4. 4.4.4.4 "";
  5. }
  6. or
  7. geo $rt_filtered_ip {
  8. default $binary_remote_addr;
  9. 127.0.0.1 "";
  10. 192.168.1.0/24 "";
  11. 10.1.0.0/16 "";
  12. ::1 "";
  13. 2001:0db8::/32 "";
  14. 1.2.3.4 ""
  15. }
  16. limit_conn_zone $binary_remote_addr zone=perip:10m;
  17. limit_conn_zone $server_name zone=perserver:10m;
  18. limit_conn_zone $host$uri zone=peruri:10m;
  19. limit_req_zone $rt_filtered_ip zone=qps:10m rate=1r/s;
  20. server {
  21. location = /wp-login.php {
  22. limit_req zone=qps burst=5 nodelay;
  23. limit_conn perip 10;
  24. limit_conn perserver 100;
  25. limit_rate 500k;
  26. include fastcgi_params;
  27. fastcgi_pass 127.0.0.1:9000;
  28. }
  29. }
  30. ab -n 100 -c 10 example.com/wp-login.php
  31. $binary_remote_addr是限制同一客户端ip地址;
  32. $server_name是限制同一server最大并发数;
  33. limit_conn为限制并发连接数;
  34. limit_rate为限制下载速度;

 

参考

https://en.wikipedia.org/wiki/Leaky_bucket

http://nginx.org/en/docs/http/ngx_http_limit_conn_module.html

http://nginx.org/en/docs/http/ngx_http_limit_req_module.html

https://rtcamp.com/tutorials/nginx/block-wp-login-php-bruteforce-attack/

声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop】
推荐阅读
相关标签
  

闽ICP备14008679号