Linux学习之路运维点滴运维驿站

Nginx限连接限速率模块总结

2018-11-24  本文已影响23人  三杯水Plus

关于ngx_http_limit_conn_module、ngx_http_limit_req_module 模块,echo(需要安装第三方模块 ngx_http_echo_module),map(默认安装了ngx_http_map_module),geo(默认安装了ngx_http_geo_module)指令请查看官方文档,这里不再赘述。

有四种情况:

首先说明一个问题:
geo里面的IP可以是单个的,也可以是一个网段的,只要符合CIDR标准就行。
map里面的IP必须是当个,因为这里把他看着一个变量。
过CDN的白名单IP只需要客户端IP就行,CND不需要,客户端IP得一行一行写
不过CDN的白名单IP可以写一个网段
关键点limited为空时不走限速。有值的,这里white_ip为1,走限速。

测试方法

可以通过echo模块查看到(这两个都是没有走CDN的):
虚拟主机里echo的配置如下
location /echo {
default_type text/plain;
     echo http_x_forwarded_for: $http_x_forwarded_for;
     echo remote_addr: $remote_addr;
     echo firstAddr: $firstAddr;
     echo clientRealIp: $clientRealIp;
     echo white_ip: $white_ip;
}

这个要限速,没有加入白名单
http://123.11.11.11/echo
http_x_forwarded_for:
remote_addr: 59.12.13.14
firstAddr:
clientRealIp: 59.12.13.14
white_ip: 1
limited: 59.12.13.14

这个不限速,加入了白名单
http://123.11.11.11/echo
http_x_forwarded_for:
remote_addr: 114.11.183.6
firstAddr:
clientRealIp: 114.11.183.6
white_ip: 0
limited:

具体配置

  1. 不过CDN限速配置
nginx.conf里面的配置
geo $white_ip  {
        default 1;
        127.0.0.1 0;
        59.12.13.14  0;
        61.11.12.0/24  0;
......
}

map $white_ip $limited {
        1  $binary_remote_addr;
        0  "";
    }

limit_conn_zone $limited zone=addr:10m;
limit_req_zone  $limited zone=one:10m rate=50r/s;
limit_req_log_level info;
limit_conn_log_level info;

具体域名vhosts配置文件里面的应用
    location / {
          limit_req  zone=one burst=5  nodelay;
          limit_conn addr  100;
          proxy_pass http://my_test_com;
    }
  1. 过CDN限速配置
nginx.conf里面的配置
map $http_x_forwarded_for  $clientRealIpnormal {
        ""      $remote_addr;
        ~^(?P<firstAddr>[0-9\.]+),?.*$  $firstAddr;
}

map $http_http_cdn_src_ip $clientRealIp{
        ""   $clientRealIpnormal;
        default $http_http_cdn_src_ip;
}

map $clientRealIp  $white_ip  {
        default 1;
        127.0.0.1 0;
        59.12.13.14  0;
......

}

map $white_ip $limited {
        1  $clientRealIp;
        0  "";
    }

limit_conn_zone $limited zone=addr:10m;
limit_req_zone  $limited zone=one:10m rate=30r/s;
limit_req_zone  $limited zone=two:10m rate=20r/s;
limit_req_log_level info;
limit_conn_log_level info;

具体域名vhosts配置文件里面的应用
    location / {
          limit_req  zone=two burst=1  nodelay;
          proxy_pass http://mynew_test_com;
    }
  1. 不用白名单的不过CDN
nginx.conf里面的配置
limit_conn_zone $binary_remote_addr zone=addr:10m;
limit_req_zone  $binary_remote_addr zone=one:10m rate=50r/s;
limit_req_log_level info;
limit_conn_log_level info;

具体域名vhosts配置文件里面的应用
    location / {
          limit_req  zone=one burst=5  nodelay;
          limit_conn addr  100;
          proxy_pass http://my_test_com;
    }
  1. 不用白名单的过CDN
nginx.conf里面的配置
map $http_x_forwarded_for  $clientRealIp {
"" $remote_addr;
~^(?P<firstAddr>[0-9\.]+),?.*$ $firstAddr;
}

limit_conn_zone $clientRealIp_addr zone=addr:10m;
limit_req_zone  $clientRealIp_addr zone=one:10m rate=50r/s;
limit_req_log_level info;
limit_conn_log_level info;

具体域名vhosts配置文件里面的应用
    location / {
          limit_req  zone=one burst=5  nodelay;
          limit_conn addr  100;
          proxy_pass http://my_test_com;
    }
上一篇 下一篇

猜你喜欢

热点阅读