Concurrent optimization of nginx single machine for 1w

Keywords: Web Server Nginx Apache socket Database

Concurrent optimization of nginx single machine for 1w
Catalog
ab tool
Integral Optimizing Thought
Specific optimization ideas
Write scripts to optimize concurrent configuration
Performance statistics tools
tips
summary

ab tool
ab -c 10000 -n 200000 http://localhost/index.html

[root@study02 ~]# ab -c 10000 -n 100000 http://192.168.0.217/index.html
This is ApacheBench, Version 2.3 <$Revision: 1430300 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking 192.168.0.217 (be patient)
socket: Too many open files (24)
Integral Optimizing Thought
Allow more socket connections
Allow more files to be opened
Specific optimization ideas
1. socket level
System level
Do not resist floods
Maximum number of connections somaxconn
Speed up tcp connection recovery recyle
Is empty tcp connection allowed to recycle reuse
nginx
Each subprocess allows open connections
Speed up http connection, close quickly, keep alive_timeout 0
2. Document level
nginx level
Subprocesses allow open files worker_rlimit_nofile
System level
Ulimit-n 10000 (set a larger value to allow the number of open files)
Specific configuration operations
1. System Configuration
View the maximum number of connections allowed to open by the system

more /proc/sys/net/core/somaxconn
echo 50000 > /proc/sys/net/core/somaxconn
Open System Fast Connection Recycling

cat /proc/sys/net/ipv4/tcp_tw_recycle
echo 1 > /proc/sys/net/ipv4/tcp_tw_recycle
Opening an empty tcp connection allows recycling

cat /proc/sys/net/ipv4/tcp_tw_reuse
echo 1 > /proc/sys/net/ipv4/tcp_tw_reuse
Do not resist floods

cat /proc/sys/net/ipv4/tcp_syncookies
echo 0 > /proc/sys/net/ipv4/tcp_syncookies
2. nginx configuration
http 1.0 client server request-response-disconnect
http 1.1 prevents frequent handshakes. nginx setting the keepalive_timeout parameter is how long to keep the connection after a request has been completed to reduce the number of tcp connections. In highly concurrent websites, keeping alived is an option that requires serious attention. It needs to be set to zero without maintaining the connection to improve concurrency.
worker_rlimit_nofile 10000;
events {

worker_connections 10000;

}
keepalive_timeout 0;
Write scripts to optimize concurrent configuration
echo 50000 > /proc/sys/net/core/somaxconn
echo 1 > /proc/sys/net/ipv4/tcp_tw_recycle
echo 1 > /proc/sys/net/ipv4/tcp_tw_reuse
echo 0 > /proc/sys/net/ipv4/tcp_syncookies
Eliminate problems:

dmesg observation of the system
Observe the error.log of nginx
dmesg|tail

Performance statistics tools
Install the statistics module http_stub_status_module to observe the state of nginx

1. Enter the installation package directory of nginx before you install it, and find out if there are any statistical modules that can be installed.
Delete all packages that can be installed

root@STUDY3 nginx-1.14.2]# cat auto/options |grep YES
HTTP=YES
HTTP_CACHE=YES
HTTP_CHARSET=YES
HTTP_GZIP=YES
HTTP_SSI=YES
HTTP_ACCESS=YES
HTTP_AUTH_BASIC=YES
HTTP_MIRROR=YES
HTTP_USERID=YES
HTTP_AUTOINDEX=YES
HTTP_GEO=YES
HTTP_MAP=YES
HTTP_SPLIT_CLIENTS=YES
HTTP_REFERER=YES
HTTP_REWRITE=YES
HTTP_PROXY=YES
HTTP_FASTCGI=YES
HTTP_UWSGI=YES
HTTP_SCGI=YES
HTTP_GRPC=YES
HTTP_MEMCACHED=YES
HTTP_LIMIT_CONN=YES
HTTP_LIMIT_REQ=YES
HTTP_EMPTY_GIF=YES
HTTP_BROWSER=YES
HTTP_UPSTREAM_HASH=YES
HTTP_UPSTREAM_IP_HASH=YES
HTTP_UPSTREAM_LEAST_CONN=YES
HTTP_UPSTREAM_KEEPALIVE=YES
HTTP_UPSTREAM_ZONE=YES
MAIL_POP3=YES
MAIL_IMAP=YES
MAIL_SMTP=YES
STREAM_LIMIT_CONN=YES
STREAM_ACCESS=YES
STREAM_GEO=YES
STREAM_MAP=YES
STREAM_SPLIT_CLIENTS=YES
STREAM_RETURN=YES
STREAM_UPSTREAM_HASH=YES
STREAM_UPSTREAM_LEAST_CONN=YES
STREAM_UPSTREAM_ZONE=YES
        --with-select_module)            EVENT_SELECT=YES           ;;
        --with-poll_module)              EVENT_POLL=YES             ;;
        --with-threads)                  USE_THREADS=YES            ;;
        --with-file-aio)                 NGX_FILE_AIO=YES           ;;
        --with-http_ssl_module)          HTTP_SSL=YES               ;;
        --with-http_v2_module)           HTTP_V2=YES                ;;
        --with-http_realip_module)       HTTP_REALIP=YES            ;;
        --with-http_addition_module)     HTTP_ADDITION=YES          ;;
        --with-http_xslt_module)         HTTP_XSLT=YES              ;;
        --with-http_image_filter_module) HTTP_IMAGE_FILTER=YES      ;;
        --with-http_geoip_module)        HTTP_GEOIP=YES             ;;
        --with-http_sub_module)          HTTP_SUB=YES               ;;
        --with-http_dav_module)          HTTP_DAV=YES               ;;
        --with-http_flv_module)          HTTP_FLV=YES               ;;
        --with-http_mp4_module)          HTTP_MP4=YES               ;;
        --with-http_gunzip_module)       HTTP_GUNZIP=YES            ;;
        --with-http_gzip_static_module)  HTTP_GZIP_STATIC=YES       ;;
        --with-http_auth_request_module) HTTP_AUTH_REQUEST=YES      ;;
        --with-http_random_index_module) HTTP_RANDOM_INDEX=YES      ;;
        --with-http_secure_link_module)  HTTP_SECURE_LINK=YES       ;;
        --with-http_degradation_module)  HTTP_DEGRADATION=YES       ;;
        --with-http_slice_module)        HTTP_SLICE=YES             ;;
        --with-http_perl_module)         HTTP_PERL=YES              ;;
        --with-http_stub_status_module)  HTTP_STUB_STATUS=YES       ;;
        --with-mail)                     MAIL=YES                   ;;
        --with-mail_ssl_module)          MAIL_SSL=YES               ;;
            MAIL=YES
            MAIL_SSL=YES
        --with-stream)                   STREAM=YES                 ;;
        --with-stream_ssl_module)        STREAM_SSL=YES             ;;
        --with-stream_realip_module)     STREAM_REALIP=YES          ;;
        --with-stream_geoip_module)      STREAM_GEOIP=YES           ;;
                                         STREAM_SSL_PREREAD=YES     ;;
        --with-google_perftools_module)  NGX_GOOGLE_PERFTOOLS=YES   ;;
        --with-cpp_test_module)          NGX_CPP_TEST=YES           ;;
        --with-compat)                   NGX_COMPAT=YES             ;;
        --with-debug)                    NGX_DEBUG=YES              ;;
        --with-pcre)                     USE_PCRE=YES               ;;
        --with-pcre-jit)                 PCRE_JIT=YES               ;;
        --with-libatomic)                NGX_LIBATOMIC=YES          ;;
        --test-build-devpoll)            NGX_TEST_BUILD_DEVPOLL=YES ;;
        --test-build-eventport)          NGX_TEST_BUILD_EVENTPORT=YES ;;
        --test-build-epoll)              NGX_TEST_BUILD_EPOLL=YES   ;;
        --test-build-solaris-sendfilev)  NGX_TEST_BUILD_SOLARIS_SENDFILEV=YES ;;

