nginx的preaccess 阶段的limit_req模块与limit_conn模块

Posted rdchenxi

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了nginx的preaccess 阶段的limit_req模块与limit_conn模块相关的知识,希望对你有一定的参考价值。

limit_conn 模块限制并发连接数

[[email protected] 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;
        

  测试

[[email protected] 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>
[[email protected] ~]# 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>

  限制一个连接每秒处理的请求数

[[email protected] 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; #定义使用共享内存
        

  测试 访问两次

[[email protected] 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>
[[email protected] 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>

  设置连接池

[[email protected] 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; #定义使用共享内存
	

  访问第五次生效

[[email protected] 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>
[[email protected] 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>
[[email protected] 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>
[[email protected] 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>
[[email protected] 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>
[[email protected] 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>

  两个模块同时启用

[[email protected] 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

[[email protected] 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>

[[email protected] ~]# 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模块的主要内容,如果未能解决你的问题,请参考以下文章

nginx lua redis 访问频率限制(转)

Openresty之resty.limit.conn模块介绍

Nginx处理HTTP请求的11个阶段

Nginx之HTTP模块

Nginx 限制ip的访问频率

nginx实现限速