使用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 ...
随机推荐
- Educational Codeforces Round 72 (Rated for Div. 2) A题
Problem Description: You play your favourite game yet another time. You chose the character you didn ...
- luogu 3998 [SHOI2013]发微博 map
考试的时候被卡常了~ code: #include <bits/stdc++.h> #define ll long long #define N 200002 #define setIO( ...
- HDOJ->考新郎(sdut1021)
考新郎 Problem Description 在一场盛大的集体婚礼中,为了使婚礼进行的丰富一些,司仪临时想出了有一个有意思的节目,叫做"考新郎",具体的操作是这样的: 首先,给每 ...
- error while loading shared libraries: libmysqlclient.so.20 问题小结
问题:安装完成sysbench之后,查看sysbench版本号时出现下面问题.这种报错很常见, [root@zero01 sysbench]# /usr/local/sysbench/bin/sysb ...
- python 识别鼠标左键点击
#coding=utf- import pyHook import pythoncom # 监听到鼠标事件调用 def onMouseEvent(event): if(event.MessageNam ...
- intel官方的手册
最近在学习汇编语言,需要用到intel的手册,无论是csdn还是其他的,都要下载币,还不便宜,也很老的资料了. 直接到这个地址:https://software.intel.com/en-us/art ...
- jQuery源代码学习之十——动画Animate
一.Animate模块的代码结构 // 定义了一些变量 tweeners = {}; function createFxNow() {} function createTween() {} funct ...
- 基于Kafka+ELK搭建海量日志平台
早在传统的单体应用时代,查看日志大都通过SSH客户端登服务器去看,使用较多的命令就是 less 或者 tail.如果服务部署了好几台,就要分别登录到这几台机器上看,等到了分布式和微服务架构流行时代,一 ...
- FLUENT多孔介质数值模拟设置【转载】
转载自:http://zhengjun0228.blog.163.com/blog/static/71377014200971895419613/ 多孔介质条件 多孔介质模型可以应用于很多问题,如通过 ...
- elasticsearch shield在java中的应用
官方文档:https://www.elastic.co/guide/en/shield/current/_using_elasticsearch_java_clients_with_shield.ht ...