nginx高并发优化——轻松应对1万并发
来源:互联网 发布:关于网络交友的漫画 编辑:程序博客网 时间:2024/05/22 06:46
测试机器512M内存,swap分区407M ,centos6.5,nginx1.10.1,php5.6
默认的Linux内核参数和nginx参数考虑的是最通用场景,不适合高并发
所以优化思路主要包括两个层面:系统层面+nginx层面
一、系统层面
1、调整同时打开文件数量
ulimit -n 20480
2、TCP最大连接数(somaxconn)
echo 10000 > /proc/sys/net/core/somaxconn
3、TCP连接立即回收、回用(recycle、reuse)
echo 1 > /proc/sys/net/ipv4/tcp_tw_reuse
echo 1 > /proc/sys/net/ipv4/tcp_tw_recycle
4、不做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 生效
sysctl -p
二、nginx层面
修改nginx配置文件,nginx.conf
增加work_rlimit_nofile和worker_connections数量,并禁用keepalive_timeout。
worker_processes 1; #nginx 进程数,建议按照cpu 数目来指定,一般为它的倍数
worker_rlimit_nofile 20000; #一个nginx 进程打开的最多文件描述符数目,理论值应该是最多打开文件数(ulimit -n)与nginx 进程数相除,但是nginx 分配请求并不是那么均匀,所以最好与ulimit -n 的值保持一致
events {
use epoll;#使用epoll的I/O模型
worker_connections 20000;#每个进程允许的最多连接数, 理论上每台nginx 服务器的最大连接数为worker_processes*worker_connections
multi_accept on;
}
http {
keepalive_timeout 0;
}
三、测试
重启nginx
service nginx restart
使用ab压力测试
默认的Linux内核参数和nginx参数考虑的是最通用场景,不适合高并发
所以优化思路主要包括两个层面:系统层面+nginx层面
一、系统层面
1、调整同时打开文件数量
ulimit -n 20480
2、TCP最大连接数(somaxconn)
echo 10000 > /proc/sys/net/core/somaxconn
3、TCP连接立即回收、回用(recycle、reuse)
echo 1 > /proc/sys/net/ipv4/tcp_tw_reuse
echo 1 > /proc/sys/net/ipv4/tcp_tw_recycle
4、不做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 生效
sysctl -p
二、nginx层面
修改nginx配置文件,nginx.conf
增加work_rlimit_nofile和worker_connections数量,并禁用keepalive_timeout。
worker_processes 1; #nginx 进程数,建议按照cpu 数目来指定,一般为它的倍数
worker_rlimit_nofile 20000; #一个nginx 进程打开的最多文件描述符数目,理论值应该是最多打开文件数(ulimit -n)与nginx 进程数相除,但是nginx 分配请求并不是那么均匀,所以最好与ulimit -n 的值保持一致
events {
use epoll;#使用epoll的I/O模型
worker_connections 20000;#每个进程允许的最多连接数, 理论上每台nginx 服务器的最大连接数为worker_processes*worker_connections
multi_accept on;
}
http {
keepalive_timeout 0;
}
三、测试
重启nginx
service nginx restart
使用ab压力测试
D:\phpStudy\Apache\bin>ab -r -n 150000 -c 10000 http://192.168.1.198/msg.phpThis is ApacheBench, Version 2.3 <$Revision: 1706008 $>Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/Licensed to The Apache Software Foundation, http://www.apache.org/Benchmarking 192.168.1.198 (be patient)Completed 15000 requestsCompleted 30000 requestsCompleted 45000 requestsCompleted 60000 requestsCompleted 75000 requestsCompleted 90000 requestsCompleted 105000 requestsCompleted 120000 requestsCompleted 135000 requestsCompleted 150000 requestsFinished 150000 requestsServer Software: nginx/1.10.1Server Hostname: 192.168.1.198Server Port: 80Document Path: /msg.phpDocument Length: 955 bytesConcurrency Level: 10000Time taken for tests: 553.886 secondsComplete requests: 150000Failed requests: 74065 (Connect: 0, Receive: 0, Length: 74065, Exceptions: 0)Non-2xx responses: 74065Total transferred: 108769526 bytesHTML transferred: 85048014 bytesRequests per second: 270.81 [#/sec] (mean)Time per request: 36925.756 [ms] (mean)Time per request: 3.693 [ms] (mean, across all concurrent requests)Transfer rate: 191.77 [Kbytes/sec] receivedConnection Times (ms) min mean[+/-sd] median maxConnect: 0 1 19.0 1 3004Processing: 332 33370 25597.6 31689 92093Waiting: 163 32879 25640.0 31420 91598Total: 332 33370 25597.5 31689 92093Percentage of the requests served within a certain time (ms) 50% 31689 66% 60464 75% 60730 80% 60928 90% 61319 95% 61790 98% 62191 99% 62640 100% 92093 (longest request)D:\phpStudy\Apache\bin>
如果不优化,运行时间超过半个小时!!!
ab -r -n 150000 -c 10000 http://192.168.1.198/msg.phpThis is ApacheBench, Version 2.3 <$Revision: 1706008 $>Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/Licensed to The Apache Software Foundation, http://www.apache.org/Benchmarking 192.168.1.198 (be patient)Completed 15000 requestsCompleted 30000 requestsCompleted 45000 requestsCompleted 60000 requestsCompleted 75000 requestsCompleted 90000 requestsCompleted 105000 requestsCompleted 120000 requestsCompleted 135000 requestsCompleted 150000 requestsFinished 150000 requestsServer Software: nginx/1.10.1Server Hostname: 192.168.1.198Server Port: 80Document Path: /msg.phpDocument Length: 955 bytesConcurrency Level: 10000Time taken for tests: 3136.477 secondsComplete requests: 150000Failed requests: 0Total transferred: 168150000 bytesHTML transferred: 143250000 bytesRequests per second: 47.82 [#/sec] (mean)Time per request: 209098.485 [ms] (mean)Time per request: 20.910 [ms] (mean, across all concurrent requests)Transfer rate: 52.35 [Kbytes/sec] receivedConnection Times (ms) min mean[+/-sd] median maxConnect: 0 20 236.9 1 3045Processing: 4178 202109 29524.0 208780 220830Waiting: 1246 105285 59956.2 104752 216204Total: 4179 202129 29523.9 208806 220831Percentage of the requests served within a certain time (ms) 50% 208806 66% 210991 75% 211892 80% 212733 90% 213611 95% 214917 98% 217376 99% 217451 100% 220831 (longest request)
0 0
- nginx高并发优化——轻松应对1万并发
- Nginx优化详细,应对高并发
- nginx高并发优化
- 单机 nginx 应对高并发处理
- Nginx高并发负载的优化(1)
- Nginx实现高并发优化
- Nginx实现高并发优化
- 应对高并发笔记
- 应对高并发笔记
- 如何应对高并发?
- nginx优化 轻松突破十万并发
- 突破10万高并发的nginx性能优化经验(含内核参数优化)
- [nginx]nginx高并发优化实践
- Memcached笔记——(四)应对高并发攻击
- Memcached笔记——(四)应对高并发攻击
- Memcached笔记——(四)应对高并发攻击
- Memcached笔记——(二)应对高并发攻击
- Memcached笔记——(四)应对高并发攻击
- php同名方法
- pc站常用css默认样式(仅供参考)
- 如何快速在当前文件夹中打开cmd或者以管理员身份打开cmd
- ALE&IDoc& EDI(8)--Serialization
- 版本更新 但是不能判断版本
- nginx高并发优化——轻松应对1万并发
- Java学习总结(16.07.08)Object类的几个常用方法
- Falling Ants
- ALE&IDoc& EDI(9)--others
- atoi()
- 前端基础四 表格
- Win10默认开启数字小键盘的方法
- noip2013day2T3华容道
- c风格的字符串