Sometimes we find a large number of TIME-WAIT connections on the server, as many as tens of thousands. By modifying the kernel parameters, the system can quickly recycle time-wait {This article is only for your own use, please do not spray}
Check the TCP connection status: [root@centos ~]# netstat -n | awk '/^tcp/ {++S[$NF]} END {for(a in S) print a, S[a]}' LAST_ACK 56 SYN_RECV 77 ESTABLISHED 11213 FIN_WAIT1 4013 FIN_WAIT2 1638 CLOSING 3 TIME_WAIT 12261
Modify kernel parameters to quickly recycle time_wait sockets: [root@centos ~]# echo "net.ipv4.tcp_tw_reuse = 1" >> /etc/sysctl.conf # means to enable reuse. Allow TIME-WAIT sockets to be reused for new TCP connections, the default is 0, which means close; [root@centos ~]# echo "net.ipv4.tcp_tw_recycle = 1" >> /etc/sysctl.conf # Indicates to enable fast recycling of TIME-WAIT sockets in TCP connections, the default is 0, which means close. [root@centos ~]# sysctl -p
After the modification is completed, look at the TCP connection status. The time-wait connection is significantly reduced, and it feels that accessing Apache is much faster. [root@centos ~]# netstat -n | awk '/^tcp/ {++S[$NF]} END {for(a in S) print a, S[a]}' LAST_ACK 26 SYN_RECV 225 ESTABLISHED 9228 FIN_WAIT1 4002 FIN_WAIT2 611 CLOSING 2 TIME_WAIT 415