高性能緩存服務(wù)器Squid架構(gòu)配置
隨著網(wǎng)站訪問人數(shù)越來越多,承受的并發(fā)和壓力也越來越高,這時候我們需要對網(wǎng)站和架構(gòu)進行優(yōu)化,今天我們來討論使用Squid對架構(gòu)進行優(yōu)化,緩存網(wǎng)站。網(wǎng)上對squid描述的文章也有成千上萬,我這里簡單記錄一下實踐的步驟。
一、實施環(huán)境
- 系統(tǒng)版本:CentOSx86_64 5.8
- Squid版本:squid-2.6
- Nginx版本:nginx-1.4.2
二、正式安裝
安裝之前我們需要對系統(tǒng)進行優(yōu)化,主要優(yōu)化系統(tǒng)內(nèi)核相關(guān)參數(shù),僅供參考:
- #sysctl.conf config 2014-03-26
- net.ipv4.ip_forward = 0
- net.ipv4.conf.default.rp_filter = 1
- net.ipv4.conf.default.accept_source_route = 0
- kernel.sysrq = 0
- kernel.core_uses_pid = 1
- net.ipv4.tcp_syncookies = 1
- kernel.msgmnb = 65536
- kernel.msgmax = 65536
- kernel.shmmax = 68719476736
- kernel.shmall = 4294967296
- net.ipv4.tcp_max_tw_buckets = 10000
- net.ipv4.tcp_sack = 1
- net.ipv4.tcp_window_scaling = 1
- net.ipv4.tcp_rmem = 4096 87380 4194304
- net.ipv4.tcp_wmem = 4096 16384 4194304
- net.core.wmem_default = 8388608
- net.core.rmem_default = 8388608
- net.core.rmem_max = 16777216
- net.core.wmem_max = 16777216
- net.core.netdev_max_backlog = 262144
- net.core.somaxconn = 262144
- net.ipv4.tcp_max_orphans = 3276800
- net.ipv4.tcp_max_syn_backlog = 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_mem = 94500000 915000000 927000000
- net.ipv4.tcp_fin_timeout = 1
- net.ipv4.tcp_keepalive_time = 15
- net.ipv4.ip_local_port_range = 1024 65535
接下來上自動安裝Squid腳本,里面分別配置了兩個虛擬主機域名,前端有LVS,LVS均衡后端多組squid集群,根據(jù)命中率去調(diào)整squid集群的數(shù)量,Squid后端均衡Nginx或者Apache。(完整的架構(gòu)LVS+Keepalived+Squid+Nginx+Resin/Tomcat/PHP+MySQL集群)
簡單邏輯圖如下:
直接上腳本:
- #!/bin/sh
- #Auto make install squid server
- #Author wugk 2014-03-26
- SQUID_CNF=/etc/squid/squid.conf
- CACHE_DIR=(
- /data/cache1
- /data/cache2
- )
- #Install squid shell
- yum install -y squid
- #config squid.conf
- cat >>$SQUID_CNF <<EOF
- #global config squid.conf 2014-03-26
- http_port 80 accel vhost vport
- cache_peer 192.168.149.128 parent 80 0 originserver name=wugk1
- cache_peer 192.168.149.129 parent 80 0 originserver name=wugk2
- cache_peer_domain wugk1 www.wugk1.com
- cache_peer_domain wugk2 www.wugk2.com
- visible_hostname localhost
- forwarded_for off
- via off
- cache_vary on
- #acl config
- acl manager proto cache_object
- acl localhost src 127.0.0.1/32
- acl to_localhost dst 127.0.0.0/8 0.0.0.0/32
- acl localnet src 10.0.0.0/8 # RFC1918 possible internal network
- acl localnet src 172.16.0.0/12 # RFC1918 possible internal network
- acl localnet src 192.168.0.0/16 # RFC1918 possible internal network
- acl SSL_ports port 443
- acl Safe_ports port 80 8080 # http
- acl Safe_ports port 21 # ftp
- acl Safe_ports port 443 # https
- acl all src 0.0.0.0/0
- acl CONNECT method CONNECT
- http_access allow manager localhost
- http_access deny manager
- http_access deny !Safe_ports
- http_access deny CONNECT !SSL_ports
- http_access allow localnet
- http_access allow localhost
- http_access allow all
- acl PURGE method PURGE
- http_access allow PURGE localhost
- http_access deny PURGE
- #squid config 2014-03-25
- cache_dir aufs /data/cache1 10240 16 256
- cache_dir aufs /data/cache2 10240 16 256
- cache_mem 4000 MB
- maximum_object_size 8 MB
- maximum_object_size_in_memory 256 KB
- hierarchy_stoplist cgi-bin ?
- coredump_dir /var/spool/squid
- refresh_pattern ^ftp: 1440 20% 10080
- refresh_pattern ^gopher: 1440 0% 1440
- refresh_pattern -i (/cgi-bin/|\?) 0 0% 0
- refresh_pattern \.(jpg|png|gif|mp3|xml|html|htm|css|js) 1440 50% 2880 ignore-reload
- refresh_pattern . 0 20% 4320
- EOF
- #config cache_dir
- mkdir -p ${CACHE_DIR[@]} ;chown -R squid:squid ${CACHE_DIR[@]}
- #restart squid server
- /etc/init.d/squid restart
- if
- [ "$?" == "0" ];then
- echo "The Squid Server Install Successfully !!"
- else
- echo "The Squid Server Install Failed !!,Please Check Log......"
- fi
***測試,前端LVS截圖(注LVS此處不配置了,博客有專門的安裝方法)
通過瀏覽器查看head頭,緩存命中情況截圖如下:
通過命令
- squidclient -p 80 mgr:info |egrep "(Request Hit Ratios|Byte Hit Ratios)"
查看緩存命中率如下:
三、批量清空緩存
使用Shell腳本批量清空squid緩存腳本auto_clean_cache.sh
- #!/bin/sh
- DIR=/data/cache/
- Command=/usr/sbin/squidclient
- if
- [ "$1" = "" ];then
- echo "Usage:{$0 "\$1" ,Example exec $0 forum.php}"
- exit
- fi
- grep -r -a $1 ${DIR} | strings | grep "http:"|grep -v "=" >list.txt
- count=`cat list.txt|wc -l`
- if
- [ "$count" -eq "0" ];then
- echo -e "---------------------------------\nThe $1 cache already update,Please exit ......"
- exit
- fi
- while read line
- do
- $Command -m PURGE -p 80 "$line" >>/dev/null
- if [ $? -eq 0 ];then
- echo -e "----------------------------------\nThe $line cache update successfully!"
- fi
- done < list.txt
腳本執(zhí)行:
- [root@node2 ~]# sh auto_clean_cache.sh forum.php
- ----------------------------------
- The http://www.wugk2.com/forum.php cache update successfully!
- [root@node2 ~]#
更多squid優(yōu)化及深入配置后期更新。