[Reserved] lightweight HTTP server Nginx (Nginx performance optimization techniques)

First, compile and install process optimization

1. Reduce the compiled file size Nginx

When compiling Nginx, to be the default debug mode, the debug mode is inserted in a lot of information and tracking ASSERT like, after completion of the translation, a Nginx have several megabytes. Nginx cancel the debug mode before compilation after compilation Nginx only a few hundred kilobytes, it can be before compiling, modify the source code, cancel debug mode, as follows:
after the Nginx source code files are extracted, find the source directory the auto / cc / gcc files, where you can find the following lines:

1 # debug  
2 CFLAGS=”$CFLAGS -g”  

Comment out or delete the two lines, you can cancel the debug mode.

2. Specify the type of CPU compiler optimized for specific CPU

When compiling the Nginx, GCC compiler default parameter "-O", to optimize GCC compiler can use the following two parameters:
--with-opt-CC = '- O3'
--with the CPU-CPU-opt = # compiled for a specific CPU, valid values include: pentium, pentiumpro, pentium3, pentium4 , athlon, opteron, amd64, sparc32, sparc64, ppc64
to determine the CPU type, the following command:

1 [root@localhost home]#cat /proc/cpuinfo | grep "model name"

Second, the use of TCMalloc optimize performance Nginx

TCMalloc full name Thread-Caching Malloc, is a member of the open source tool developed by Google "google-perftools" in. Compared with the standard malloc glibc library, TCMalloc library on the efficiency and speed of memory allocation is much higher, which improves server performance under high concurrency to a large extent, thereby reducing the load on the system. The following outlines how to add TCMalloc library support for Nginx.
To install TCMalloc libraries need to install libunwind (32-bit operating system without installing) and google-perftools two packages, library, libunwind, provides the basic function calls and function call chain based on 64-bit register function CPU and operating system programs. The following describes specific operation using the Nginx TCMalloc Optimization:

1. Installation libunwind library
from http://download.savannah.gnu.org/releases/libunwind appropriate download libunwind version downloaded here is libunwind-0.99-alpha.tar.gz, the installation process is as follows:
 

1 [root@localhost home]#tar zxvf libunwind-0.99-alpha.tar.gz  
2 [root@localhost home]# cd libunwind-0.99-alpha/  
3 [root@localhost libunwind-0.99-alpha]#CFLAGS=-fPIC ./configure  
4 [root@localhost libunwind-0.99-alpha]#make CFLAGS=-fPIC  
5 [root@localhost libunwind-0.99-alpha]#make CFLAGS=-fPIC install  

2. Installation google-perftools

From http://google-perftools.googlecode.com appropriate download google-perftools version downloaded here is google-perftools-1.8.tar.gz, the installation process is as follows:

1 [root@localhost home]#tar zxvf google-perftools-1.8.tar.gz  
2 [root@localhost home]#cd google-perftools-1.8/  
3 [root@localhost google-perftools-1.8]# ./configure  
4 [root@localhost google-perftools-1.8]#make && make install  
5 [root@localhost google-perftools-1.8]#echo "/usr/local/lib" > /etc/ld.so.conf.d/usr_local_lib.conf  
6 [root@localhost google-perftools-1.8]# ldconfig  

So far, google-perftools installation is complete.

3. recompile Nginx
order to make Nginx support google-perftools, need to be added during the installation process "-with-google_perftools_module" option to recompile Nginx, installation code is as follows:
 

1 [root@localhostnginx-0.7.65]#./configure \  
2 >--with-google_perftools_module --with-http_stub_status_module  --prefix=/opt/nginx  
3 [root@localhost nginx-0.7.65]#make  
4 [root@localhost nginx-0.7.65]#make install  

Here Nginx installation is complete.

4. Add google-perftools directory thread
creates a thread directory, where the file in / tmp / tcmalloc, the operation is as follows:
 

1 [root@localhost home]#mkdir /tmp/tcmalloc  
2 [root@localhost home]#chmod 0777 /tmp/tcmalloc  

5. Modify the main configuration file Nginx

Nginx.conf modified file, add the following code below this line pid:
 

1 #pid        logs/nginx.pid;  
2 google_perftools_profiles /tmp/tcmalloc;  

Subsequently, to restart the Nginx, google-perftools completed loading.

6. Verify the operating state
in order to verify google-perftools has normally loaded, see the following command:
 

