测试机为腾讯云服务器1核1G内存、swap分区2G,禁用除SSH外的所有服务,只保留nginx。 优化思路主要由两个层面组成。 系统级nginx级。
一.系统层面
1、调整同时打开的文件数量
多功能一体机20480
2、TCP最大连接数(somaxconn ) )。
echo 10000/proc/sys/net/core/somaxconn
3、TCP连接即时回收、回收(recycle、reuse ) ) ) ) ) ) ) ) ) ) ) ) ) ) ) )。
echo1/proc/sys/net/IP v4/TCP _ tw _ reuse
echo1/proc/sys/net/IP v4/TCP _ tw _ recycle
4、TCP不做防洪措施
echo0/proc/sys/net/IP v4/TCP _ syn cookies
也可以按原样使用优化配置将其添加到/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.IP v4.TCP _ rmem=409640961677216
net.IP v4.TCP _ wmem=4096409616777216
net.IP v4.TCP _ mem=78643220971523145728
net.IP v4.TCP _ max _ syn _ backlog=16384
net.core.net dev _ max _ backlog=20000
net.ipv4.tcp_fin_timeout=15
net.IP v4.TCP _ max _ syn _ backlog=16384
net.ipv4.tcp_tw_reuse=1
net.ipv4.tcp_tw_recycle=1
net.IP v4.TCP _ max _ orphans=131072
net.ipv4.tcp_syncookies=0
使用: sysctl -p已启用
sysctl -p
二. nginx水平
修改nginx配置文件,nginx.conf
增加work_rlimit_nofile和worker_connections的数量以禁用keepalive_timeout。
工作器_进程1;
worker_rlimit_nofile 20000;
events {
use epoll;
worker_connections 20000;
multi_accept on;
}
http {
keepalive_timeout 0;
}
重新启动nginx
/usr/local/nginx/sbin/nginx-s reload
使用ab压力测试
a B-c10000-n 1500003358127.0.0.1/index.html
测试结果:
This is ApacheBench,Version 2.3
Copyright 1996 Adam Twiss,Zeus Technology Ltd,http://www.zeustech.net/
licensedtotheapachesoftwarefoundation,http://www.apache.org/
benchmarking 127.0.0.1 (be patient ) ) )。
已完成15000请求
已完成30000请求
已完成45000请求
已完成60000请求
已完成75000请求
已完成90000请求
已完成105000请求
已完成120000请求
已完成135000请求
已完成150000请求
finished 150000请求
Server Software: nginx/1.8.0
Server Hostname: 127.0.0.1
服务器端口: 80
文档路径:/index.html
文档长度: 612字节
Concurrency Level: 10000
timetakenfortests 336019.185 seconds
Complete requests: 150000
故障请求: 0
Write errors: 0
总传输3360131180388字节
html transferred 336095121324 bytes
requestspersecond :7818.53 [ #/sec ] (mean )
timeperrequest :1279.013 [ ms ] (mean )
timeperrequest :128 [ ms ] ) ) )。
传输速率:6677.33 [ kbytes/sec ] received
连接时间(ms ) )。
min mean[ /-sd] median max
Connect: 0 650 547.9 522 7427
处理:212519157.4496958
Waiting: 0 404 139.7 380 845
Total: 259 1168 572.1 1066 7961
percentageoftherequestsservedwithinacertaintime (ms )。
50% 1066
66% 1236
75% 1295
80% 1320
90% 1855
95% 2079
98% 2264
99% 2318
100 y 61 (长请求) )。