Modifying Linux Kernel Parameters improves Nginx server performance

Source: Internet
Author: User
When the number of TCPTIME_WAIT sockets frequently reaches two or 30 thousand for a high-concurrency Nginx server in a concurrent Linux, the server is easily dragged to death. By modifying Linux kernel parameters, you can reduce the number of TIME_WAIT sockets on the Nginx server. Vietcsysctl. conf adds the following lines: reference net. ipv4.tcp _ fin_timeout30net.ipv

When the number of TCP TIME_WAIT sockets frequently reaches two or 30 thousand for a high-concurrency Nginx server in a concurrent Linux, the server is easily dragged to death. By modifying Linux kernel parameters, you can reduce the number of TIME_WAIT sockets on the Nginx server.

Vi/etc/sysctl. conf


Add the following lines:
Reference
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


Simple Description:
Net. ipv4.tcp _SyncOokies = 1 indicates enabling SYN Cookies. When a SYN wait queue overflows, cookies are enabled to prevent a small number of SYN attacks. The default value is 0, indicating that the process is disabled;
Net. ipv4.tcp _ tw_reuse = 1 indicates enabling reuse. Allow TIME-WAIT sockets to be re-used for a New TCP connection. The default value is 0, indicating that the TCP connection is disabled;
Net. ipv4.tcp _ tw_recycle = 1 indicates to enable quick recovery of TIME-WAIT sockets in TCP connections. The default value is 0, indicating to disable it.
Net. ipv4.tcp _ fin_timeout = 30 indicates that if the socket is disabled by the local end, this parameter determines the time it remains in the FIN-WAIT-2 state.
Net. ipv4.tcp _ keepalive_time = 1200 indicates the frequency of keepalive messages sent by TCP when keepalive is in use. The default value is 2 hours, which is changed to 20 minutes.
Net. ipv4.ip _ local_port_range = 1024 65000 indicates the port range used for external connection. The default value is small: 32768 to 61000, Which is changed to 1024 to 65000.
Net. ipv4.tcp _ max_syn_backlog = 8192 indicates the length of the SYN queue. The default value is 1024. The length of the queue is 8192, which can accommodate more network connections waiting for connection.
Net. ipv4.tcp _ max_tw_buckets = 5000 indicates that the system maintains the maximum number of TIME_WAIT sockets at the same time. If this number is exceeded, the TIME_WAIT socket is immediately cleared and warning information is printed. The default value is 180000, Which is changed to 5000. For servers such as Apache and Nginx, the number of TIME_WAIT sockets can be greatly reduced by parameters in the previous lines, but for SquId, But the effect is not big. This parameter can control the maximum number of TIME_WAIT sockets to prevent the Squid server from being dragged to death by a large number of TIME_WAIT sockets.

Echo "==================================== execute the followingCommandMake the configuration take effect: ========================="
# Http://www.linuxidc.com/command that takes effect immediately after changing Linux Kernel Parameters!

/Sbin/sysctl-p

Nginx Optimization

Use FastCGI Cache
Fastcgi_cache TEST
Enable FastCGI cache and specify a name for it. I personally think it is very useful to enable cache, which can effectively reduce the CPU load and prevent 502 errors.

Fastcgi_cache_path/usr/local/nginx/fastcgi_cache leveLs=
Keys_zone = TEST: 10 m
Inactive = 5 m;
This command specifies a path for the FastCGI cache, the directory structure level, the storage time of the keyword region, and the non-active deletion time.


For stress testing, webbench is used. The test result can only be used as a reference.
There will be other parameter modifications that have been made earlier in the future. If you have time, share them. You are welcome to discuss and communicate with each other. Deduction: ⑥ ()

Related Article

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.