Centos6.5内核优化-10万并发

内核优化

netstat -n | awk '/^tcp/ {++S[$NF]} END {for(a in S) print a, S[a]}'
net.ipv4.tcp_syncookies = 1
net.ipv4.tcp_abort_on_overflow = 1
net.ipv4.tcp_max_tw_buckets = 6000
net.ipv4.tcp_sack = 1
net.ipv4.tcp_window_scaling = 1
net.ipv4.tcp_rmem = 4096 87380 4194304
net.ipv4.tcp_wmem = 4096 66384 4194304
net.ipv4.tcp_mem = 94500000 915000000 927000000
net.core.optmem_max = 81920
net.core.wmem_default = 8388608
net.core.wmem_max = 16777216
net.core.rmem_default = 8388608
net.core.rmem_max = 16777216
net.ipv4.tcp_max_syn_backlog = 1020000
net.core.netdev_max_backlog = 262144
net.core.somaxconn = 262144
net.ipv4.tcp_max_orphans = 262144

net.ipv4.tcp_timestamps = 0
net.ipv4.tcp_synack_retries = 1
net.ipv4.tcp_syn_retries = 1
net.ipv4.tcp_tw_recycle = 1
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_fin_timeout = 15
net.ipv4.tcp_keepalive_time = 30
net.ipv4.ip_local_port_range = 1024 65000
net.netfilter.nf_conntrack_tcp_timeout_established = 180
net.netfilter.nf_conntrack_max = 1048576
net.nf_conntrack_max = 1048576
#开启SYN Cookies,当出现SYN 等待队列溢出时,启用cookies 来处理
#布尔类型,控制着当有很多的连接请求时内核的行为。启用的话,如果服务超载,内核将主动地发送RST包
#默认180000,Apache、Nginx等服务器,参数可以控制TIME_WAIT的最大数量,服务器被大量的TIME_WAIT拖死
#有选择的应答 
#表示设置tcp/ip会话的滑动窗口大小是否可变。参数值为布尔值,为1可变,为0不可变,提高数据传输的能力
#TCP接收缓冲区
#TCP发送缓冲区
# Out of socket memory
#该文件表示每个套接字所允许的最大缓冲区的大小。
#该文件指定了发送套接字缓冲区大小的缺省值(以字节为单位)
#指定了发送套接字缓冲区大小的最大值(以字节为单位)
#指定了接收套接字缓冲区大小的缺省值(以字节为单位)
#指定了接收套接字缓冲区大小的最大值(以字节为单位)
#表示SYN队列的长度,默认为1024,加大队列长度为10200000,可以容纳更多等待连接的网络连接数。
#每个网络接口接收数据包的速率比内核处理这些包的速率快时,允许送到队列的数据包的最大数目。
#默认内核参数的net.core.somaxconn限制到128,而nginx 定义的NGX_LISTEN_BACKLOG 默认为511
#这个限制仅仅是为了防止简单的DoS 攻击,不能过分依靠它或者人为地减小这个值,更应该增加这个
#1Gbps 的链路会遇到以前用过的序列号。时间戳能够让内核接受这种“异常”的数据包要将其关掉。
#三次握手中的第二次握手。这个设置决定了内核放弃连接之前发送SYN+ACK 包的数量。
#在内核放弃建立连接之前发送SYN 包的数量
#表示开启TCP连接中TIME-WAIT sockets的快速回收,默认为0,表示关闭;
#表示开启重用。允许将TIME-WAIT sockets重新用于新的TCP连接,默认为0,表示关闭
#修改系統默认的 TIMEOUT 时间。
#表示当keepalive起用的时候,TCP发送keepalive消息的频度。缺省是2小时,建议改为20分钟。
#表示用于向外连接的端口范围。缺省情况下很小:32768到61000,改为10000到65000。
#縮短established的超時時間
#CONNTRACK_MAX 允许的最大跟踪连接条目,是在内核内存中netfilter可以同时处理的“任务”

 
vim  /etc/sysctl.conf
    # 关闭路由转发
    net.ipv4.ip_forward = 0
     
    # 开启反向路径过滤
    net.ipv4.conf.all.rp_filter= 1
    net.ipv4.conf.default.rp_filter = 1
     
    # 处理无源路由的包
    net.ipv4.conf.all.accept_source_route = 0
    net.ipv4.conf.default.accept_source_route = 0
     
    kernel.sysrq = 0
    kernel.core_uses_pid = 1
    kernel.msgmnb = 65536
    kernel.msgmax = 65536
    kernel.shmmax = 68719476736
    kernel.shmall = 4294967296
     
    # TTL
    net.ipv4.ip_default_ttl = 64
     
    # 内核panic时,1s后自动重启
    kernel.panic = 1
     
    # 允许更多的PIDs
    kernel.pid_max = 32768
    # 应对DDOS攻击,TCP连接建立设置
    net.ipv4.tcp_syncookies = 1
    net.ipv4.tcp_synack_retries = 1
    net.ipv4.tcp_syn_retries = 1
    net.ipv4.tcp_max_syn_backlog = 262144
    # 应对timewait过高,TCP连接断开设置
    net.ipv4.tcp_max_tw_buckets = 6000
    net.ipv4.tcp_tw_recycle = 1
    net.ipv4.tcp_tw_reuse = 0
    net.ipv4.tcp_timestamps = 0
    net.ipv4.tcp_fin_timeout = 10
    net.ipv4.ip_local_port_range = 10000 65000
     
    # 内存资源使用相关设定
    net.core.wmem_default = 8388608
    net.core.rmem_default = 8388608
    net.core.rmem_max = 16777216
    net.core.wmem_max = 16777216
    net.ipv4.tcp_rmem = 4096 87380 4194304
    net.ipv4.tcp_wmem = 4096 16384 4194304
    net.ipv4.tcp_mem = 94500000 915000000 927000000
     
    # TCP keepalived连接设置
    net.ipv4.tcp_keepalive_time = 30
    net.ipv4.tcp_keepalive_intvl = 15
    net.ipv4.tcp_keepalive_probes = 5
     
    # 其他TCP相关参数调节
    net.core.somaxconn = 262144
    net.core.netdev_max_backlog = 262144
    net.ipv4.tcp_max_orphans = 3276800
    net.ipv4.tcp_sack = 1
    net.ipv4.tcp_window_scaling = 1

    # 生效
    /sbin/sysctl -p

 

 

posted @ 2017-05-04 19:38  sunmmi  阅读(1461)  评论(0)    收藏  举报