天天看點

EMQX的伺服器調優1. Linux 作業系統參數2. TCP 協定棧網絡參數3. 總結

文章目錄

  • 1. Linux 作業系統參數
    • 1.1 file-max
    • 1.2 ulimit
    • 1.3 nr_open
  • 2. TCP 協定棧網絡參數
    • 2.1 并發連接配接 backlog 設定
    • 2.2 可用端口範圍
    • 2.3 TCP Socket 讀寫 Buffer 設定
    • 2.4 TCP 連接配接追蹤設定
    • 2.5 TIME-WAIT Socket 最大數量、回收與重用設定
    • 2.6 FIN-WAIT-2 Socket 逾時設定
    • 2.7 立即生效
  • 3. 總結

1. Linux 作業系統參數

1.1 file-max

說明:所有程序最大的檔案數
  • 臨時設定(不推薦)
  • 持久化+即時生效(推薦)
vim /etc/sysctl.conf
fs.file-max=2097152

sysctl -p
           

1.2 ulimit

說明:使用者最大句柄數
  • 臨時設定(不推薦)

  • 持久化設定
vim /etc/security/limits.conf
*      soft   nofile      1048576
*      hard   nofile      1048576
           
說明:在Centos7系統中,

/etc/security/limits.conf

檔案的配置作用域縮小了,其隻适用于通過PAM認證登入使用者的資源限制。而systemd service的資源設定,則需修改全局配置

/etc/systemd/system.conf

  • 全局配置
# vim /etc/systemd/system.conf
DefaultLimitNOFILE=1048576
           

1.3 nr_open

說明:單個程序可配置設定的最大檔案數
  • 持久化+即時生效(推薦)
vim /etc/sysctl.conf
fs.nr_open=2097152

sysctl -p
           
  • 臨時生效(不推薦)

或者

echo 2097152 > /proc/sys/fs/nr_open
           

2. TCP 協定棧網絡參數

/etc/sysctl.conf

中添加如下參數:

2.1 并發連接配接 backlog 設定

net.ipv4.tcp_max_syn_backlog = 65536
net.core.netdev_max_backlog =  32768
net.core.somaxconn = 32768
           

2.2 可用端口範圍

2.3 TCP Socket 讀寫 Buffer 設定

net.core.wmem_default = 8388608
net.core.rmem_default = 8388608
net.core.rmem_max = 16777216
net.core.wmem_max = 16777216
net.core.optmem_max=16777216
#net.ipv4.tcp_mem = 94500000 915000000 927000000
net.ipv4.tcp_rmem=1024 4096 16777216
net.ipv4.tcp_wmem=1024 4096 16777216
           

2.4 TCP 連接配接追蹤設定

net.nf_conntrack_max=1000000
net.netfilter.nf_conntrack_max=1000000
net.netfilter.nf_conntrack_tcp_timeout_time_wait=30
           

2.5 TIME-WAIT Socket 最大數量、回收與重用設定

net.ipv4.tcp_max_tw_buckets=1048576
#以下兩個不建議開啟該設定,NAT 模式下可能引起連接配接 RST
#net.ipv4.tcp_tw_recycle = 1
#net.ipv4.tcp_tw_reuse = 1
           

說明:

net.ipv4.tcp_tw_reuse = 1 表示開啟重用。允許将TIME-WAIT sockets重新用于新的TCP連接配接,預設為0,表示關閉;

net.ipv4.tcp_tw_recycle = 1 表示開啟TCP連接配接中TIME-WAIT sockets的快速回收,預設為0,表示關閉。

2.6 FIN-WAIT-2 Socket 逾時設定

2.7 立即生效

同樣執行如下指令立即生效:

3. 總結

file-max=2097152
fs.nr_open=2097152
net.ipv4.tcp_max_syn_backlog = 65536
net.core.netdev_max_backlog =  32768
net.core.somaxconn = 32768
net.ipv4.ip_local_port_range = 1024  65535
net.core.wmem_default = 8388608
net.core.rmem_default = 8388608
net.core.rmem_max = 16777216
net.core.wmem_max = 16777216
net.core.optmem_max=16777216
#net.ipv4.tcp_mem = 94500000 915000000 927000000
net.ipv4.tcp_rmem=1024 4096 16777216
net.ipv4.tcp_wmem=1024 4096 16777216
net.nf_conntrack_max=1000000
net.netfilter.nf_conntrack_max=1000000
net.netfilter.nf_conntrack_tcp_timeout_time_wait=30
net.ipv4.tcp_max_tw_buckets=1048576
net.ipv4.tcp_max_tw_buckets=1048576
           
EMQX的伺服器調優1. Linux 作業系統參數2. TCP 協定棧網絡參數3. 總結

繼續閱讀