六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 429|回复: 0

高性能缓存服务器Squid架构配置

[复制链接]
 楼主| 发表于 2015-4-26 23:47:48 | 显示全部楼层 |阅读模式
高性能缓存服务器Squid架构配置
随着网站访问人数越来越多,承受的并发和压力也越来越高,这时候我们需要对网站和架构进行优化,今天我们来讨论使用Squid对架构进行优化,缓存网站。网上对squid描述的文章也有成千上万,我这里简单记录一下实践的步骤。
一、实施环境
  • 系统版本:CentOSx86_64 5.8
  • Squid版本:squid-2.6
  • Nginx版本:nginx-1.4.2
二、正式安装
安装之前我们需要对系统进行优化,主要优化系统内核相关参数,仅供参考:
  1.     #sysctl.conf config 2014-03-26
  2.     net.ipv4.ip_forward = 0
  3.     net.ipv4.conf.default.rp_filter = 1
  4.     net.ipv4.conf.default.accept_source_route = 0
  5.     kernel.sysrq = 0
  6.     kernel.core_uses_pid = 1
  7.     net.ipv4.tcp_syncookies = 1
  8.     kernel.msgmnb = 65536
  9.     kernel.msgmax = 65536
  10.     kernel.shmmax = 68719476736
  11.     kernel.shmall = 4294967296
  12.     net.ipv4.tcp_max_tw_buckets = 10000
  13.     net.ipv4.tcp_sack = 1
  14.     net.ipv4.tcp_window_scaling = 1
  15.     net.ipv4.tcp_rmem = 4096        87380   4194304
  16.     net.ipv4.tcp_wmem = 4096        16384   4194304
  17.     net.core.wmem_default = 8388608
  18.     net.core.rmem_default = 8388608
  19.     net.core.rmem_max = 16777216
  20.     net.core.wmem_max = 16777216
  21.     net.core.netdev_max_backlog = 262144
  22.     net.core.somaxconn = 262144
  23.     net.ipv4.tcp_max_orphans = 3276800
  24.     net.ipv4.tcp_max_syn_backlog = 262144
  25.     net.ipv4.tcp_timestamps = 0
  26.     net.ipv4.tcp_synack_retries = 1
  27.     net.ipv4.tcp_syn_retries = 1
  28.     net.ipv4.tcp_tw_recycle = 1
  29.     net.ipv4.tcp_tw_reuse = 1
  30.     net.ipv4.tcp_mem = 94500000 915000000 927000000
  31.     net.ipv4.tcp_fin_timeout = 1
  32.     net.ipv4.tcp_keepalive_time = 15
  33.     net.ipv4.ip_local_port_range = 1024    65535
