nginx的preaccess 阶段的limit_req模块与limit_conn模块
limit_conn 模块限制并发连接数
[root@python vhast]# vim limit_conn.conf limit_conn_zone $binary_remote_addr zone=addr:10m; #$binary_remote_addr 表示二进制格式IP地址;定义10M的共享内存
#limit_req_zone $binary_remote_addr zone=one:10m rate=1r/s;
server {
server_name test.limit.com;
root html/;
location /{
limit_conn_status 500; #当达到最大限制后,向用户返回一个错误码;默认503;修改为500
limit_conn_log_level warn; #
limit_rate 5; #限制返回用户的速度没秒5个 字节
limit_conn addr 1; #为查看测试效果设置并发连接为1
#limit_req zone=one burst=1 nodelay;
#limit_req zone=one;
}
}
测试
[root@python vhast]# curl test.limit.com
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
body {
width: 35em;
margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif;
}
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p> <p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p> <p><em>Thank you for using nginx.</em></p>
</body>
</html>
[root@python ~]# curl test.limit.com 同时请求第二连接
<html>
<head><title>500 Internal Server Error</title></head>
<body>
<center><h1>500 Internal Server Error</h1></center>
<hr><center>nginx/1.15.9</center>
</body>
</html>
限制一个连接每秒处理的请求数
[root@python vhast]# vim limit_conn.conf limit_conn_zone $binary_remote_addr zone=addr:10m; #$binary_remote_addr 表示二进制格式IP地址;定义10M的共享内存
limit_req_zone $binary_remote_addr zone=one:10m rate=2r/m; # 设置共享内存为10M,每分钟处理2个请求
server {
server_name test.limit.com;
root html/;
location /{
limit_conn_status 500; #当达到最大限制后,向用户返回一个错误码;默认503;修改为500
limit_conn_log_level warn; #
#limit_rate 5; #限制返回用户的速度没秒50 字节
#limit_conn addr 1; #为查看测试效果设置并发连接为1
#limit_req zone=one burst=3 nodelay; # 定义可以接受请求池里最大可以接受用户3个请求,使用默认响应码
limit_req zone=one; #定义使用共享内存
}
}
测试 访问两次
[root@python vhast]# curl test.limit.com
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
body {
width: 35em;
margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif;
}
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p> <p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p> <p><em>Thank you for using nginx.</em></p>
</body>
</html>
[root@python vhast]# curl test.limit.com
<html>
<head><title>503 Service Temporarily Unavailable</title></head>
<body>
<center><h1>503 Service Temporarily Unavailable</h1></center>
<hr><center>nginx/1.15.9</center>
</body>
</html>
设置连接池
[root@python vhast]# cat limit_conn.conf
limit_conn_zone $binary_remote_addr zone=addr:10m; #$binary_remote_addr 表示二进制格式IP地址;定义10M的共享内存
limit_req_zone $binary_remote_addr zone=one:10m rate=2r/m; # 设置共享内存为10M,每分钟处理2个请求
server {
server_name test.limit.com;
root html/;
location /{
limit_conn_status 500; #当达到最大限制后,向用户返回一个错误码;默认503;修改为500
limit_conn_log_level warn; #
#limit_rate 5; #限制返回用户的速度没秒50 字节
#limit_conn addr 1; #为查看测试效果设置并发连接为1
limit_req zone=one burst=3 nodelay; # 定义可以接受请求池里最大可以接受用户3个请求,使用默认响应码
#limit_req zone=one; #定义使用共享内存
}
}
访问第五次生效
[root@python vhast]# curl test.limit.com
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
body {
width: 35em;
margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif;
}
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p> <p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p> <p><em>Thank you for using nginx.</em></p>
</body>
</html>
[root@python vhast]# curl test.limit.com
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
body {
width: 35em;
margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif;
}
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p> <p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p> <p><em>Thank you for using nginx.</em></p>
</body>
</html>
[root@python vhast]# curl test.limit.com
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
body {
width: 35em;
margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif;
}
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p> <p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p> <p><em>Thank you for using nginx.</em></p>
</body>
</html>
[root@python vhast]# curl test.limit.com
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
body {
width: 35em;
margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif;
}
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p> <p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p> <p><em>Thank you for using nginx.</em></p>
</body>
</html>
[root@python vhast]# curl test.limit.com
<html>
<head><title>503 Service Temporarily Unavailable</title></head>
<body>
<center><h1>503 Service Temporarily Unavailable</h1></center>
<hr><center>nginx/1.15.9</center>
</body>
</html>
[root@python vhast]# curl test.limit.com
<html>
<head><title>503 Service Temporarily Unavailable</title></head>
<body>
<center><h1>503 Service Temporarily Unavailable</h1></center>
<hr><center>nginx/1.15.9</center>
</body>
</html>
两个模块同时启用
[root@python vhast]# vim limit_conn.conf limit_conn_zone $binary_remote_addr zone=addr:10m; #$binary_remote_addr 表示二进制格式IP地址;定义10M的共享内存
limit_req_zone $binary_remote_addr zone=one:10m rate=2r/m; # 设置共享内存为10M,每分钟处理2个请求
server {
server_name test.limit.com;
root html/;
location /{
limit_conn_status 500; #当达到最大限制后,向用户返回一个错误码;默认503;修改为500
limit_conn_log_level warn; #
limit_rate 50; #限制返回用户的速度没秒50 字节
limit_conn addr 1; #为查看测试效果设置并发连接为1
#limit_req zone=one burst=3 nodelay; # 定义可以接受请求池里最大可以接受用户3个请求,使用默认响应码503
limit_req zone=one; #定义使用共享内存
}
}
测试;返回的是503,这是因为limit_req在limit_conn之前执行因为red模块以经向客户段返回了,所有不会向用户返回500
[root@python vhast]# curl test.limit.com
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
body {
width: 35em;
margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif;
}
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p> <p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p> <p><em>Thank you for using nginx.</em></p>
</body>
</html> [root@python ~]# curl test.limit.com
<html>
<head><title>503 Service Temporarily Unavailable</title></head>
<body>
<center><h1>503 Service Temporarily Unavailable</h1></center>
<hr><center>nginx/1.15.9</center>
</body>
</html>
nginx的preaccess 阶段的limit_req模块与limit_conn模块的更多相关文章
- Nginx模块之limit_conn & limit_req
limit_conn模块 生效阶段:NGX_HTTP_PREACCESS_PHASE阶段 生效范围:全部worker进程(基于共享内存),进入preaccess阶段前不生效,限制的有效性取决于key的 ...
- Nginx PREACCESS阶段 如何限制每个客户端每秒处理请求数
L:56 limit_req_zone $binary_remote_addr zone=one:10m rate=2r/m;#以用户IP作为key 开辟共享内存10M 并且限制每分钟2个请求 rat ...
- nginx 的请求处理阶段
nginx处理的11个阶段 nginx处理用户请求的流程 接收用户请求头部之后 1 .匹配对应得location 2.是否进行限速 3.验证用户是否有权限访问该资源:和判断是否是盗链的请求 4.生成用 ...
- nginx限制请求之二:(ngx_http_limit_req_module)模块
相关文章: <高可用服务设计之二:Rate limiting 限流与降级> <nginx限制请求之一:(ngx_http_limit_conn_module)模块> <n ...
- 循序渐进nginx(三):日志管理、http限流、https配置,http_rewrite模块,第三方模块安装,结语
目录 日志管理 access_log error_log 日志文件切割 自定义错误页 http访问限流 限制请求数 语法 使用 限制连接数 语法 测试 补充: https配置 使用 生成证书 配置ng ...
- Nginx服务编译安装、日志功能、状态模块及访问认证模式实操
系统环境 [root@web ~]# cat /etc/redhat-release CentOS release 6.9 (Final) [root@web ~]# uname -a Linux d ...
- NGINX 加载动态模块(NGINX 1.9.11开始增加加载动态模块支持)
NGINX 1.9.11开始增加加载动态模块支持,从此不再需要替换nginx文件即可增加第三方扩展.目前官方只有几个模块支持动态加载,第三方模块需要升级支持才可编译成模块. tinywan@tinyw ...
- 高性能Web服务器Nginx的配置与部署研究(13)应用模块之Memcached模块+Proxy_Cache双层缓存模式
通过<高性能Web服务器Nginx的配置与部署研究——(11)应用模块之Memcached模块的两大应用场景>一文,我们知道Nginx从Memcached读取数据的方式,如果命中,那么效率 ...
- [转帖]利用nginx实现负载均衡 | 哈希算法,sticky模块实现session粘滞
利用nginx实现负载均衡 | 哈希算法,sticky模块实现session粘滞 2018年08月02日 10:06:03 Minza 阅读数 483 https://blog.csdn.net/ha ...
随机推荐
- nodejs的req取参req.body,req.params,req.query
1/req.query: Get:/domo?name=ximiximi&blog=https://home.cnblogs.com/u/ximiximi-blog/ app.get('/do ...
- CentOS6.5_x64卸载系统原有MySQL
1.查看系统是否存在MySQL的版本 rpm -qa | grep mysql 2.删除老版本的开头文件和库(rpm -e --nodeps XXX) rpm -e --nodeps mysql-5. ...
- 封装一个漂亮的ant design form标签组件
在ant design 的form组件中 能用于提交的组件比较少,所以我在这写了一个可以单选.多选标签提交的组件,调用非常简单. 代码: import React,{Fragment} from 'r ...
- stream.js
<script src='stream-min.js'></script> 下载 stream.js 2Kb minified streams是什么? Streams 是一个操 ...
- PHP中PHP $_POST和PHP $_REQUEST及PHP $_GET的用法及区别
笔者最近开始学习PHP语言大法,记录一下学习过程中遇到的知识点. 今天介绍的是php中有关 php $_post 和 php $_request 及 php $_get 的用法及区 ...
- spring中@Component注解
1.@controller 控制器(注入服务) 2.@service 业务(注入dao) 3.@repository dao(实现dao访问) 4.@component (把普通pojo实例化到spr ...
- Node.js Learning Notes
简介 简单的说 Node.js 就是运行在服务端的 JavaScript. Node.js 是一个基于Chrome JavaScript 运行时建立的一个平台. Node.js是一个事件驱动I/O服务 ...
- 1.1、webrtc的历史和现状
1.1.webrtc的历史和现状 本书目录 温馨提示:本书的内容,将按照顺序一一展开,上篇文章阐述本书的诞生的原因,推荐阅读方式等. 如果你还没有阅读上一篇文章(必读前言—— 作者的独白),我建议返回 ...
- 远程服务器返回错误: 404错误、远程服务器返回错误:500错误、 HttpWebResponse远程服务器返回错误:(404、500) 错误。
现象 我们编码实现请求一个页面时,请求的代码类似如下代码: HttpWebRequest req = (HttpWebRequest)WebRequest.Create(strUrl);req.Use ...
- 「NOIP2013」货车运输
传送门 Luogu 解题思路 首先 \(\text{Kruskal}\) 一下,构造出一棵森林. 并查集还要用来判断连通性. 倍增 \(\text{LCA}\) 的时候顺便维护一下路径最小值即可. 细 ...