优化开启Nginx的最大并发性能【支持2万到3万并发量!】

优化开启Nginx的最大并发性能

参考:Nginx优化之高并发配置,支持2万到3万并发量

系统层面

# 调整同时打开文件数量
ulimit -n 20480

# TCP最大连接数(somaxconn)
echo 10000 > /proc/sys/net/core/somaxconn

# TCP连接立即回收、回用(recycle、reuse)
echo 1 > /proc/sys/net/ipv4/tcp_tw_reuseecho 1 > /proc/sys/net/ipv4/tcp_tw_recycle

# 不做TCP洪水抵御
echo 0 > /proc/sys/net/ipv4/tcp_syncookies

# 也可以直接使用优化后的配置,在/etc/sysctl.conf中加入:
net.core.somaxconn = 20480
net.core.rmem_default = 262144
net.core.wmem_default = 262144
net.core.rmem_max = 16777216
net.core.wmem_max = 16777216
net.ipv4.tcp_rmem = 4096 4096 16777216
net.ipv4.tcp_wmem = 4096 4096 16777216
net.ipv4.tcp_mem = 786432 2097152 3145728
net.ipv4.tcp_max_syn_backlog = 16384
net.core.netdev_max_backlog = 20000
net.ipv4.tcp_fin_timeout = 15
net.ipv4.tcp_max_syn_backlog = 16384
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_tw_recycle = 1
net.ipv4.tcp_max_orphans = 131072
net.ipv4.tcp_syncookies = 0

# 配置生效
sysctl -p

nginx层面

  • 增加work_rlimit_nofile数量
  • 增加worker_connections数量
  • 禁用keepalive_timeout
# 修改nginx配置文件,nginx.conf
worker_processes  1;
worker_rlimit_nofile 20000;		# 增加work_rlimit_nofile数量

events {
    use epoll;
    worker_connections 20000;	# 增加worker_connections数量
    multi_accept on;
}

http {
  keepalive_timeout 0;		  # 禁用keepalive_timeout
}

# 重启nginx
nginx -s reload

测试命令

ab命令全称为:Apache bench ,是Apache自带的压力测试工具;可以对服务器进行压力测试,可以测试安装Web服务器每秒种处理的HTTP请求。

缺点:会给服务器造成非常高的负载,可能会造成目标服务器资源耗尽,严重时可能会导致死机,而且它没有图形化结果不能监控,所以只能用作临时紧急任务和简单的测试。

ab命令会创建多个并发线程,模拟多个访问者同时对某一个url地址进行访问,测试的目标基于url;安装httpdab命令也会被同时安装,所以不需要再另行安装。

# 安装ab命令:
yum install httpd-tools

# 使用ab命令测试web服务器
ab https://www.baidu.com/

# 向web服务器发送指定并发指定次数的请求测试:求个数要大于并发数,否则失败
ab -n 1000 -c 100 https://www.baidu.com/

语法格式	# 注:地址末要加/号
ab [参数] [http/https/域名地址/IP地址/接口]
# 常用参数:
-n	一共发送多少个请求
-c	每个请求的并发次数

# 不常用参数
-A	指定连接服务器的基本的认证凭据
-C	添加cookie
-g	将测试结果输出为“gnuolot”文件
-h	显示帮助信息
-H	为请求追加一个额外的头
-i	使用“head”请求方式
-k	激活HTTP中的“keepAlive”特性
-p	指定包含数据的文件
-q	不显示进度百分比
-T	使用POST数据时,设置内容类型头
-v	设置详细模式等级
-w	以HTML表格方式打印结果
-x	以表格方式输出时,设置表格的属性
-X	使用指定的代理服务器发送请求
-y	以表格方式输出时,设置表格属性

开始压测

测试前查看连接数

业务低峰期连接数不多

image-20220628145340892

开始压测

指定发送10000次请求,每个请求5000个并发(求个数要大于并发数)

