关于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)
Traffic Shaping和Traffic Policing
在桶满水之后,常见的两种处理方式为:
1)暂时拦截住上方水的向下流动,等待桶中的一部分水漏走后,再放行上方水
2)溢出的上方水直接抛弃
将水看作网络通信中数据包的抽象,则方式1起到的效果称为Traffic Shaping,方式2起到的效果称为Traffic Policing
由此可见,Traffic Shaping的核心理念是"等待",Traffic Policing的核心理念是"丢弃"。它们是两种常见的流速控制方法
Syntax: limit_req zone=name [burst=number] [nodelay];
Default: —
Context: http, server, location
示例
limit_req_zone $binary_remote_addr zone=one:10m rate=1r/s;
server {
location /search/ {
limit_req zone=one burst=5 nodelay;
}
第一段配置
第一个参数:$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(比如搜索引擎)的访问
limit_req_zone $anti_spider zone=one:10m rate=10r/s;
limit_req zone=one burst=100 nodelay;
if ($http_user_agent ~* "googlebot|bingbot|Feedfetcher-Google") {
set $anti_spider $http_user_agent;
}
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
Syntax: limit_conn zone number;
Default: —
Context: http, server, location
示例
http {
limit_conn_zone $binary_remote_addr zone=addr:10m;
...
server {
...
location /download/ {
limit_conn addr 1;
#带宽限制,对单个连接限数,如果一个ip两个连接,就是500x2k
limit_rate 100k;
}
}
}
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
正常情况下可以这样来配置
map $remote_addr $rt_filtered_ip {
default $binary_remote_addr;
1.2.3.4 "";
4.4.4.4 "";
}
or
geo $rt_filtered_ip {
default $binary_remote_addr;
127.0.0.1 "";
192.168.1.0/24 "";
10.1.0.0/16 "";
::1 "";
2001:0db8::/32 "";
1.2.3.4 ""
}
limit_conn_zone $binary_remote_addr zone=perip:10m;
limit_conn_zone $server_name zone=perserver:10m;
limit_conn_zone $host$uri zone=peruri:10m;
limit_req_zone $rt_filtered_ip zone=qps:10m rate=1r/s;
server {
location = /wp-login.php {
limit_req zone=qps burst=5 nodelay;
limit_conn perip 10;
limit_conn perserver 100;
limit_rate 500k;
include fastcgi_params;
fastcgi_pass 127.0.0.1:9000;
}
}
ab -n 100 -c 10 example.com/wp-login.php
$binary_remote_addr是限制同一客户端ip地址;
$server_name是限制同一server最大并发数;
limit_conn为限制并发连接数;
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/
关于nginx的限速模块的更多相关文章
- Nginx 限速模块一览
为了保护服务器不被刷流量,或者业务方面的一些限制,需要做一些限速措施. 一.http 请求并发连接数模块:ngx_http_limit_conn_module 这个模块可以设置每个定义的变量(比如客户 ...
- nginx利用geo模块做限速白名单以及geo实现全局负载均衡的操作记录
geo指令使用ngx_http_geo_module模块提供的.默认情况下,nginx有加载这个模块,除非人为的 --without-http_geo_module.ngx_http_geo_modu ...
- Nginx限速模块初探
Nginx限速模块分为哪几种?按请求速率限速的burst和nodelay参数是什么意思?漏桶算法和令牌桶算法究竟有什么不同?本文将带你一探究竟.我们会通过一些简单的示例展示Nginx限速模块是如何工作 ...
- (转)nginx利用geo模块做限速白名单以及geo实现全局负载均衡的操作记录
nginx利用geo模块做限速白名单以及geo实现全局负载均衡的操作记录 原文:http://www.cnblogs.com/kevingrace/p/6165572.html Nginx的geo模块 ...
- nginx上传模块nginx_upload_module和nginx_uploadprogress_module模块进度显示,如何传递GET参数等。
ownload:http://www.grid.net.ru/nginx/download/nginx_upload_module-2.2.0.tar.gzconfigure and make : . ...
- Nginx访问限制模块limit_conn_zone 和limit_req_zone配置使用
nginx可以通过limit_conn_zone 和limit_req_zone两个组件来对客户端访问目录和文件的访问频率和次数进行限制,另外还可以善用进行服务安全加固,两个模块都能够对客户端访问进行 ...
- web服务器-Nginx下载限速
web服务器-Nginx下载限速 一. 限速介绍 在生产环境中,为了保护WEB服务器的安全,我们都会对用户的访问做出一些限制,保证服务器的安全及资源的合理分配. 限流(rate limiting)是N ...
- php5.5安装及phpmyadmin&nginx配置php模块
安装php5.5: 下载源地址:rpm -Uvh rpm包安装:yum install php55w.x86_64 php55w-cli.x86_64 php55w-common.x86_64 php ...
- 为nginx增加nginx_http_concat模块
为nginx增加nginx_http_concat模块 时间 2013-06-05 22:14:56 我行我思 原文 http://www.fanjun.me/?p=562 主题 Nginx 缘由 ...
随机推荐
- [操作系统实验lab3]实验报告
[感受] 这次操作系统实验感觉还是比较难的,除了因为助教老师笔误引发的2个错误外,还有一些关键性的理解的地方感觉还没有很到位,这些天一直在不断地消化.理解Lab3里的内容,到现在感觉比Lab2里面所蕴 ...
- Sprint Three 回顾与总结&发表评论&团队贡献分
● 一.回顾与总结 (1)回顾 燃尽图: Sprint计划-流程图: milestones完成情况如下: (2)总结 从sprint one到three,我们团队配合十分默契,互相帮助,虽然遇到了不少 ...
- 使用Eclipse Installer安装Eclipse
由于一些原因,需要重新安装Eclipse,登陆到Eclipse官网下载Eclipse时发现社区又推出了Eclipse Installer.所以就下下来尝尝鲜. 刚开始确实有些选项不太明白,不过现在挺喜 ...
- QTableView表格视图的列宽设置
Qt中的表格控件可以通过从QTableView或QTableWidget派生子类实现.其中,QTableWidget只是对QTableView的一种简单封装.因为使用QTableView常常需要用户指 ...
- VC使用libcurl模拟登录CSDN并自动评论资源以获取积分
环境:Win7 64位+VC2008 软件及源码下载:(http://pan.baidu.com/s/1jGE52pK) 涉及到的知识点: C++多线程编程 libcurl的使用(包括发送http请求 ...
- 区间合并 --- Codeforces 558D : Gess Your Way Out ! II
D. Guess Your Way Out! II Problem's Link: http://codeforces.com/problemset/problem/558/D Mean: 一棵满二叉 ...
- P6 Enterprise Project Portfolio Contract Management
Find documentation for Primavera products here: Primavera Cloud Services Primavera Prime Primavera P ...
- PhpStorm的open in browser怎么修改端口和相对路径
昨天下班后,在电脑安装phpstorm.xampp安装正常,但是在phpstorm上直接打开网站文件一直报错,一直报错502.我感觉好奇快,怎么会报错呢.后面我用hbuild打开文件,在浏览器显示正常 ...
- Javascript面向对象编程(三) --- 非构造函数的继承
一.什么是"非构造函数"的继承? 比如,现在有一个对象,叫做"中国人". var Chinese = { nation:'中国' }; 还有一个对象,叫做&qu ...
- SharePoint 2013 排错之"Code blocks are not allowed in this file"
今天,设置页面布局的自定义母版页时,设置完了以后保存,然后预览报错,错误如下截图:删掉自定义母版页的MasterPageFile属性,页面依然报错:感觉甚是奇怪,因为有版本控制,还原为最初的版本,依然 ...