Modify Linux kernel parameters Improve Nginx server concurrency performance

  
                  

When Nginx reaches a high number of concurrent Linux, the number of TCP TIME_WAIT sockets often reaches 20,000 or 30,000, so the server can easily be dragged to death. In fact, we can simply reduce the number of TIME_WAIT sockets on the Nginx server by modifying the Linux kernel parameters, which in turn improves the concurrent performance of the Nginx server. Vi /etc/sysctl.conf Add the following lines: net.ipv4.tcp_fin_timeout = 30 net.ipv4.tcp_keepalive_time = 1200

net.ipv4.tcp_syncookies = 1 net.ipv4.tcp_tw_reuse = 1 net.ipv4. Tcp_tw_recycle = 1 net.ipv4.ip_local_port_range = 1024 65000 net.ipv4.tcp_max_syn_backlog = 8192 net.ipv4.tcp_max_tw_buckets = 5000 Brief Description: net.ipv4.tcp_syncookies = 1 means to enable SYN Cookies. When the SYN wait queue overflow occurs, cookies are enabled for processing, which can prevent a small number of SYN attacks. The default is 0, which means closed; net.ipv4.tcp_tw_reuse = 1 means to enable re-use. Allows TIME-WAIT sockets to be reused for new TCP connections. The default is 0, which means closed. net.ipv4.tcp_tw_recycle = 1 means to enable fast recovery of TIME-WAIT sockets in TCP connection. The default is 0, which means closed. Net.ipv4.tcp_fin_timeout = 30 means that if the socket is requested to be closed by the local end, this parameter determines when it stays in the FIN-WAIT-2 state. Net.ipv4.tcp_keepalive_time = 1200 Indicates how often TCP sends keepalive messages when keepalive is enabled. The default is 2 hours and changed to 20 minutes. Net.ipv4.ip_local_port_range = 1024 65000 Indicates the range of ports used for outgoing connections. By default it is very small: 32768 to 61000, changed to 1024 to 65000. Net.ipv4.tcp_max_syn_backlog = 8192 Indicates the length of the SYN queue. The default is 1024. The extended queue length is 8192, which can accommodate more network connections waiting to be connected. Net.ipv4.tcp_max_tw_buckets = 5000 indicates that the system keeps the maximum number of TIME_WAIT sockets at the same time. If this number is exceeded, the TIME_WAIT socket will be cleared immediately and a warning message will be printed. The default is 180000, which is changed to 5000. For Apache, Nginx and other servers, the parameters of the last few lines can reduce the number of TIME_WAIT sockets, but for Squid, the effect is not great. This parameter controls the maximum number of TIME_WAIT sockets, preventing the Squid server from being dragged by a large number of TIME_WAIT sockets. Echo “================================================================================================== =======” #Change the linux kernel parameters, the command takes effect immediately! /sbin/sysctl -p Nginx Optimization Use FastCGI Cache fastcgi_cache TEST to enable the FastCGI cache and give it a name. Personally feel that opening the cache is very useful, can effectively reduce the CPU load, and prevent 502 errors. Fastcgi_cache_path /usr/local/nginx/fastcgi_cache levels=1:2 keys_zone=TEST:10m inactive=5m; This directive specifies a path for the FastCGI cache, directory structure level, keyword area storage time and inactive deletion time

Other Notes

Nginx was developed by Igor Sysoev for the second-most visited Rambler.ru site in Russia, which has been running on the site for more than two and a half years. Igor publishes the source code as a class BSD license.

In the case of high concurrent connections, Nginx is a good alternative to the Apache server. Nginx can also be used as a 7-layer load balancing server. According to my test results, Nginx 0.6.31 + PHP 5.2.6 (FastCGI) can withstand more than 30,000 concurrent connections, which is equivalent to 10 times that of Apache in the same environment.

According to my experience, 4GB memory server + Apache (prefork mode) can only handle 3000 concurrent connections, because they will occupy more than 3GB of memory, and have to reserve 1GB of memory for the system. I used to have two Apache servers because the MaxClients set in the configuration file was 4000. When the number of Apache concurrent connections reached 3800, the server memory and Swap space were full and crashed.

And this Nginx 0.6.31 + PHP 5.2.6 (FastCGI) server under 30,000 concurrent connections, the 10 Nginx processes opened consume 150M memory (15M * 10 = 150M), open 64 The php-cgi process consumes 1280M of memory (20M*64=1280M), plus the memory consumed by the system itself, which consumes less than 2GB of memory. If the server memory is small, you can only open 25 php-cgi processes, so the total memory consumed by php-cgi is only 500M.

Copyright © Windows knowledge All Rights Reserved