Enhance NGINX performance

1. At the system level
1, adjust the number of open files at the same time
ulimit -n 20480
2, the maximum number of TCP connections (somaxconn)
echo 10000> /proc/sys/ net/core/somaxconn
3, TCP connection is immediately recycled and reused (recycle, reuse)
echo 1> /proc/sys/net/ipv4/tcp_tw_reuse
echo 1> /proc/sys/net /ipv4/tcp_tw_recycle
4. Don’t do TCP flood protection
echo 0> /proc/sys/net/ipv4/tcp_syncookies
You can also directly use the optimized configuration in /etc/sysctl.conf Join:
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
Use: sysctl -p to take effect
sysctl -p

Second, nginx level
Modify nginx configuration file, nginx.conf
add work _rlimit_nofile and worker_connections number, and disable keepalive_timeout.
worker_processes 1; #nginx The number of processes, it is recommended to specify the number of CPUs, generally a multiple of it
worker_rlimit_nofile 20000; #The maximum number of file descriptors opened by an nginx process, the theoretical value should be the maximum number of open files ( ulimit -n) is divided by the number of nginx processes, but nginx allocation requests are not so uniform, so it is best to keep the same value with ulimit -n
events {
use epoll;#Use epoll’s I/O model
worker_connections 20000;#The maximum number of connections allowed per process, theoretically the maximum number of connections per nginx server is worker_processes*worker_connections
multi_accept on;
}
http {
keepalive_timeout 0 ;
}

3. Test
Restart nginx
service nginx restart

Use ab stress test
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

Percenta ge 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>

如果不优化,运行时间超过半Hours! ! ! 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 requests Server Software: nginx/1.10.1Server Hostname: 192.168.1.198Server Port: 80 Document Path: /msg.phpDocument Length: 955 bytes Concurrency 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] received Connection Times (ms) min mean[+/-sd] median maxConnect: 0 20 236.9 1 3045 Processing: 4178 202109 29524.0 208780 220830Waiting: 1246 105285 59956.2 104752 216204Total: 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)

Leave a Comment

Your email address will not be published.