使用nginx构建限频、限速、限并发的应用保护层
使用nginx构建限频、限速、限并发的应用保护层
nginx本身提供了基础的限频、限速、限并发连接等能力。
限频
基于uri等限制某一个客户端,某类客户端持续时间段内建立连接的次数。
限速
限制客户端读取、发送数据包的速度,从总体看,即使限制网速。
限并发
限制客户端同时允许创建的连接,防止单个客户端创建过多连接耗尽服务器资源。
限频
http://nginx.org/en/docs/http/ngx_http_limit_req_module.html
http {
# 定义2条限速区域
# 以 $binary_remote_addr 字段作为限频统计点
# zone=one:10m # 定义区域名称为one,限统计总占用内存10MB
# 限制请求频率为 最多1次/per second
limit_req_zone $binary_remote_addr zone=one:10m rate=1r/s;
# 以 $binary_remote_addr 作为统计限频的依据
# zone=zone2:32m zone名称定为zone2,定义32MB的统计内存占用
# 限制请求频率 最多10次/per minute
limit_req_zone $binary_remote_addr zone=zone2:32m rate=10r/m;
# Sets the desired logging level for cases when the server refuses to
# process requests due to rate exceeding, or delays request processing.
# Logging level for delays is one point less than for refusals;
# for example, if “limit_req_log_level notice” is specified, delays are logged with the info level.
limit_req_log_level info; # default error, <info | notice | warn | error>
limit_req_status 503; # default 503 added from nginx-1.3.15
server {
# ...
location /search/ {
# 使用zone=one的限频规则
limit_req zone=one burst=5;
# limit_req zone=zone2 burst=5 nodelay; # 使用
}
}
详细解释
限频规则定义时,可以使用内置变量$binary_remote_addr或者自定义变量。
比如,此次使用了$binary_remote_addr ( $binary_remote_addr据各资料解释,是$remote_addr的二进制描述形式,即字符串"10.0.12.1"的整数形式表示的IP地址,使用此值,将会降低单个ip的内存占用,能够统计更多的ip请求)。
如果,希望根据其它类条件做限速,比如 $user_agent等。
$realip_remote_addr
$remote_addr
$remote_user
内置变量列表 http://nginx.org/en/docs/varindex.html
使用自定义变量
正常情况下,我们的服务一般前面还有一层防护盾,或者其它四层的lvs/haproxy等代理,直接使用$remote_addr / $binary_remote_addr会取到负载均衡器或者盾的地址,导致本来应该针对实际用户IP的限速,变成针对整体盾的限速。
创宇盾
创宇盾会传递X-Connecting-Ip并设置其为收到的第一个层级请求的IP地址。
http://help.yunaq.com/faq/67/index.html
为方便使用,可以通过map指令将其记录到自定义变量中$clientRealIp
map $http_x_connecting_ip $clientRealIp {
default $remote_addr;
"~(\d+\.\d+\.\d+\.\d+)" $1;
}
然后限频定义时,直接引用即可。
limit_req_zone $clientRealIp zone=zoneA:32m rate=30r/m;
limit_req_zone $clientRealIp zone=zoneB:32m rate=6r/m;
# levels info | notice | warn | error; default error
limit_req_log_level notice;
最终,在location中进行限频保护。
server {
location *~ ^/view/ {
limite_req zone=zoneA nodelay;
if ( $limit_req_status = REJECTED ) {
default_type text/plain;
return 419 "frequency limit";
}
proxy_pass http://1.1.1.1:8090;
}
location *~ ^/api/v1/ {
limit_req zone=zoneB burst=3 nodelay;
if ( $limit_req_status = REJECTED ) {
default_type application/json;
return 200 '{"code":419, "status":"failed","message":"频繁访问"}';
}
proxy_pass http://1.1.1.1:8090;
}
}
限速
...
限并发
...
自定义错误码和错误页
...
其它
nginx location/map 正则、map调测工具
docker run -p8080:80 -d --name nginx-regex-tester ruanzx/nginx-regex-tester
使用nginx构建限频、限速、限并发的应用保护层的更多相关文章
- Nginx对同一IP限速限流
limit_conn_zone是限制同一个IP的连接数,而一旦连接建立以后,客户端会通过这连接发送多次请求,那么limit_req_zone就是对请求的频率和速度进行限制. limit_conn_zo ...
- 性能提速:debounce(防抖)、throttle(节流/限频)
debounce与throttle是用户交互处理中常用到的性能提速方案,debounce用来实现防抖动,throttle用来实现节流(限频).那么这两个方法到底是什么(what)?为何要用(why-解 ...
- redis限频
做法 使用redis的lua脚本功能来限频 在redis中定时刷新系统时间来作为一个全局的时钟 限频脚本: /** * 获取令牌的lua脚本 */ public final static String ...
- nginx利用limit模块设置IP并发防CC攻击
nginx利用limit模块设置IP并发防CC攻击 分类: 系统2013-01-21 09:02 759人阅读 评论(0) 收藏 举报 来源:http://blog.xencdn.net/nginx- ...
- nginx + uWSGI 为 django 提供高并发
django 的并发能力真的是令人担忧,这里就使用 nginx + uwsgi 提供高并发 nginx 的并发能力超高,单台并发能力过万(这个也不是绝对),在纯静态的 web 服务中更是突出其优越的地 ...
- Nginx的一些优化(突破十万并发)
Nginx的一些优化(突破十万并发) nginx指令中的优化(配置文件) worker_processes 8; nginx进程数,建议按照cpu数目来指定,一般为它的倍数. worker_cpu_a ...
- nginx简介(轻量级开源高并发web服务器:大陆使用者百度、京东、新浪、网易、腾讯、淘宝等)(并发量5w)(一般网站apache够用了,而且稳定)
nginx简介(轻量级开源高并发web服务器:大陆使用者百度.京东.新浪.网易.腾讯.淘宝等)(并发量5w)(一般网站apache够用了,而且稳定) 一.总结 1.在连接高并发的情况下,Nginx是A ...
- 从SpringBoot构建十万博文聊聊限流特技
前言 在开发十万博客系统的的过程中,前面主要分享了爬虫.缓存穿透以及文章阅读量计数等等.爬虫的目的就是解决十万+问题:缓存穿透是为了保护后端数据库查询服务:计数服务解决了接近真实阅读数以及数据库服务的 ...
- Spring Security构建Rest服务-1401-权限表达式
Spring Security 的权限表达式 用法,在自定义的BrowserSecurityConfig extends WebSecurityConfigurerAdapter 配置文件里,每一个a ...
随机推荐
- percona-toolkit 主从工具 master-slave
复制类 pt-heartbeat 监控mysql复制延迟 pt-slave-delay 设定从落后主的时间 pt-slave-find 查找和打印所有mysql复制层级关系 pt-slav ...
- 下载、配置全新的eclipse
1.https://www.eclipse.org/downloads/ 2.确保安装配置了JDK,打开eclipse-inst-win64.exe,让eclipse installer程序UPDAT ...
- 格符\b的使用示例:每隔1秒消去1个字符
/* 退格符\b的使用示例:每隔1秒消去1个字符 */ #include <time.h> #include <stdio.h> /*--- 等待x毫秒 ---*/ int s ...
- saltstack运维工具
salt介绍 saltstack是由thomas Hatch于2011年创建的一个开源项目,设计初衷是为了实现一个快速的远程执行系统. salt强大吗 系统管理员日常会进行大量的重复性操作,例如安装软 ...
- Python学习日记(二)——字符转编码操作
首先搞清楚:Python3的默认编码是unicode,Python2的默认编码是ASCII码 为什么需要编解码? 打个比方:假如说我做了一个游戏,叫<西游记>,游戏传到了日本去.但是日本人 ...
- spring中文参考指南
主要是4.x版本的 比较全面的:https://muyinchen.gitbooks.io/spring-framework-5-0-0-m3/content/3.5-bean/3.5.4-reque ...
- Facebook币Libra学习-4.新的智能合约语言Move入门
Move是一种新的编程语言,旨在为Libra Blockchain提供安全可编程的基础.Libra Blockchain中的帐户是任意数量的Move资源和Move模块的容器.提交给Libra Bloc ...
- kotlin之null值安全性
var a: String =null // 编译错误 var a: String? =null // 编译通过 要允许null值, 需要将变量声明为可为null的字符串类型:String? fun ...
- c++ template Queue
#pragma once#include <iostream>#include <iomanip> using namespace std; template<class ...
- CSS 自适应技巧
DIV的内容垂直居中 不再MARGINT-TOP多少 来居中显示 display:table-cell; #block-1{ width:100%; height:80px; display:tabl ...