本文共 2761 字,大约阅读时间需要 9 分钟。
前言* 随着网站访问人数越来越多,承受的并发和压力也越来越高,这时候我们需要对网站和架构进行优化,今天我们来讨论使用Squid对架构进行优化,缓存网站。网上对squid描述的文章也有成千上万,我这里简单记录一下实践的步骤。
一、实施环境
系统版本:CentOSx86_64 5.8Squid版本:squid-2.6Nginx版本:nginx-1.4.2
二、正式安装
安装之前我们需要对系统进行优化,主要优化系统内核相关参数,仅供参考:
vi /etc/sysctl.conf#sysctl.conf config 2014-03-26net.ipv4.ip_forward = 0net.ipv4.conf.default.rp_filter = 1net.ipv4.conf.default.accept_source_route = 0kernel.sysrq = 0kernel.core_uses_pid = 1net.ipv4.tcp_syncookies = 1kernel.msgmnb = 65536kernel.msgmax = 65536kernel.shmmax = 68719476736kernel.shmall = 4294967296net.ipv4.tcp_max_tw_buckets = 10000net.ipv4.tcp_sack = 1net.ipv4.tcp_window_scaling = 1net.ipv4.tcp_rmem = 4096 87380 4194304net.ipv4.tcp_wmem = 4096 16384 4194304net.core.wmem_default = 8388608net.core.rmem_default = 8388608net.core.rmem_max = 16777216net.core.wmem_max = 16777216net.core.netdev_max_backlog = 262144net.core.somaxconn = 262144net.ipv4.tcp_max_orphans = 3276800net.ipv4.tcp_max_syn_backlog = 262144net.ipv4.tcp_timestamps = 0net.ipv4.tcp_synack_retries = 1net.ipv4.tcp_syn_retries = 1net.ipv4.tcp_tw_recycle = 1net.ipv4.tcp_tw_reuse = 1net.ipv4.tcp_mem = 94500000 915000000 927000000net.ipv4.tcp_fin_timeout = 1net.ipv4.tcp_keepalive_time = 15net.ipv4.ip_local_port_range = 1024 65535
优化Linux文件打开最大数:
vi /etc/security/limits.conf * soft nproc 65535* hard nproc 65535* soft nofile 65535* hard nofile 65535
接下来上自动安装Squid脚本,里面分别配置了两个虚拟主机域名,前端有LVS,LVS均衡后端多组squid集群,根据命中率去调整squid集群的数量,Squid后端均衡Nginx或者Apache。(完整的架构LVS+Keepalived+Squid+Nginx+Resin/Tomcat/PHP+MySQL集群)
简单逻辑图如下:
直接上脚本:
#!/bin/sh#Auto make install squid server#Author wugk 2014-03-26SQUID_CNF=/etc/squid/squid.confCACHE_DIR=( /data/cache1 /data/cache2)#Install squid shellyum install -y squid#config squid.confcat >>$SQUID_CNF <
最后测试,前端LVS截图(注LVS此处不配置了,博客有专门的安装方法)
通过浏览器查看head头,缓存命中情况截图如下:
通过命令
squidclient -p 80 mgr:info |egrep "(Request Hit Ratios|Byte Hit Ratios)"
查看缓存命中率如下:
三、批量清空缓存
使用Shell脚本批量清空squid缓存脚本auto_clean_cache.sh
#!/bin/shDIR=/data/cache/Command=/usr/sbin/squidclientif [ "$1" = "" ];then echo "Usage:{$0 "\$1" ,Example exec $0 forum.php}" exitfigrep -r -a $1 ${DIR} | strings | grep "http:"|grep -v "=" >list.txtcount=`cat list.txt|wc -l`if [ "$count" -eq "0" ];then echo -e "---------------------------------\nThe $1 cache already update,Please exit ......" exitfiwhile read linedo $Command -m PURGE -p 80 "$line" >>/dev/null if [ $? -eq 0 ];then echo -e "----------------------------------\nThe $line cache update successfully!" fidone < list.txt
脚本执行:
[root@node2 ~]# sh auto_clean_cache.sh forum.php----------------------------------The http://www.wugk2.com/forum.php cache update successfully![root@node2 ~]#
更多squid优化及深入配置后期更新。。
转载地址:http://jyaxx.baihongyu.com/