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模块的更多相关文章

  1. Nginx模块之limit_conn & limit_req

    limit_conn模块 生效阶段:NGX_HTTP_PREACCESS_PHASE阶段 生效范围:全部worker进程(基于共享内存),进入preaccess阶段前不生效,限制的有效性取决于key的 ...

  2. Nginx PREACCESS阶段 如何限制每个客户端每秒处理请求数

    L:56 limit_req_zone $binary_remote_addr zone=one:10m rate=2r/m;#以用户IP作为key 开辟共享内存10M 并且限制每分钟2个请求 rat ...

  3. nginx 的请求处理阶段

    nginx处理的11个阶段 nginx处理用户请求的流程 接收用户请求头部之后 1 .匹配对应得location 2.是否进行限速 3.验证用户是否有权限访问该资源:和判断是否是盗链的请求 4.生成用 ...

  4. nginx限制请求之二:(ngx_http_limit_req_module)模块

    相关文章: <高可用服务设计之二:Rate limiting 限流与降级> <nginx限制请求之一:(ngx_http_limit_conn_module)模块> <n ...

  5. 循序渐进nginx(三):日志管理、http限流、https配置,http_rewrite模块,第三方模块安装,结语

    目录 日志管理 access_log error_log 日志文件切割 自定义错误页 http访问限流 限制请求数 语法 使用 限制连接数 语法 测试 补充: https配置 使用 生成证书 配置ng ...

  6. Nginx服务编译安装、日志功能、状态模块及访问认证模式实操

    系统环境 [root@web ~]# cat /etc/redhat-release CentOS release 6.9 (Final) [root@web ~]# uname -a Linux d ...

  7. NGINX 加载动态模块(NGINX 1.9.11开始增加加载动态模块支持)

    NGINX 1.9.11开始增加加载动态模块支持,从此不再需要替换nginx文件即可增加第三方扩展.目前官方只有几个模块支持动态加载,第三方模块需要升级支持才可编译成模块. tinywan@tinyw ...

  8. 高性能Web服务器Nginx的配置与部署研究(13)应用模块之Memcached模块+Proxy_Cache双层缓存模式

    通过<高性能Web服务器Nginx的配置与部署研究——(11)应用模块之Memcached模块的两大应用场景>一文,我们知道Nginx从Memcached读取数据的方式,如果命中,那么效率 ...

  9. [转帖]利用nginx实现负载均衡 | 哈希算法,sticky模块实现session粘滞

    利用nginx实现负载均衡 | 哈希算法,sticky模块实现session粘滞 2018年08月02日 10:06:03 Minza 阅读数 483 https://blog.csdn.net/ha ...

随机推荐

  1. AlertDialog 、SimpleDialog、 showModalBottomSheet、showToast 自定义 Dialog

    // AlertDialog .SimpleDialog.showModalBottomSheet.showToast // 使用showToast安装插件 https://pub.dev/packa ...

  2. Good Bye 2019E(点坐标缩小一半以区分奇偶性)

    设某个点的坐标为(x,y),根据坐标奇偶性公可分为四类,0表示偶数,1表示奇数,(0,0),(0,1),(1,0),(1,1). 如果所有点的坐标都属于一类,那么它们之间的距离都是4的倍数,无法分辨. ...

  3. 【PAT甲级】1107 Social Clusters (30分)(非递归并查集)

    题意: 输入一个正整数N(<=1000),表示人数,接着输入N行每行包括一个他的爱好数量:和爱好的序号.拥有相同爱好的人们可以默认他们在同一个俱乐部,输出俱乐部的数量并从大到小输出俱乐部的人数( ...

  4. markdown整理

    html标签# h1 ## h2 ### h3 #### h4 ##### h5 ###### h6 一级标题:内容=== 二级标题:内容--- 强调文字:>内容 链接:[文字](链接地址) 图 ...

  5. 理解ASP.NET Core验证模型 Claim, ClaimsIdentity, ClaimsPrincipal

    Claim, ClaimsIdentity, ClaimsPrincipal: Claim:姓名:xxx,领证日期:xxx ClaimsIdentity:身份证/驾照 ClaimsPrincipal: ...

  6. HBuilder笔记

    官网: https://uniapp.dcloud.io/quickstart HBuilderX - 高效极客技巧 https://ask.dcloud.net.cn/article/13191 插 ...

  7. sqlserver查询使用with(nolock)详解

    所有Select加 With (NoLock)解决阻塞死锁 在查询语句中使用 NOLOCK 和 READPAST 处理一个数据库死锁的异常时候,其中一个建议就是使用 NOLOCK 或者 READPAS ...

  8. 【C语言】极坐标转换为直角坐标

    写一个程序把极坐标(r,θ) (θ之单位为度)转换为直角坐标( X,Y). 转换公式是x=r.cosθ y=r.sinθ 程序输出:输出转换后的坐标. 弧度和角度的换算关系如下: 1弧度=180/π度 ...

  9. fiddler的代理设置

    fiddler通过监听系统的8888端口实现对系统浏览器发出的http报文进行截获监听,因此要使fiddler能够监听到浏览器数据,需要对浏览器设置代理端口 浏览器 <--8888端口代理--& ...

  10. 传奇定时器OnTimer功能详解(泡点、时间触发、任务活动)

    传奇定时器OnTimer功能详解(泡点.时间触发.任务活动) 定时器功能,是传奇服务端中非常常见的一种功能,常见如:泡点脚本.赌博脚本,任务活动指定时间刷怪,时间触发一些都需要用到OnTimer功能, ...