Optimization of Linux kernel parameters for Nginx optimization (2)

Source: Internet
Author: User

Original Blog address (Welcome to visit): http://www.loveyqq.tk/blog/2014/05/27/nginxyou-hua-pian-zhi-linux-nei-he-can-shu-de-you-hua/

because the default Linux kernel parameters are considered the most common scenario, This is clearly incompatible with the definition of a Web server to support high concurrent access, so it is necessary to modify the Linux kernel parameters so that nginx can have higher performance.

#原有字段net. ipv4.tcp_syncookies = # new Field Fs.file-max = 999999net.ipv4.tcp_tw_reuse = 1net.ipv4.tcp_keepalive_time = 600net.ipv4.tcp_fin_timeout = 30net.ipv4.tcp_max_tw_buckets = 5000net.ipv4.ip_local_port_range = 1024 61000net.ipv4.tcp_rmem = 10240 87380 12582912net.ipv4.tcp_wmem = 10240 87380 12582912net.core.netdev_max_backlog = 8096net.core.rmem_default = 6291456net.core.wmem_default = 6291456net.core.rmem_max = 12582912net.core.wmem_max = 12582912net.ipv4.tcp_max_syn_backlog = 1024


Then execute the sysctl-p command to make the above parameters effective.

The above parameter meanings are explained as follows:

Fs.file-max = 999999: This parameter indicates the maximum number of handles that a process (such as a worker process) can open at the same time, which limits the maximum number of concurrent connections and is configured according to the actual situation.

Net.ipv4.tcp_tw_reuse = 1: This parameter is set to 1, which means that the socket of the time-wait state is allowed to be re-used for new TCP connections, which makes sense for the server because there is always a large number of time-wait-state connections on the server.

Net.ipv4.tcp_keepalive_time = 600: This parameter indicates the frequency at which TCP sends keepalive messages when KeepAlive is enabled. The default is 2 hours, and if you set it smaller, you can quickly clean up invalid connections.

Net.ipv4.tcp_fin_timeout = 30: This parameter indicates the maximum time that the socket remains in the Fin-wait-2 state when the server actively shuts down the connection.

Net.ipv4.tcp_max_tw_buckets = 5000: This parameter indicates the maximum number of sockets that the operating system is allowed to time_wait, and if this number is exceeded, the time_wait socket is immediately cleared and the warning message is printed. This parameter defaults to 180 000, and too many time_wait sockets cause the Web server to become slower.

Net.ipv4.tcp_max_syn_backlog = 1024: This parameter indicates that the TCP three handshake establishment phase accepts the maximum length of the SYN request queue, defaults to 1024, and sets it larger to make the nginx busy too late to accept a new connection , Linux does not lose client-initiated connection requests.

Net.ipv4.ip_local_port_range = 1024 61000: This parameter defines the range of values for local (not including remote) ports in UDP and TCP connections.

Net.ipv4.tcp_rmem = 10240 87380 12582912: This parameter defines the minimum, default, maximum value of the TCP accept cache (for TCP to accept sliding windows).

Net.ipv4.tcp_wmem = 10240 87380 12582912: This parameter defines the minimum, default, and maximum values for the TCP send cache (for TCP send sliding windows).

Net.core.netdev_max_backlog = 8096: When the network card accepts packets faster than the kernel processing speed, there is a queue to hold the packets. This parameter represents the maximum value of the queue.

Net.core.rmem_default = 6291456: This parameter indicates that the kernel socket accepts the default size of the buffer.

Net.core.wmem_default = 6291456: This parameter indicates the default size of the kernel socket sending buffer.

Net.core.rmem_max = 12582912: This parameter indicates the maximum size of the kernel socket to accept the buffer.

Net.core.wmem_max = 12582912: This parameter indicates the maximum size of the kernel socket sending buffer.

Net.ipv4.tcp_syncookies = 1: This parameter is independent of performance and is used to resolve SYN attacks on TCP.

Note: The size of the sliding window and the socket buffer affect the number of concurrent connections to some extent. Each TCP connection consumes memory for maintaining the TCP sliding window, which shrinks or expands depending on the processing speed of the server.

The setting of parameter Net.core.wmem_max = 12582912, which needs to balance the total size of the physical memory and the maximum number of connections that Nginx can handle concurrently. Of course, it is not appropriate to reduce the size of the sliding window simply to provide concurrency so that the server does not have an out-of-memory problem, because a sliding window is too small to affect the transfer speed of large data volumes. Net.core.rmem_default = 6291456, Net.core.wmem_default = 6291456,

Net.core.rmem_max = 12582912 and Net.core.wmem_max = 12582912 The settings for these 4 parameters need to be considered in terms of our business characteristics and actual hardware costs.

The maximum number of connections that Nginx handles concurrently: determined by the work_processes and work_connections parameters in the nginx.conf.



Optimization of Linux kernel parameters for Nginx optimization (2)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.