Posts nginx控制ip连接和请求数
Post
Cancel

nginx控制ip连接和请求数

nginx有两个模块,分别用来配置 ip最大连接数 和单位时间内最多请求数

ip连接限制

ngx_http_limit_conn_module

官网模块地址: http://nginx.org/en/docs/http/ngx_http_limit_conn_module.html

主要指令 limit_conn_zone

使用示例:

1
2
3
4
5
6
7
8
9
http{
	limit_conn_zone $binary_remote_addr zone=perip:10m; 
	#定义一个 perip 访问限制区,连接使用的内存10M,存放ip信息 
	# $binary_remote_addr 表示ip作为键  即统计同一个ip连接数 

	server {
    	limit_conn perip 10; #最大连接数10
	}
}

ip请求次数限制

ngx_http_limit_req_module

官网模块地址: http://nginx.org/en/docs/http/ngx_http_limit_req_module.html

主要指令 limit_req_zone

使用示例:

1
2
3
4
5
6
7
8
9
10
11
12
http{
    limit_req_zone $binary_remote_addr zone=perip:10m rate=5r/s;
	#定义一个 perip 请求限制区 使用10m内存存放键统计信息 
    #$binary_remote_addr 表示通过ip作为键统计次数
    # rate=5r/s 表示同一个ip每秒最多5个请求
    server {
        limit_req zone=perip burst=10 nodelay;
        # 定义此服务下 突发请求次数不超过10次 没有延迟 使用延迟 nodelay | delay=3  3个请求延迟处理
        #limit_req zone=perip burst=10 delay=3;
        
    }
}
This post is licensed under CC BY 4.0 by the author.

世界眷顾谁

互联网备忘录

Comments powered by Disqus.