Chinaunix首页 | 论坛 | 博客
  • 博客访问: 4393893
  • 博文数量: 1214
  • 博客积分: 13195
  • 博客等级: 上将
  • 技术积分: 9105
  • 用 户 组: 普通用户
  • 注册时间: 2007-01-19 14:41
个人简介

C++,python,热爱算法和机器学习

文章分类

全部博文(1214)

文章存档

2021年(13)

2020年(49)

2019年(14)

2018年(27)

2017年(69)

2016年(100)

2015年(106)

2014年(240)

2013年(5)

2012年(193)

2011年(155)

2010年(93)

2009年(62)

2008年(51)

2007年(37)

分类: 系统运维

2016-09-17 13:57:44

原文地址:

随着网站访问人数越来越多,承受的并发和压力也越来越高,这时候我们需要对网站和架构进行优化,今天我们来讨论使用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集群)

简单逻辑图如下:

wKiom1MyeQCSP8bRAAGXIgAao8Q437.jpg

直接上脚本:

  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. #Install squid shell 
  9. yum install -y squid 
  10. #config squid.conf 
  11. cat >>$SQUID_CNF <<EOF 
  12. #global config squid.conf 2014-03-26 
  13. http_port 80 accel vhost vport 
  14. cache_peer 192.168.149.128 parent 80 0 originserver name=wugk1 
  15. cache_peer 192.168.149.129 parent 80 0 originserver name=wugk2 
  16. cache_peer_domain wugk1 
  17. cache_peer_domain wugk2 
  18. visible_hostname localhost 
  19. forwarded_for off 
  20. via off 
  21. cache_vary on 
  22. #acl config 
  23. acl manager proto cache_object 
  24. acl localhost src 127.0.0.1/32 
  25. acl to_localhost dst 127.0.0.0/8 0.0.0.0/32 
  26. acl localnet src 10.0.0.0/8     # RFC1918 possible internal network 
  27. acl localnet src 172.16.0.0/12  # RFC1918 possible internal network 
  28. acl localnet src 192.168.0.0/16 # RFC1918 possible internal network 
  29. acl SSL_ports port 443 
  30. acl Safe_ports port 80 8080         # http 
  31. acl Safe_ports port 21          # ftp 
  32. acl Safe_ports port 443         # https 
  33. acl all src 0.0.0.0/0 
  34. acl CONNECT method CONNECT 
  35. http_access allow manager localhost 
  36. http_access deny manager 
  37. http_access deny !Safe_ports 
  38. http_access deny CONNECT !SSL_ports 
  39. http_access allow localnet 
  40. http_access allow localhost 
  41. http_access allow all 
  42. acl PURGE method PURGE 
  43. http_access allow PURGE localhost 
  44. http_access deny PURGE 
  45. #squid config 2014-03-25 
  46. cache_dir aufs /data/cache1 10240 16 256 
  47. cache_dir aufs /data/cache2 10240 16 256 
  48. cache_mem 4000 MB 
  49. maximum_object_size 8 MB 
  50. maximum_object_size_in_memory 256 KB 
  51. hierarchy_stoplist cgi-bin ? 
  52. coredump_dir /var/spool/squid 
  53. refresh_pattern ^ftp:           1440    20%     10080 
  54. refresh_pattern ^gopher:        1440    0%      1440 
  55. refresh_pattern -i (/cgi-bin/|\?) 0     0%      0 
  56. refresh_pattern \.(jpg|png|gif|mp3|xml|html|htm|css|js) 1440    50%     2880    ignore-reload 
  57. refresh_pattern .               0       20%     4320 
  58. EOF 
  59. #config cache_dir 
  60. mkdir -p  ${CACHE_DIR[@]} ;chown -R squid:squid  ${CACHE_DIR[@]} 
  61. #restart squid server 
  62. /etc/init.d/squid restart 
  63. if 
  64.     [ "$?" == "0" ];then 
  65.     echo "The Squid Server Install Successfully !!" 
  66. else 
  67.     echo "The Squid Server Install Failed !!,Please Check Log......"                                                                                                                                                                          
  68. fi 

最后测试,前端LVS截图(注LVS此处不配置了,博客有专门的安装方法)

wKiom1MyokSjpHrmAAIfiwjj0yc342.jpg

通过浏览器查看head头,缓存命中情况截图如下:

wKiom1MyjuvyPcYVAAJ2smk3I8A715.jpg

通过命令

	
  1. squidclient -p 80 mgr:info |egrep "(Request Hit Ratios|Byte Hit Ratios)" 

查看缓存命中率如下:

wKiom1Mzl4DQS5i0AALkpC-Girk997.jpg

 三、批量清空缓存

使用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 ! 
  4. [root@node2 ~]# 

更多squid优化及深入配置后期更新。

阅读(819) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~