zoukankan      html  css  js  c++  java
  • nginxhttp请求限制丶tcp会话限制和下载速度限制

    (1)nginx请求限制

    ngx_http_limit_req_module:开启对单个ip丶单个会话在单位时间内请求的限制rate表示限制的速率
    1.修改nginx配置文件

    #vim /usr/local/nginx/conf/nginx.conf 
    http {
    	limit_req_zone $binary_remote_addr  zone=req_zone:10m  rate=1r/s;		//1r/s表示一秒之内最多1次请求,也可以用5r/m,表示一分钟之内最多5次请求。
    	server {
    		location / {
    			limit_req    zone=req_zone;									//调用上面的请求限制;1r/s只接收单个ip1个请求,其余请求拒绝处理并返回错误码给客户端
                            #limit_req	zone=req_zone	burst=3	nodelay;				       //请求超过1r/s,剩下的将被延迟处理,请求数超过burst定义的数量,多余的请求返回503
    		}
    		}
    }
    #nginx -t 
    #nginx -s reload 
    


    2.压力测试

    #yum install httpd-tools -y 
    #ab -n 50 -c 20 http://192.168.1.31/index.html
    


    总共发起了50个请求,失败了49个请求;

    (2)nginx tcp会话连接请求

    ngx_http_limit_conn_module:开启对单个ip、单个会话同时存在的连接数的限制
    1.修改nginx配置文件

    #vim /usr/local/nginx/conf/nginx.conf 
    http {
    	limit_conn_zone	$binary_remote_addr	zone=conn_zone:10m;		//同一个时刻只允许一个客户端ip连接
    	server {
    		location {
    			limit_conn	  conn_zone	1;			//同一时刻只允许一个客户端ip一个tcp会话
    		}
    	}
    }
    

    2.压力测试

    #yum install httpd-tools -y 
    #ab -n 50 -c 20 http://192.168.1.31/index.html
    


    50个请求都成功了,说明50个请求都是用1个tcp连接
    注意:单个ip发起多个请求可以使用1个tcp会话连接,所以说请求限制比tcp的会话限制精准

    (3)下载速率限制

    limit_rate:开启nginx限速功能,可配置在http、server、location和if in location配置段。 limit_rate 500k表示限速500kB每秒,限速对象是单个连接,因此如果一个IP有多个连接的话,每个连接都是限速500k。

    #vim /usr/local/nginx/conf/nginx.conf 
    http {
        server {
            location {
                limit_rate 500k;                        //只能对单个ip单个连接做限制,限制500k
        }
        }
    }
    #nginx -t 
    #nginx -s reload
    
  • 相关阅读:
    开发错误记录2 .MainActivity (server)' ~ Channel is unrecoverably broken and will be disposed!
    开发错误记录1:解决:Only the original thread that created a view hierarchy can touch its views.
    第二篇 Python初识别及变量名定义规范
    第三篇 Python执行方式和变量初始
    第一篇 Python安装与环境变量的配置
    1. Linux系统常用操作
    Python的内存管理、命名规则、3个特性讲解
    操作系统及Python解释器工作原理讲解
    计算机基础小白篇
    Oracle SQL性能优化
  • 原文地址:https://www.cnblogs.com/lovelinux199075/p/9052632.html
Copyright © 2011-2022 走看看