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
Welcome to nginx!
Welcome to nginx!
If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.
For online documentation and support please refer to
nginx.org.
Commercial support is available at
nginx.com.
Thank you for using nginx.
[root@python ~]# curl test.limit.com 同时请求第二连接
500 Internal Server Error
500 Internal Server Error
nginx/1.15.9
限制一个连接每秒处理的请求数
[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
Welcome to nginx!
Welcome to nginx!
If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.
For online documentation and support please refer to
nginx.org.
Commercial support is available at
nginx.com.
Thank you for using nginx.
[root@python vhast]# curl test.limit.com
503 Service Temporarily Unavailable
503 Service Temporarily Unavailable
nginx/1.15.9
设置连接池
[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
Welcome to nginx!
Welcome to nginx!
If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.
For online documentation and support please refer to
nginx.org.
Commercial support is available at
nginx.com.
Thank you for using nginx.
[root@python vhast]# curl test.limit.com
Welcome to nginx!
Welcome to nginx!
If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.
For online documentation and support please refer to
nginx.org.
Commercial support is available at
nginx.com.
Thank you for using nginx.
[root@python vhast]# curl test.limit.com
Welcome to nginx!
Welcome to nginx!
If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.
For online documentation and support please refer to
nginx.org.
Commercial support is available at
nginx.com.
Thank you for using nginx.
[root@python vhast]# curl test.limit.com
Welcome to nginx!
Welcome to nginx!
If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.
For online documentation and support please refer to
nginx.org.
Commercial support is available at
nginx.com.
Thank you for using nginx.
[root@python vhast]# curl test.limit.com
503 Service Temporarily Unavailable
503 Service Temporarily Unavailable
nginx/1.15.9
[root@python vhast]# curl test.limit.com
503 Service Temporarily Unavailable
503 Service Temporarily Unavailable
nginx/1.15.9
两个模块同时启用
[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
Welcome to nginx!
Welcome to nginx!
If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.
For online documentation and support please refer to
nginx.org.
Commercial support is available at
nginx.com.
Thank you for using nginx.
[root@python ~]# curl test.limit.com
503 Service Temporarily Unavailable
503 Service Temporarily Unavailable
nginx/1.15.9