zoukankan      html  css  js  c++  java
  • 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 {
             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 {
             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 {
             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 {
             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 {
             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 {
             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 {
             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>
    

      

    草都可以从石头缝隙中长出来更可况你呢
  • 相关阅读:
    2491 玉蟾宫
    1704 卡片游戏
    1020 孪生蜘蛛
    1215 迷宫
    3149 爱改名的小融 2
    1316 文化之旅 2012年NOIP全国联赛普及组
    1664 清凉冷水
    157. [USACO Nov07] 奶牛跨栏
    [SCOI2005]繁忙的都市
    【NOIP2014模拟赛No.1】我要的幸福
  • 原文地址:https://www.cnblogs.com/rdchenxi/p/11153249.html
Copyright © 2011-2022 走看看