Check to see if there is an http_stub_status_module module

[root@STUDY3 nginx-1.14.2]# cat auto/options |grep YES|grep 'http_stub_status_module'

    --with-http_stub_status_module)  HTTP_STUB_STATUS=YES       ;;

make && make install
2. Install nginx performance statistics tools

./configure --prefix=/usr/local/nginx/ --with-http_stub_status_module

3. Check whether the module has been successfully installed

[root@STUDY3 nginx-1.14.2]# /usr/local/nginx/sbin/nginx -V
nginx version: nginx/1.14.2
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-36) (GCC)
configure arguments: --prefix=/usr/local/nginx/ --with-http_stub_status_module

Explain that the module has been installed successfully

4. Add the following configuration to the server configuration of nginx to turn on the performance statistics tool

location /nginx_status {
    stub_status on;
    access_log off;
    allow 127.0.0.1;
}

5. View and refresh the access page http://192.168.0.217/nginx_status View status
Active connections: 2020
server accepts handled requests
897553 897553 442986
Reading: 0 Writing: 1 Waiting: 2019
6.ab test

[root@study02 ~]# ab -c 10000 -n 200000 http://192.168.0.217/index.html
This is ApacheBench, Version 2.3 <$Revision: 1430300 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking 192.168.0.217 (be patient)
Completed 20000 requests
Completed 40000 requests
Completed 60000 requests
Completed 80000 requests
Completed 100000 requests
Completed 120000 requests
Completed 140000 requests
Completed 160000 requests
Completed 180000 requests
Completed 200000 requests
Finished 200000 requests
Server Software:        nginx/1.14.2
Server Hostname:        192.168.0.217
Server Port:            80

Document Path:          /index.html
Document Length:        612 bytes

Concurrency Level:      10000
Time taken for tests:   13.268 seconds
Complete requests:      200000
Failed requests:        345710
   (Connect: 0, Receive: 0, Length: 174517, Exceptions: 171193)
Write errors:           0
Non-2xx responses:      21
Total transferred:      24276700 bytes
HTML transferred:       17581305 bytes
Requests per second:    15074.19 [#/sec](mean)
Time per request:       663.386 [ms](mean)
Time per request:       0.066 [ms](mean, across all concurrent requests)
Transfer rate:          1786.87 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0  322  85.2    324    1238
Processing:    69  322 108.4    315     692
Waiting:        0   38 103.0      0     503
Total:        373  643  97.8    625    1651

Percentage of the requests served within a certain time (ms)
  50%    625
  66%    640
  75%    643
  80%    646
  90%    739
  95%    883
  98%    976
  99%   1015
 100%   1651 (longest request)

tips
Testing machines also need to configure ulimit-n with a larger number
Test machines need to be configured with echo 50000 >/proc/sys/net/core/somaxconn
summary
When deploying servers, we must first understand the configurations of servers and the limits that servers can handle. When testing, we can start with static html page testing of nginx, without database caching, without logical processing, without cdn to test the maximum capability of nginx. You know that if you add php, connect to the database, do the database caching and do the image cdn concurrency will have an impact, then things will also be the same after debugging the maximum performance of the squeezing server one by one, targeted optimization is correct.
Original address https://www.cnblogs.com/lisqiong/p/11403928.html

Posted by rachae1 on Sat, 24 Aug 2019 02:40:41 -0700