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

简介:

 前言随着网站访问人数越来越多,承受的并发和压力也越来越高,这时候我们需要对网站和架构进行优化,今天我们来讨论使用Squid对架构进行优化,缓存网站。网上对squid描述的文章也有成千上万,我这里简单记录一下实践的步骤。

一、实施环境

1
2
3
系统版本:CentOSx86_64 5.8
Squid版本:squid-2.6
Nginx版本:nginx-1.4.2

二、正式安装

  安装之前我们需要对系统进行优化,主要优化系统内核相关参数,仅供参考:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
vi  /etc/sysctl .conf
#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

优化Linux文件打开最大数:

1
2
3
4
5
vi   /etc/security/limits .conf 
* soft nproc 65535
* hard nproc 65535
* soft nofile 65535
* hard nofile 65535

接下来上自动安装Squid脚本,里面分别配置了两个虚拟主机域名,前端有LVSLVS均衡后端多组squid集群,根据命中率去调整squid集群的数量,Squid后端均衡Nginx或者Apache。(完整的架构LVS+Keepalived+Squid+Nginx+Resin/Tomcat/PHP+MySQL集群)

简单逻辑图如下:

wKiom1MyeQCSP8bRAAGXIgAao8Q437.jpg


 直接上脚本:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#!/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此处不配置了,博客有专门的安装方法)

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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#!/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

 脚本执行:

1
2
3
4
[root@node2 ~]# sh auto_clean_cache.sh forum.php
----------------------------------
The http: //www.wugk2.com/forum.php cache update successfully!
[root@node2 ~]#

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


本文转自 wgkgood 51CTO博客,原文链接:http://blog.51cto.com/wgkgood/1384580


相关文章
|
6天前
|
消息中间件 安全 Unix
SSH配置多台服务器之间的免密登陆以及登陆别名
SSH配置多台服务器之间的免密登陆以及登陆别名
18 1
|
21天前
|
弹性计算
2024年阿里云服务器不同实例规格与配置实时优惠价格整理与分享
2024年阿里云服务器的优惠价格新鲜出炉,有特惠云服务器也有普通优惠价格,本文为大家整理汇总了2024年阿里云服务器的优惠价格,包含特惠云服务器和其他配置云服务器的优惠价格。以便大家了解自己想购买的云服务器选择不同实例规格和带宽情况下的价格,仅供参考。
2024年阿里云服务器不同实例规格与配置实时优惠价格整理与分享
|
28天前
|
人工智能 运维 监控
构建高性能微服务架构:现代后端开发的挑战与策略构建高效自动化运维系统的关键策略
【2月更文挑战第30天】 随着企业应用的复杂性增加,传统的单体应用架构已经难以满足快速迭代和高可用性的需求。微服务架构作为解决方案,以其服务的细粒度、独立性和弹性而受到青睐。本文将深入探讨如何构建一个高性能的微服务系统,包括关键的设计原则、常用的技术栈选择以及性能优化的最佳实践。我们将分析微服务在处理分布式事务、数据一致性以及服务发现等方面的挑战,并提出相应的解决策略。通过实例分析和案例研究,我们的目标是为后端开发人员提供一套实用的指南,帮助他们构建出既能快速响应市场变化,又能保持高效率和稳定性的微服务系统。 【2月更文挑战第30天】随着信息技术的飞速发展,企业对于信息系统的稳定性和效率要求
|
3天前
|
存储 弹性计算 安全
阿里云服务器2核2G、2核4G配置最新租用收费标准及活动价格参考
2核2G、2核4G配置是很多个人和企业建站以及部署中小型的web应用等场景时首选的云服务器配置,这些配置的租用价格也是用户非常关心的问题,本文为大家整理汇总了2024年阿里云服务器2核2G、2核4G配置不同实例规格及地域之间的收费标准,同时整理了这些配置最新活动价格,以供大家参考和选择。
阿里云服务器2核2G、2核4G配置最新租用收费标准及活动价格参考
|
4天前
|
监控 负载均衡 API
构建高性能微服务架构:后端开发的最佳实践
【4月更文挑战第14天】 在当今快速发展的软件开发领域,微服务架构已成为构建可扩展、灵活且容错的系统的首选方法。本文深入探讨了后端开发人员在设计和维护高性能微服务时需要遵循的一系列最佳实践。我们将从服务划分原则、容器化部署、API网关使用、负载均衡、服务监控与故障恢复等方面展开讨论,并结合实际案例分析如何优化微服务性能及可靠性。通过本文的阅读,读者将获得实施高效微服务架构的实用知识与策略。
|
6天前
|
域名解析 网络协议 应用服务中间件
阿里云服务器配置免费https服务
阿里云服务器配置免费https服务
|
9天前
|
数据采集
robots.txt配置 减小服务器压力
robots.txt配置 减小服务器压力
13 0
|
15天前
|
存储 缓存 NoSQL
Redis 服务器指南:高性能内存数据库的完整使用指南
Redis 服务器指南:高性能内存数据库的完整使用指南
|
20天前
|
网络协议 Linux 网络安全
Linux服务器DNS服务器配置实现bind的正向解释和反向解释
Linux服务器DNS服务器配置实现bind的正向解释和反向解释
17 0
|
21天前
|
弹性计算 网络安全 虚拟化
ECS数据问题之升级配置预防数据丢失如何解决
ECS(Elastic Compute Service,弹性计算服务)是云计算服务提供商提供的一种基础云服务,允许用户在云端获取和配置虚拟服务器。以下是ECS服务使用中的一些常见问题及其解答的合集:

热门文章

最新文章