[root@h11 ~]# ab -n 10000 -c 5000 https://xxx.xxx.com/
# 以下为输出信息
This is ApacheBench, Version 2.3 <$Revision: 1430300 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking https://xxx.xxx.com/ (be patient)
SSL handshake failed (5).
SSL handshake failed (5).
SSL handshake failed (5).
SSL handshake failed (5).
SSL handshake failed (5).
SSL handshake failed (5).
SSL handshake failed (5).
SSL handshake failed (5).
SSL handshake failed (5).
SSL handshake failed (5).
Completed 1000 requests		# 完成1000个请求
SSL handshake failed (5).
SSL handshake failed (5).
SSL handshake failed (5).
SSL handshake failed (5).
SSL handshake failed (5).
SSL handshake failed (5).
SSL handshake failed (5).
SSL handshake failed (5).
SSL read failed (1) - closing connection
139747434387200:error:140E0197:SSL routines:SSL_shutdown:shutdown while in init:ssl_lib.c:1075:
SSL handshake failed (5).
SSL handshake failed (5).
SSL handshake failed (5).
SSL handshake failed (5).
SSL handshake failed (5).
SSL handshake failed (5).
SSL handshake failed (5).
Completed 2000 requests		# 完成2000个请求
SSL handshake failed (5).
SSL handshake failed (5).
SSL handshake failed (5).
SSL handshake failed (5).
SSL handshake failed (5).
SSL handshake failed (5).
SSL handshake failed (5).
SSL handshake failed (5).
Completed 3000 requests		# 完成3000个请求
SSL handshake failed (5).
SSL handshake failed (5).
SSL handshake failed (5).
SSL handshake failed (5).
^C						  # 此处我强制结束掉了

Server Software:        nginx/1.16.1								 # 服务器软件
Server Hostname:        https://xxx.xxx.com/	                        # 服务器主机名
Server Port:            443									         # 服务器端口
SSL/TLS Protocol:       TLSv1.2,ECDHE-RSA-AES128-GCM-SHA256,2048,128	# 协议

Document Path:          /			# 文档路径
Document Length:        8 bytes	     # 文档长度

Concurrency Level:      5000		# 并发级别
Time taken for tests:   31.081 seconds	# 测试所需的时间
Complete requests:      3346		   # 完成请求
Failed requests:        37			   # 失败请求
   (Connect: 0, Receive: 0, Length: 37, Exceptions: 0)
Write errors:           0			   # 写错误
Total transferred:      833868 bytes	# 总转移
HTML transferred:       26472 bytes		# HTML转移
Requests per second:    107.65 [#/sec] (mean)	# 每秒请求每个请求
Time per request:       46445.075 [ms] (mean)	# 每个请求
Time per request:       9.289 [ms] (mean, across all concurrent requests)	# 意思是,跨所有并发请求
Transfer rate:          26.20 [Kbytes/sec] received	# 传输速率

Connection Times (ms)	# 连接次数分钟
              min  mean[+/-sd] median   max
Connect:        0 2248 3833.1   1016   29088	# 连接
Processing:     0  251 1758.9      1   28692	# 处理
Waiting:        0   86 250.5      1    3441		# 等待
Total:          4 2499 4120.4   1206   29088	# # 总

Percentage of the requests served within a certain time (ms)	# 在一定时间内处理的请求的百分比(ms)
  50%   1206
  66%   1633
  75%   2421
  80%   3214
  90%   6514
  95%  12829
  98%  15847
  99%  25927
 100%  29088 (longest request)	# (最长的请求)

测试中查看连接数

明显看到连接数逐渐增多

image-20220628145902436

结束压测

因服务器压力过大,故压测过程中强制Ctrl+c结束

测试后查看连接数

结束压测后,连接数恢复正常

image-20220628152502449

总结

  • 支持的并发数取决于:
    • 服务器的配置:内存、CPU
    • 数据库的配置:内存、CPU
    • 服务器带宽大小
  • PS:若配置过低,进行与配置不匹配的压力测试,可能会导致服务器宕机!
posted @ 2022-08-03 18:23  秋风お亦冷  阅读(3204)  评论(0编辑  收藏  举报