nginx single concurrent optimization 1w

table of Contents

ab tool
overall optimization ideas
specific optimization ideas
to write the script completes and optimize the allocation of
performance statistics tool
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)

Overall optimization ideas

  • Allowing more socket connection is established
  • Allow more open files

Specific optimization ideas

1, socket level

  • System level
  • Not against flood
  • The maximum number of connections somaxconn
  • Tcp connection speed up recovery recyle
  • Empty tcp connection is allowed to reuse recycled
  • nginx
  • Each child process allows open connections (work_connections)
  • Accelerate http connection, quick closing, keepalive_timeout 0

2, file level

  • nginx level
  • Worker_rlimit_nofile child process allows the file to open
  • System level
  • ulimit -n 10000 (a relatively large value is provided, allowing open files)

Specific configuring

1, system configuration

View the system to allow the maximum number of open connections

more /proc/sys/net/core/somaxconn
echo 50000 > /proc/sys/net/core/somaxconn

Open the quick-connect system recovery

cat /proc/sys/net/ipv4/tcp_tw_recycle
echo 1 > /proc/sys/net/ipv4/tcp_tw_recycle

Empty open tcp connection allows recycling

cat /proc/sys/net/ipv4/tcp_tw_reuse
echo 1 > /proc/sys/net/ipv4/tcp_tw_reuse

Not against flood

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 - OFF
  • http 1.1 to prevent the frequent shake hands, nginx set keepalive_timeout argument is also keep the option to connect long to reduce the number of tcp connections in high concurrency website, keepalived is a need for serious attention after a request is completed, need not be set to 0 do stay connected to improve concurrency
worker_rlimit_nofile 10000;
events {
    worker_connections 10000;
}
keepalive_timeout 0;

Scripting complete concurrent optimal allocation

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 the problem:

  • dmesg observation system
  • nginx to observe the error.log

dmesg|tail

Performance statistics tool

Installation statistics module http_stub_status_module, easy to observe the state of nginx

  • 1. Before entering the nginx installation package directory to find whether there are statistics module can be installed

Delete all selected packages 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 ;;

See if there 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 tool
    ./configure --prefix=/usr/local/nginx/ --with-http_stub_status_module
  • 3. Check whether the module is 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

Description module has been installed successfully

  • 4. Add the following configuration items inside the nginx server configuration tool opening performance statistics
    location /nginx_status {
        stub_status on;
        access_log off;
        allow 127.0.0.1;
    }
  • 5. View, refresh the page access to view the status http://192.168.0.217/nginx_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

  • Test machine also need to configure a large digital ulimit -n
  • Test machine needs to be configured echo 50000> / proc / sys / net / core / somaxconn

to sum up

In doing server deployment time, we must first understand the limits of server configuration and server can handle, when the first test start nginx can only run static html page test begins, do not even cache database, not logic treatment, do not do cdn to test the maximum capacity of nginx. We all know that if join php, connected to a database, the database caches do cdn pictures do have an impact concurrency, after the thing that is the same debug them individually for maximum performance press server, targeted optimization is correct.

Guess you like

Origin www.cnblogs.com/lisqiong/p/11403928.html