服务器优化简介
ngin优化
修改nginx.conf文件
#cpu核数
worker_processes 2;
events {
worker_connections 65535;
use epoll;
}
http {
server_names_hash_bucket_size 128;
client_header_buffer_size 2k;
large_client_header_buffers 4 4k;
client_max_body_size 8m;
#...其他配置
}
tomcat优化
一、修改server.xml
<Connector port="8080" protocol="org.apache.coyote.http11.Http11NioProtocol"
connectionTimeout="20000" maxThreads="2000" minSpareThreads="25"
maxSpareThreads="250" acceptCount="500" URIEncoding="UTF-8" redirectPort="8443"
useBodyEncodingForURI="true"
/>
二、修改bin/catalina.sh
JAVA_OPTS="-server -Xms512m -Xmx512m -XX:PermSize=128m -XX:MaxPermSize=512m"
系统层面
1,调整同时打开文件数量
ulimit -n 20480
2,TCP最大连接数(somaxconn)
echo 10000>/ proc / sys / net / core / somaxconn
3,TCP连接立即回收,回用回收,重用)
回声1>/ proc / sys / net / ipv4 / tcp\_tw\_reuse
回声1>/ proc / sys / net / ipv4 / tcp\_tw\_recycle
4,不做TCP洪水抵御
回声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 {
使用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.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>
如果不优化,运行时间超过半个小时
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)