1 [root@ localhost home]# lsof -n | grep tcmalloc  
2 nginx      2395 nobody   9w  REG    8,8       0    1599440 /tmp/tcmalloc.2395  
3 nginx      2396 nobody   11w REG   8,8       0    1599443 /tmp/tcmalloc.2396  
4 nginx      2397 nobody   13w REG  8,8        0    1599441  /tmp/tcmalloc.2397  
5 nginx     2398 nobody    15w REG  8,8     0    1599442 /tmp/tcmalloc.2398  

Since Nginx configuration file, the value set worker_processes 4, thus opening Nginx four threads, each have a row. Each thread behind the digital file value is the start of Nginx PID values.

So far, Nginx's use TCMalloc optimize operation is complete.

Three, Nginx kernel parameter optimization

Optimize the kernel parameters, mainly the system kernel parameters in Linux systems for Nginx applications perform optimization, common optimization parameter values are as follows.
The following example is given for an optimized reference:

 1 net.ipv4.tcp_max_tw_buckets = 65535 
 2 net.ipv4.ip_local_port_range = 1024 65000  
 3 net.ipv4.tcp_tw_recycle = 1 
 4 net.ipv4.tcp_tw_reuse = 1 
 5 net.ipv4.tcp_syncookies = 1 
 6 net.core.somaxconn = 262144 
 7 net.core.netdev_max_backlog = 262144 
 8 net.ipv4.tcp_max_orphans = 262144 
 9 net.ipv4.tcp_max_syn_backlog = 262144 
10 net.ipv4.tcp_synack_retries = 1 
11 net.ipv4.tcp_syn_retries = 1 
12 net.ipv4.tcp_fin_timeout = 1 
13 net.ipv4.tcp_keepalive_time = 30 

The added value of the kernel parameter above /etc/sysctl.conf file, and then execute the command so as to take effect as follows:

[root @ localhost home] # / sbin / sysctl -p
The following is an example of the meaning of the options are introduced:

The number of parameters used to set timewait, the default is 180,000, this is set to 65535.
 net.ipv4.tcp_max_tw_buckets

Option allows the system to set the open port range.
 net.ipv4.ip_local_port_range

Option is used to enable timewait rapid recovery.
 net.ipv4.tcp_tw_recycle

Options for setting open reuse TIME-WAIT sockets allow re-used for new TCP connections.
 net.ipv4.tcp_tw_reuse

Options for setting open SYN Cookies, when the SYN queue overflow occurs, enable cookies for processing.
 net.ipv4.tcp_syncookies

Option 128 is the default value, this parameter is used to adjust the number of connected systems simultaneously tcp initiated, highly concurrent request, the default value of the link may result in a timeout or retransmission, and therefore, requires a combination of the number of concurrent requests to adjust the value.
 net.core.somaxconn

Option represents a fast rate when each network interface to receive packets of these packet rate than the core processing allows the maximum number of data packets transmitted to the queue.
 net.core.netdev_max_backlog

Option is used to set the system up to the number of TCP sockets are not linked to any user file handle. If this number is exceeded, the orphaned connection is reset immediately and print out a warning message. This restriction only to prevent simple DoS attacks. This restriction does not overly rely on artificially reduce this value even more the situation is to increase this value.
 net.ipv4.tcp_max_orphans

Options for the record that the client has not yet received confirmation of the maximum connection information request. For 128MB of system memory, the default value for this parameter is 1024, the small memory system is 128.
 net.ipv4.tcp_max_syn_backlog

The values of the parameters determines the number of cores SYN + ACK packet transmission is aborted before connection.
 net.ipv4.tcp_synack_retries

Option means giving up the kernel build number of SYN packets sent before the connection.
 net.ipv4.tcp_syn_retries

Option determines socket remain in FIN-WAIT-2 state time. The default is 60 seconds. This value is set up correctly is very important, and sometimes even a small Web server load, there will be risks because a large number of dead sockets arising out of memory.
 net.ipv4.tcp_fin_timeout

Keepalive option indicates when enabled, TCP transmission frequency of keepalive messages. The default value is 2 (in hours).
 net.ipv4.tcp_keepalive_time

 

Original Address: https://blog.51cto.com/ixdba/803475

Guess you like

Origin www.cnblogs.com/91donkey/p/11711658.html