ngx_http_limit_conn_module这个模块用于限制每个定义的key值的连接数,特别是单IP的连接数。

  不是所有的连接数都会被计数。一个符合计数要求的连接是整个请求头已经被读取的连接。

  控制nginx并发连接数量参数如下说明:

  limit_conn_zone参数:

  参数语法:limit_conn_zone key zone=name:szie;

  上下文:http 标签块

  用于设置共享区域内存,key可以是字符串、nginx自带变量或者前两个组合,如$binary_remove_addr、server_name为内存区域的名称,name为内存区域的名称,size为内存区域的大小。

  

  limit_conn参数:

  参数语法:limit_conn zone number;

  上下文:http、server、location 标签块

  用于指定key设置最大连接数,当超过最大连接数时,服务器会返回503错误。

  限制单IP并发连接数

  nginx配置文件如下:(红色标记为添加或者修改内容)

  

[root@Nginx conf]# cat nginx.conf
worker_processes ;
worker_rlimit_nofile ;
worker_cpu_affinity ;
error_log logs/error.log;
events {
worker_connections ;
}
http {
include mime.types;
server_tokens on;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout ;
#limit_req_zone $binary_remove_addr zone=one:10m rate=1r/s;
#limit_req zone=one burst=;
limit_conn_zone $binary_remove_addr zone=addr:10m; # 添加limit_conn_zone参数
client_header_timeout ;
client_boby_timeout ;
send_timeout ;
client_max_boby_size 8m;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
include www_date/brian.conf;
include www_date/brianzjz.conf;
include www_date/status.conf;
}

  虚拟主机配置文件,添加limit_conn参数:

[root@Nginx www_date]# cat brian.conf
server {
listen ;
server_name www.brian.com;
location / {
root html/brian;
index index.html index.htm;
limit_conn addr ; # 设置单IP连接数为1
auth_basic "brian training";
auth_basic_user_file /opt/nginx/conf/htpasswd; }
location ~ .*\.(js|jpg|JPG|jpeg|JPEG|css|bmp|gif|GIF)$ {
access_log off;
}
access_log logs/brian.log main gzip buffer=128k flush=5s;
error_page /50x.html;
location = /50x.html {
root html;
}
}

  测试:(使用ab测试工具进行测试)

ab -c 1 -n 10 http://127.0.0.1/            # 测试命令

  测试过程中查看nginx的访问日志 ,这时候你会发现当并发为1时,返回值都是200 ,访问正常

  修改测试方式:

ab -c 2 -n 10 http://127.0.0.1/

  测试过程中查看nginx的访问日志 ,这时候你会发现当并发为2时,状态码200和503间隔是1:1 说明nginx已经做了并发连接的限制,对超时连接做出了503响应

以上功能可以用于服务器下载,限制每次访问下载目录的连接数

  限制虚拟主机总连接数:

  不仅可以限制单IP的并发连接数,还可以限制虚拟主机的总连接数,甚至可以对两个同时限制

  nginx配置文件如下:(红色标记为增加或者修改内容)

[root@Nginx conf]# cat nginx.conf
worker_processes ;
worker_rlimit_nofile ;
worker_cpu_affinity ;
error_log logs/error.log;
events {
worker_connections ;
}
http {
include mime.types;
server_tokens on;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout ;
#limit_req_zone $binary_remove_addr zone=one:10m rate=1r/s;
#limit_req zone=one burst=;
limit_conn_zone $binary_remove_addr zone=addr:10m; # 添加下面红色标记的两项
limit_conn_zone $server_name zone=perserver:10m;
client_header_timeout ;
client_boby_timeout ;
send_timeout ;
client_max_boby_size 8m;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
include www_date/brian.conf;
include www_date/brianzjz.conf;
include www_date/status.conf;
}

  虚拟主机配置文件:(红色标记为增加或者修改内容)

[root@Nginx www_date]# cat brian.conf
server {
listen ;
server_name www.brian.com;
location / {
root html/brian;
index index.html index.htm;
#limit_conn addr ;
   limit_conn perserver ; # 添加此项
auth_basic "brian training";
auth_basic_user_file /opt/nginx/conf/htpasswd; }
location ~ .*\.(js|jpg|JPG|jpeg|JPEG|css|bmp|gif|GIF)$ {
access_log off;
}
access_log logs/brian.log main gzip buffer=128k flush=5s;
error_page /50x.html;
location = /50x.html {
root html;
}
}

  测试:(使用ab测试工具进行测试)

ab -c 5 -n 1000 http://127.0.0.1/    # 测试命令(并发连接数为5 访问1000次)

  在nginx的统计日志中200和503的状态码出现的次数接近是2:1 说明配置生效成功

