AB测试# 进入AB工具目录 cd /usr/local/httpd/bin # ab命令 -c 并发数 -n 请求次数 测试网址(必须带文件) ./ab -c 1000 -n 50000 http://172.20.17.211/index.html # 结果分析 Percentage of the requests served within a certain time (ms) 50% 50 66% 51 75% 53 80% 55 // 80%页面 消耗时间在55毫秒 90% 663 95% 1051 98% 3001 99% 3200 100% 19926 (longest request) # 再来测试一个大的 [root@localhost bin]# ./ab -c 2000 -n 80000 http://172.20.17.211/index.html This is ApacheBench, Version 2.3 Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/ Licensed to The Apache Software Foundation, http://www.apache.org/ Benchmarking 172.20.17.211 (be patient) socket: Too many open files (24) # 报错了,提示打开的文件太多了,来解决 # 查看默认服务器准许最高打开的描述符,一般1024 ulimit -n # 临时修改最高描述符个数 ulimit -n 20000 # 永久神效 vim /etc/rc.local # 最后一行加入 echo 100000 > /proc/sys/fs/file-max # 查看系统错误信息 dmesg|tail # 如果提示下面信息,表示洪水攻击,或者说服务器处理不过来了 possible syn flooding on port 80 . sending cookies.
Apache优化# 编辑Apache连接数配置 vim /etc/httpd/conf/httpd.conf StartServers 8 MinSpareServers 5 MaxSpareServers 20 ServerLimit 256 MaxClients 256 MaxRequestsPerChild 4000 修改 ServerLimit 1500 MaxClients 1500
Nginx 优化
Linux 系统优化# sock 优化 more /proc/sys/net/core/somaxconn echo 50000 > /proc/sys/net/core/somaxconn # 加快TCP回收 cat /proc/sys/net/ipv4/tcp_tw_recycle # 默认值0:不自动回收 echo 1 > /proc/sys/net/ipv4/tcp_tw_recycle # 空的TCP回收利用 cat /proc/sys/net/ipv4/tcp_tw_reuse echo 1 > /proc/sys/net/ipv4/tcp_tw_reuse # 不做洪水抵御 cat /proc/sys/net/ipv4/tcp_syncookies echo 0 > /proc/sys/net/ipv4/tcp_syncookies # 编辑 vim /etc/sysctl.conf 文件永久优化 vm.swappiness = 0 net.ipv4.neigh.default.gc_stale_time=120 net.ipv4.conf.all.rp_filter=0 net.ipv4.conf.default.rp_filter=0 net.ipv4.conf.default.arp_announce = 2 net.ipv4.conf.all.arp_announce=2 net.ipv4.tcp_max_tw_buckets = 5000 net.ipv4.tcp_syncookies = 0 # SYN等待队列溢出时,启用cookies来处理 0关闭1启用 net.ipv4.tcp_max_syn_backlog = 10240 # 表示未收到客户端确认信息的连接(SYN消息)最大长度 net.ipv4.tcp_synack_retries = 2 # 重试次数 net.ipv4.conf.lo.arp_announce=2 net.ipv4.tcp_tw_reuse = 1 # 空的TCP回收利用 0关闭 1启用 net.ipv4.tcp_tw_recycle = 1 # 加快TCP回收 0关闭 1启用 net.core.somaxconn = 262144 # 限制监听(LISTEN)队列最大数据包的数量 # 立即生效 sysctl -p /etc/sysctl.conf
优化脚本vim /data/sh/tcpopt.sh nginx 添加统计模块,便于观察nginx状态./configure –prefix=/usr/local/nginx/ –add-module=/app/ngx_http_consistent_hash-master –with-http_stub_status_module make && make install # 配置nginx location /status { stub_status On; # 开启统计模块 access_log Off; # 关闭日志记录 allow 192.168.1.168; # 准许访问的IP deny all; # 其他都不准许访问 }