复制代码
接下来上自动安装Squid脚本,里面分别配置了两个虚拟主机域名,前端有LVS,LVS均衡后端多组squid集群,根据命中率去调整squid集群的数量,Squid后端均衡Nginx或者Apache。(完整的架构LVS+Keepalived+Squid+Nginx+Resin/Tomcat/PHP+MySQL集群)
简单逻辑图如下:
直接上脚本:
  1.     #!/bin/sh
  2.     #Auto make install squid server
  3.     #Author wugk 2014-03-26
  4.     SQUID_CNF=/etc/squid/squid.conf
  5.     CACHE_DIR=(
  6.         /data/cache1
  7.         /data/cache2
  8.     )
  9.     #Install squid shell
  10.     yum install -y squid
  11.     #config squid.conf
  12.     cat >>$SQUID_CNF <<EOF
  13.     #global config squid.conf 2014-03-26
  14.     http_port 80 accel vhost vport
  15.     cache_peer 192.168.149.128 parent 80 0 originserver name=wugk1
  16.     cache_peer 192.168.149.129 parent 80 0 originserver name=wugk2
  17.     cache_peer_domain wugk1 www.wugk1.com
  18.     cache_peer_domain wugk2 www.wugk2.com
  19.     visible_hostname localhost
  20.     forwarded_for off
  21.     via off
  22.     cache_vary on
  23.     #acl config
  24.     acl manager proto cache_object
  25.     acl localhost src 127.0.0.1/32
  26.     acl to_localhost dst 127.0.0.0/8 0.0.0.0/32
  27.     acl localnet src 10.0.0.0/8     # RFC1918 possible internal network
  28.     acl localnet src 172.16.0.0/12  # RFC1918 possible internal network
  29.     acl localnet src 192.168.0.0/16 # RFC1918 possible internal network
  30.     acl SSL_ports port 443
  31.     acl Safe_ports port 80 8080         # http
  32.     acl Safe_ports port 21          # ftp
  33.     acl Safe_ports port 443         # https
  34.     acl all src 0.0.0.0/0
  35.     acl CONNECT method CONNECT
  36.     http_access allow manager localhost
  37.     http_access deny manager
  38.     http_access deny !Safe_ports
  39.     http_access deny CONNECT !SSL_ports
  40.     http_access allow localnet
  41.     http_access allow localhost
  42.     http_access allow all
  43.     acl PURGE method PURGE
  44.     http_access allow PURGE localhost
  45.     http_access deny PURGE
  46.     #squid config 2014-03-25
  47.     cache_dir aufs /data/cache1 10240 16 256
  48.     cache_dir aufs /data/cache2 10240 16 256
  49.     cache_mem 4000 MB
  50.     maximum_object_size 8 MB
  51.     maximum_object_size_in_memory 256 KB
  52.     hierarchy_stoplist cgi-bin ?
  53.     coredump_dir /var/spool/squid
  54.     refresh_pattern ^ftp:           1440    20%     10080
  55.     refresh_pattern ^gopher:        1440    0%      1440
  56.     refresh_pattern -i (/cgi-bin/|\?) 0     0%      0
  57.     refresh_pattern \.(jpg|png|gif|mp3|xml|html|htm|css|js) 1440    50%     2880    ignore-reload
  58.     refresh_pattern .               0       20%     4320
  59.     EOF
  60.     #config cache_dir
  61.     mkdir -p  ${CACHE_DIR[@]} ;chown -R squid:squid  ${CACHE_DIR[@]}
  62.     #restart squid server
  63.     /etc/init.d/squid restart
  64.     if
  65.         [ "$?" == "0" ];then
  66.         echo "The Squid Server Install Successfully !!"
  67.     else
  68.         echo "The Squid Server Install Failed !!,Please Check Log......"                                                                                                                                                                          
  69.     fi
复制代码
最后测试,前端LVS截图(注LVS此处不配置了,博客有专门的安装方法)
通过浏览器查看head头,缓存命中情况截图如下:
通过命令
  • squidclient -p 80 mgr:info |egrep "(Request Hit Ratios|Byte Hit Ratios)"

查看缓存命中率如下:
三、批量清空缓存
使用Shell脚本批量清空squid缓存脚本auto_clean_cache.sh
  1.     #!/bin/sh
  2.     DIR=/data/cache/
  3.     Command=/usr/sbin/squidclient
  4.     if
  5.             [ "$1" = "" ];then
  6.             echo "Usage:{$0 "\$1" ,Example exec $0 forum.php}"
  7.             exit
  8.     fi
  9.     grep -r -a $1 ${DIR} | strings | grep "http:"|grep -v "=" >list.txt
  10.     count=`cat list.txt|wc -l`
  11.     if
  12.             [ "$count" -eq "0" ];then
  13.             echo -e "---------------------------------\nThe $1 cache already update,Please exit ......"   
  14.             exit
  15.     fi
  16.     while read line
  17.     do
  18.             $Command -m PURGE -p 80 "$line" >>/dev/null
  19.             if [ $? -eq 0 ];then
  20.             echo -e "----------------------------------\nThe $line cache update successfully!"
  21.             fi
  22.     done < list.txt
复制代码
脚本执行:
  1.     [root@node2 ~]# sh auto_clean_cache.sh forum.php
  2.     ----------------------------------
  3.     The http://www.wugk2.com/forum.php cache update successfully!
  4.     [root@node2 ~]#
复制代码
更多squid优化及深入配置后期更新。


http://os.51cto.com/art/201404/435129.htm


高性能缓存服务器Squid架构配置

该会员没有填写今日想说内容.
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表