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

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

  简单逻辑图如下:

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 www.wugk1.com 
  17. cache_peer_domain wugk2 www.wugk2.com 
  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 http://www.wugk2.com/forum.php cache update successfully! 
  4. [root@node2 ~]# 

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

责任编辑:黄丹 来源: 51TCO博客
相关推荐

2020-06-02 14:57:06

Linux服务器架构

2019-07-31 14:36:46

Linux服务器框架

2019-07-10 15:10:14

高性能服务器架构

2009-11-17 14:05:57

微软高性能计算服务器

2012-09-18 09:55:28

2021-09-22 16:25:17

服务器戴尔科技集团

2011-04-07 13:39:24

WebHTTP

2012-04-02 17:27:06

rhelsquid

2020-08-02 15:00:40

SquidSSH系统运维

2021-08-10 11:05:09

服务器缓存 架构

2009-02-18 12:45:00

2011-07-06 10:42:55

FlashSoft高速缓存SSD

2010-05-19 10:31:07

IIS服务器

2011-03-11 09:51:47

Java NIO

2010-05-07 17:50:31

Unix服务器

2019-01-15 10:54:03

高性能ServerReactor

2017-12-20 14:59:44

服务器

2021-05-28 05:18:08

PHP语言roadrunnner

2012-11-08 09:57:29

低功耗高性能服务器处理器

2023-10-18 07:52:48

Nginx静态资源服务器
点赞
收藏

51CTO技术栈公众号