Nginx控制并发连接数的更多相关文章

  1. nginx——控制 Nginx 并发连接数

    1. 限制单个 IP 的并发连接数 .... http { include mime.types; default_type application/octet-stream; sendfile on ...

  2. PHP + NGINX 控制视频文件播放,并防止文件下载

    最简单的方法是使用NGINX的 internal 功能 server {    listen 80;    server_name www.xxx.com;     location / {      ...

  3. Linux下查看Nginx的并发连接数和连接状态-乾颐堂

    Linux下查看Nginx等的并发连接数和连接状态. 1.查看Web服务器(Nginx Apache)的并发请求数及其TCP连接状态: netstat -n | awk '/^tcp/ {++S[$N ...

  4. 查看 nginx 的并发连接数

    通过查看Nginx的并发连接,我们可以更清除的知道网站的负载情况.Nginx并发查看有两种方法(之所以这么说,是因为笔者只知道两种),一种是通过web界面,一种是通过命令,web查看要比命令查看显示的 ...

  5. Nginx查看并发连接数

    Nginx查看并发连接 通过界面查看 通过界面查看通过web界面查看时Nginx需要开启status模块,也就是安装Nginx时加上 --with-http_stub_status_module 然后 ...

  6. Nginx控制客户端请求的速率

    使用ngx_http_limit_req_module模块的两个参数 ngx_http_limit_req_module模块用于限制每个IP访问每个定义key的请求速率 1.limit_req_zon ...

  7. nginx最大并发连接数的思考:worker_processes、worker_connections、worker_rlimit_nofile

    参考nginx官网:http://nginx.org/en/docs/ngx_core_module.html#worker_connections 从用户的角度,http 1.1协议下,由于浏览器默 ...

  8. nginx 查看 并发连接数

    通过命令查看 #netstat -n | awk '/^tcp/ {++S[$NF]} END {for(a in S) print a, S[a]}' TIME_WAIT 17 ESTABLISHE ...

  9. nginx 查看并发连接数

    这里仅仅说一下用命令查看(也可配置页面) 通过查tcp连接数 1.netstat -n | awk '/^tcp/ {++S[$NF]} END {for(a in S) print a, S[a]} ...

随机推荐

  1. 08-01 java 帮助文档的制作和使用,使用jdk提供的帮助文档

    01_帮助文档的制作和使用 制作说明书的流程 如何制作一个说明书呢? A:写一个工具类 B:对这个类加入文档注释 怎么加呢? 加些什么东西呢? C:用工具解析文档注释 javadoc工具 D:格式 j ...

  2. (转)SSL/TLS 漏洞“受戒礼”,RC4算法关闭

    原文:https://blog.csdn.net/Nedved_L/article/details/81110603 SSL/TLS 漏洞“受戒礼” 一.漏洞分析事件起因2015年3月26日,国外数据 ...

  3. Java抽象类应用—模板方法模式

    模板方法模式(Templete method) 定义一个操作中的算法的骨架,而将一些可变部分的实现延迟到子类中,模板方法模式使得子类可以不改变一个算法的结构即可重新定义该算法的某些特定的步骤. 例: ...

  4. [Python 从入门到放弃] 3. BIF(内建函数)

    BIF (built-in functions) Python中提供了70多个内建函数,具备大量的现成功能. BIF不需要专门导入,可以直接使用,拿来就用 1.print() # 在屏幕上打印输出 如 ...

  5. jdk8-lambda表达式的使用

    1, 遍历list集合 List<Integer> list = new ArrayList<>(); list.add(1); list.add(2); list.add(3 ...

  6. 如何精准实现OCR文字识别?

    欢迎大家前往腾讯云+社区,获取更多腾讯海量技术实践干货哦~ 本文由云计算基础发表于云+社区专栏 前言 2018年3月27日腾讯云云+社区联合腾讯云智能图像团队共同在客户群举办了腾讯云OCR文字识别-- ...

  7. Customizing docker

    Customizing docker The Docker systemd unit can be customized by overriding the unit that ships with ...

  8. 代码部署工具walle(一)

    一.概述 代码部署上线.权限控制.一键版本回滚,github地址:https://github.com/meolu/walle-web walle是基于php语言做的,所以需要一个php的安装环境. ...

  9. 阿里云服务器搭建SVN

    简单步骤介绍 1:安装svn apt-get install subversion 2. 开启svn服务器 svnserve -d 检查是否开启:ps aux | grep svnserve 若出现如 ...

  10. javascript 易错点、难点笔记

    本文主要记录在学习过程中遇到的JavaScript难点或者容易疏忽的细节,也方便自己日后翻阅学习. 1.arr.length === + arr.length arr.length === + arr ...