Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save zz/73e4513be7d285b988a39edc5cc4a1f1 to your computer and use it in GitHub Desktop.
Save zz/73e4513be7d285b988a39edc5cc4a1f1 to your computer and use it in GitHub Desktop.
Shadowsocks服务端性能优化

#Shadowsocks服务端性能优化(CentOS6)

##ss加密算法 常用算法aes-256-cfb速度较慢,在路由器等arm平台表现不佳。 用于arm平台时,采用算法rc4-md5即可。 "method":"rc4-md5" 为加速加密速度,安装m2crypto

yum install m2crypto

##开启One Time Auth提升安全性 预防CCA攻击,执行

vim /etc/init.d/shadowsocks-libev

将原start()函数部分代码

daemon $DAEMON -u -c $CONF -f $PID

改为

daemon $DAEMON -u -A -c $CONF -f $PID

重启SS服务后生效,注意在客户端内开启OTA。 ##TCP参数调优 ###增加系统文件描述符最大限制数 编辑文件limits.conf

vim /etc/security/limits.conf

增加以下两行

* soft nofile 65535
* hard nofile 65535

重启后生效,使用ulimit -n 65535可即时修改。 ###sysctl内核参数优化 默认TCP拥塞算法为cubie,注重了TCP连接的均衡性,避免单一连接抢占过多资源,不利于ss代理! 对于高延迟网络(美国,欧洲),可改为hybla算法。 对于低延迟网络(日本,香港,新加坡),可改为htcp算法。 首先,查询本机已加载的TCP拥塞算法,

sysctl net.ipv4.tcp_available_congestion_control

若无hybla与htcp算法,使用下面命令加载:

/sbin/modprobe tcp_htcp
/sbin/modprobe tcp_hybla

若希望自动加载,修改rc.sysinit,

vim /etc/rc.d/rc.sysinit

# Now that we have all of our basic modules loaded and the kernel going,这行前,加入

modprobe tcp_htcp >/dev/null 2>&1
modprobe tcp_hybla  >/dev/null 2>&1

对于几种算法的分析,详情可以参考下:TCP拥塞控制算法 优缺点 适用环境 性能分析

编辑sysctl.conf文件

vim /etc/sysctl.conf

在最后加入如下内容:

# 提高整个系统的文件限制
# max open files
fs.file-max = 51200

# max read buffer
net.core.rmem_max = 67108864
# max write buffer
net.core.wmem_max = 67108864
# default read buffer
net.core.rmem_default = 65536
# default write buffer
net.core.wmem_default = 65536
# max processor input queue
net.core.netdev_max_backlog = 4096
# max backlog
net.core.somaxconn = 4096

# resist SYN flood attacks
# 表示开启SYN Cookies。当出现SYN等待队列溢出时,启用cookies来处理,可防范少量SYN攻击,默认为0,表示关闭;
net.ipv4.tcp_syncookies = 1
# reuse timewait sockets when safe
# 表示开启重用。允许将TIME-WAIT sockets重新用于新的TCP连接,默认为0,表示关闭;
net.ipv4.tcp_tw_reuse = 1
# turn off fast timewait sockets recycling
# 表示开启TCP连接中TIME-WAIT sockets的快速回收,默认为0,表示关闭;
net.ipv4.tcp_tw_recycle = 1
# short FIN timeout
# 修改系統默认的 TIMEOUT 时间。
net.ipv4.tcp_fin_timeout = 30
# short keepalive time
# 表示当keepalive起用的时候,TCP发送keepalive消息的频度。缺省是2小时,改为20分钟。
net.ipv4.tcp_keepalive_time = 1200
# outbound port range
# 表示用于向外连接的端口范围。缺省情况下很小:32768到61000,改为10000到65000。(注意:这里不要将最低值设的太低,否则可能会占用掉正常的端口!)
net.ipv4.ip_local_port_range = 10000 65000
# max SYN backlog
# 表示SYN队列的长度,默认为1024,加大队列长度为8192,可以容纳更多等待连接的网络连接数。
net.ipv4.tcp_max_syn_backlog = 4096
# max timewait sockets held by system simultaneously
# 表示系统同时保持TIME_WAIT的最大数量,如果超过这个数字,TIME_WAIT将立刻被清除并打印警告信息。
net.ipv4.tcp_max_tw_buckets = 5000

# TCP receive buffer
net.ipv4.tcp_rmem = 4096 87380 67108864
# TCP write buffer
net.ipv4.tcp_wmem = 4096 65536 67108864
# turn on path MTU discovery
net.ipv4.tcp_mtu_probing = 1

# turn on TCP Fast Open on both client and server side
# 对于内核版本新于**3.7.1**的,我们可以开启tcp_fastopen:
# net.ipv4.tcp_fastopen = 3

# for high-latency network
# net.ipv4.tcp_congestion_control = hybla

# for low-latency network, use htcp instead
net.ipv4.tcp_congestion_control = htcp

执行

sysctl --system
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment