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. 安装docker并使用docker安装mysql

    安装Docker 1. Docker 教程地址:https://www.runoob.com/docker/centos-docker.install.html 2.安装docker 命令:yum i ...

  2. Cesium 基于MapBox底图加载3DTiles 模型

    3DTiles 模型采用   CATIA V5 R22 --->3dxml --->GLB--->B3DM var extent = Cesium.Rectangle.fromDeg ...

  3. MSComm控件进行串口编程的基本步骤

    Visual C++为我们提供了一种好用的ActiveX控件Microsoft Communications Control(即MSComm)来支持应用程序对串口的访问,在应用程序中插入MSComm控 ...

  4. ubuntu 安装 gd

    最近装一套系统,提示没开启GD, 1.首先检查一下,是否安装 新建一个文件 <?php phpinfo(); ?> 如果安装了,会在页面显示 2.没安装当然没有了 这个安装也是根据php版 ...

  5. CF-----Pillars

    . Pillars There are n pillars aligned in a row and numbered from 1 to n. Initially each pillar conta ...

  6. 2、基本方法(Basic Recipes)

    学习目录:树莓派学习之路-GPIO Zero 官网地址:http://gpiozero.readthedocs.io/en/stable/recipes.html 环境:UbuntuMeta-16.0 ...

  7. codeforces-1271A - Suits

    A. Suits   A new delivery of clothing has arrived today to the clothing store. This delivery consist ...

  8. c++高斯消元法求解线性方程组

    #include<iostream> #include<math.h> #include<string.h> using namespace std; #defin ...

  9. 文件服务器之fastDFS

    FastDFS是一个开源的轻量级分布式文件系统,功能包括:文件存储.文件同步.文件访问(文件上传.文件下载)等,解决了大容量存储和负载均衡的问题.特别适合中小文件(建议范围:4KB < file ...

  10. Could not transfer artifact org.springframework.boot:spring-boot-starter-parent:pom:2.1.9.RELEASE from/to 阿里云镜像地址

    今天从 http://start.spring.io/ 下载的demo项目,导入eclipse后,pom文件一直报 parent包错,然后感觉就是自己maven镜像里面搜不到这个包, 所以改了 mav ...