nginx应对1万高并发--转载

测试机器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压力测试
  1. D:\phpStudy\Apache\bin>ab -r -n 150000  -c 10000 http://192.168.1.198/msg.php  
  2. This is ApacheBench, Version 2.3 <Revision</span><span>:&nbsp;1706008&nbsp; Revision</span><span>:&nbsp;1706008&nbsp; >  
  3. Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/  
  4. Licensed to The Apache Software Foundation, http://www.apache.org/  
  5.   
  6. Benchmarking 192.168.1.198 (be patient)  
  7. Completed 15000 requests  
  8. Completed 30000 requests  
  9. Completed 45000 requests  
  10. Completed 60000 requests  
  11. Completed 75000 requests  
  12. Completed 90000 requests  
  13. Completed 105000 requests  
  14. Completed 120000 requests  
  15. Completed 135000 requests  
  16. Completed 150000 requests  
  17. Finished 150000 requests  
  18.   
  19.   
  20. Server Software:        nginx/1.10.1  
  21. Server Hostname:        192.168.1.198  
  22. Server Port:            80  
  23.   
  24. Document Path:          /msg.php  
  25. Document Length:        955 bytes  
  26.   
  27. Concurrency Level:      10000  
  28. Time taken for tests:   553.886 seconds  
  29. Complete requests:      150000  
  30. Failed requests:        74065  
  31.    (Connect: 0, Receive: 0, Length: 74065, Exceptions: 0)  
  32. Non-2xx responses:      74065  
  33. Total transferred:      108769526 bytes  
  34. HTML transferred:       85048014 bytes  
  35. Requests per second:    270.81 [#/sec] (mean)  
  36. Time per request:       36925.756 [ms] (mean)  
  37. Time per request:       3.693 [ms] (mean, across all concurrent requests)  
  38. Transfer rate:          191.77 [Kbytes/sec] received  
  39.   
  40. Connection Times (ms)  
  41.               min  mean[+/-sd] median   max  
  42. Connect:        0    1  19.0      1    3004  
  43. Processing:   332 33370 25597.6  31689   92093  
  44. Waiting:      163 32879 25640.0  31420   91598  
  45. Total:        332 33370 25597.5  31689   92093  
  46.   
  47. Percentage of the requests served within a certain time (ms)  
  48.   50%  31689  
  49.   66%  60464  
  50.   75%  60730  
  51.   80%  60928  
  52.   90%  61319  
  53.   95%  61790  
  54.   98%  62191  
  55.   99%  62640  
  56.  100%  92093 (longest request)  
  57.   
  58. D:\phpStudy\Apache\bin>  
D:\phpStudy\Apache\bin>ab -r -n 150000  -c 10000 http://192.168.1.198/msg.php
This 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 requests
Completed 30000 requests
Completed 45000 requests
Completed 60000 requests
Completed 75000 requests
Completed 90000 requests
Completed 105000 requests
Completed 120000 requests
Completed 135000 requests
Completed 150000 requests
Finished 150000 requests


Server Software:        nginx/1.10.1
Server Hostname:        192.168.1.198
Server Port:            80

Document Path:          /msg.php
Document Length:        955 bytes

Concurrency Level:      10000
Time taken for tests:   553.886 seconds
Complete requests:      150000
Failed requests:        74065
   (Connect: 0, Receive: 0, Length: 74065, Exceptions: 0)
Non-2xx responses:      74065
Total transferred:      108769526 bytes
HTML transferred:       85048014 bytes
Requests 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] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    1  19.0      1    3004
Processing:   332 33370 25597.6  31689   92093
Waiting:      163 32879 25640.0  31420   91598
Total:        332 33370 25597.5  31689   92093

Percentage 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>

如果不优化,运行时间超过半个小时!!!
  1. ab -r -n 150000  -c 10000 http://192.168.1.198/msg.php  
  2. This is ApacheBench, Version 2.3 <Revision</span><span>:&nbsp;1706008&nbsp; Revision</span><span>:&nbsp;1706008&nbsp; >  
  3. Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/  
  4. Licensed to The Apache Software Foundation, http://www.apache.org/  
  5.   
  6. Benchmarking 192.168.1.198 (be patient)  
  7. Completed 15000 requests  
  8. Completed 30000 requests  
  9. Completed 45000 requests  
  10. Completed 60000 requests  
  11. Completed 75000 requests  
  12. Completed 90000 requests  
  13. Completed 105000 requests  
  14. Completed 120000 requests  
  15. Completed 135000 requests  
  16. Completed 150000 requests  
  17. Finished 150000 requests  
  18.   
  19.   
  20. Server Software:        nginx/1.10.1  
  21. Server Hostname:        192.168.1.198  
  22. Server Port:            80  
  23.   
  24. Document Path:          /msg.php  
  25. Document Length:        955 bytes  
  26.   
  27. Concurrency Level:      10000  
  28. Time taken for tests:   3136.477 seconds  
  29. Complete requests:      150000  
  30. Failed requests:        0  
  31. Total transferred:      168150000 bytes  
  32. HTML transferred:       143250000 bytes  
  33. Requests per second:    47.82 [#/sec] (mean)  
  34. Time per request:       209098.485 [ms] (mean)  
  35. Time per request:       20.910 [ms] (mean, across all concurrent requests)  
  36. Transfer rate:          52.35 [Kbytes/sec] received  
  37.   
  38. Connection Times (ms)  
  39.               min  mean[+/-sd] median   max  
  40. Connect:        0   20 236.9      1    3045  
  41. Processing:  4178 202109 29524.0 208780  220830  
  42. Waiting:     1246 105285 59956.2 104752  216204  
  43. Total:       4179 202129 29523.9 208806  220831  
  44.   
  45. Percentage of the requests served within a certain time (ms)  
  46.   50%  208806  
  47.   66%  210991  
  48.   75%  211892  
  49.   80%  212733  
  50.   90%  213611  
  51.   95%  214917  
  52.   98%  217376  
  53.   99%  217451  
  54.  100%  220831 (longest request)  
ab -r -n 150000  -c 10000 http://192.168.1.198/msg.php
This 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 requests
Completed 30000 requests
Completed 45000 requests
Completed 60000 requests
Completed 75000 requests
Completed 90000 requests
Completed 105000 requests
Completed 120000 requests
Completed 135000 requests
Completed 150000 requests
Finished 150000 requests


Server Software:        nginx/1.10.1
Server Hostname:        192.168.1.198
Server Port:            80

Document Path:          /msg.php
Document Length:        955 bytes

Concurrency Level:      10000
Time taken for tests:   3136.477 seconds
Complete requests:      150000
Failed requests:        0
Total transferred:      168150000 bytes
HTML transferred:       143250000 bytes
Requests 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] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0   20 236.9      1    3045
Processing:  4178 202109 29524.0 208780  220830
Waiting:     1246 105285 59956.2 104752  216204
Total:       4179 202129 29523.9 208806  220831

Percentage 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)




        </div>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值