發新話題

調整Linux TCP參數 提升伺服器性能

調整Linux TCP參數 提升伺服器性能

用netstat -ant|grep TIME_WAIT|wc -l查看發現有700多的等待請求,原來是TCP/IP 棧引起的。
  對於多數人來說,配置 TCP/IP 棧是邪惡的魔術,但是如果您運行高負載的 Web 服務器,那麼有選擇地更改 Red Hat 的一些缺省值能明顯提高系統響應速度。TCP/IP 配置值被存儲在 /proc/sys/net/ipv4 目錄中,通常可以是某個值或用「0」(關)和「1」(開)來分別表示禁用和啟用。linux缺省值如下:
  tcp_fin_timeout 180
  tcp_keepalive_time 7200
  tcp_window_scaling 1
  tcp_sack 1
  tcp_timestamps 1
  在下面的設置中,更改某些值能增加服務器可處理的 TCP/IP 連接的數量,縮短服務器在終止失效連接前的等待時間,禁用一些不必要的 IP 擴展。把這些設置添加到 /etc/sysctl.conf 文件,然後重新啟動網絡守護程序( /etc/rc.d/init.d/network restart )。
  # Decrease the time default value for tcp_fin_timeout connection
  net.ipv4.tcp_fin_timeout = 30
  # Decrease the time default value for tcp_keepalive_time connection
  net.ipv4.tcp_keepalive_time = 1800
  # Turn off tcp_window_scaling
  net.ipv4.tcp_window_scaling = 0
  # Turn off the tcp_sack
  net.ipv4.tcp_sack = 0
  #Turn off tcp_timestamps
  net.ipv4.tcp_timestamps = 0
  加到/etc/rc.local
  代碼:
  echo "30">/proc/sys/net/ipv4/tcp_fin_timeout
  echo "1800">/proc/sys/net/ipv4/tcp_keepalive_time
  echo "0">/proc/sys/net/ipv4/tcp_window_scaling
  echo "0">/proc/sys/net/ipv4/tcp_sack
  echo "0">/proc/sys/net/ipv4/tcp_timestamps
  通過以上修改,TIME_WAIT明顯減少!
---------------------------------------------------------

Securing and Optimizing Linux: RedHat Edition -A Hands on Guide
Prev        Chapter 6. Linux General Optimization        Next
6.13. Better manage your TCP/IP resources

This hack just make the time default values for TCP/IP connection lower so that more connections can be handled by time on your TCP/IP protocol. The following will decrease the amount of time your Linux box will try take to finish closing a connection and the amount of time before it will kill a stale connection. This will also turn off some IP extensions that aren't needed. The default setup for the TCP/IP parameters we'll change under Red Hat Linux are:

    *

      For the tcp_fin_timeout 180
    *

      For the tcp_keepalive_time 7200
    *

      For the tcp_window_scaling 1
    *

      For the tcp_sack 1
    *

      For the tcp_timestamps 1

To adjust the new TCP/IP values, type the following commands on your terminal:

             [root@deep] /#echo 30 >/proc/sys/net/ipv4/tcp_fin_timeout
             [root@deep] /#echo 1800 >/proc/sys/net/ipv4/tcp_keepalive_time
             [root@deep] /#echo 0  >/proc/sys/net/ipv4/tcp_window_scaling
             [root@deep] /#echo 0  >/proc/sys/net/ipv4/tcp_sack
             [root@deep] /#echo 0  >/proc/sys/net/ipv4/tcp_timestamps
            

Execute the above commands and put them in your /etc/rc.d/rc.local file so you don't need to type them again each time your system reboots.

Version 6.2 only
Edit the /etc/sysctl.conf file and add the following lines:

             # Decrease the time default value for tcp_fin_timeout connection
             net.ipv4.tcp_fin_timeout = 30
             # Decrease the time default value for tcp_keepalive_time connection
             net.ipv4.tcp_keepalive_time = 1800
             # Turn off the tcp_window_scaling
             net.ipv4.tcp_window_scaling = 0
             # Turn off the tcp_sack
             net.ipv4.tcp_sack = 0
             # Turn off the tcp_timestamps
             net.ipv4.tcp_timestamps = 0
            

You must restart your network for the change to take effect. The command to manually restart the network is the following:

             [root@deep] /# /etc/rc.d/init.d/network restart
            

Setting network parameters [ OK ] Bringing up interface lo [ OK ] Bringing up interface eth0 [ OK ] Bringing up interface eth1 [ OK ]

TOP

減少Linux下Squid服務器的TIME_WAIT套接字數量

Linux下高並發的Squid服務器,TCP TIME_WAIT套接字數量經常達到兩、三萬,服務器很容易被拖死。通過修改Linux內核參數,可以減少Squid服務器的TIME_WAIT套接字數量。
  
vi /etc/sysctl.conf

  增加以下幾行:
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

  說明:
  net.ipv4.tcp_syncookies = 1 表示開啟SYN Cookies。當出現SYN等待隊列溢出時,啟用cookies來處理,可防範少量SYN攻擊,默認為0,表示關閉;
  net.ipv4.tcp_tw_reuse = 1 表示開啟重用。允許將TIME-WAIT sockets重新用於新的TCP連接,默認為0,表示關閉;
  net.ipv4.tcp_tw_recycle = 1 表示開啟TCP連接中TIME-WAIT sockets的快速回收,默認為0,表示關閉。
  net.ipv4.tcp_fin_timeout = 30 表示如果套接字由本端要求關閉,這個參數決定了它保持在FIN-WAIT-2狀態的時間。
  net.ipv4.tcp_keepalive_time = 1200 表示當keepalive起用的時候,TCP發送keepalive消息的頻度。缺省是2小時,改為20分鐘。
  net.ipv4.ip_local_port_range = 1024 65000 表示用於向外連接的端口範圍。缺省情況下很小:32768到61000,改為1024到65000。
  net.ipv4.tcp_max_syn_backlog = 8192 表示SYN隊列的長度,默認為1024,加大隊列長度為8192,可以容納更多等待連接的網絡連接數。
  net.ipv4.tcp_max_tw_buckets = 5000 表示系統同時保持TIME_WAIT套接字的最大數量,如果超過這個數字,TIME_WAIT套接字將立刻被清除並打印警告信息。默認為180000,改為5000。對於Apache、Nginx等服務器,上幾行的參數可以很好地減少TIME_WAIT套接字數量,但是對於Squid,效果卻不大。此項參數可以控制TIME_WAIT套接字的最大數量,避免Squid服務器被大量的TIME_WAIT套接字拖死。

  執行以下命令使配置生效:
/sbin/sysctl -p

TOP

發新話題

本站所有圖文均屬網友發表,僅代表作者的觀點與本站無關,如有侵權請通知版主會盡快刪除。