本文只是为了详细了解squid的集群负载均衡的配置而模拟出来的一个实验环境。目的是想深入了解squid反向代理以及squid集群负载均衡的配置。也是对近期squid学习成果的检验,准备的环境比较简单,可能没有实用价值,只是了解其配置思路。
场景1
一家公司要提供互联网服务,为了保证访问性能,采用squid反向代理来实现,来提高性能。想像架构图如下:
<a href="http://static.oschina.net/uploads/space/2012/0213/131237_7Jmb_103999.jpg" target="_blank" rel="noopener"><img src="https://www.361way.com/wp-content/uploads/2012/11/squidLoadbalance1.jpg" width="478" height="342" alt="" /></a>
<br />
1、公司域名www.abc.com,通过DNS轮循的方式,指向A,B两台squid反向代理服务器。
2、A,B squid反向代理服务器的环境如下,他们分别代理内网的两台web服务器(图中的web1,web2), 实现高可用性与负载均衡,
A:squid反向代理服务器
IP:192.168.129.21/24(外网ip)
192.168.1.1(内网ip)
B:squid反向代理服务器
IP:192.168.129.22/24(外网ip)
192.168.1.2(内网ip)
web1:
IP:192.168.1.20
web2:
IP:192.168.1.30
squid反向代理负载均衡配置
分别在A,B上安装Squid,配置好相关环境。
1)A服务器squid配置如下:
http_port 192.168.129.21:80 vhost /*监听本机所有接口*/ # 缓存目录大小 # 注意: 硬盘上的cache空间路径需要手动创建 cache_mem 64 MB cache_dir ufs /var/spool/squid 4096 16 256 # squid的工作进程采用哪个用户和用户组来启动 cache_effective_user squid cache_effective_group squid dns_nameservers 202.103.24.68 # squid的日志存放路径 cache_access_log /var/log/squid/access.log cache_log /var/log/squid/cache.log cache_store_log /var/log/squid/store.log # 设置可见域名 visible_hostname 192.168.1.1 # 配置管理员邮箱 cache_mgr abc@126.com #将icp通讯端口设置为3130,squid之间缓存同步查询数据时用的。 icp_port 3130 #A反向代理的内部服务器(适用于squid 2.6) cache_peer 192.168.1.20 parent 80 0 no-query originserver no-digest name=a cache_peer 192.168.1.30 parent 80 0 no-query originserver no-digest name=b #连接本地的与另一台Squid工作在sibling模式,实现数据的调取(用于jsquid集群环境) cache_peer 192.168.129.21 sibling 80 3130 name=cache1 cache_peer 192.168.129.22 sibling 80 3130 name=cache2 acl Safe_ports port 80 acl Safe_ports port 3130 cache_peer_domain a www.abc.com cache_peer_domain b www.abc.com # 配置squid访问控制权限 acl all src 0.0.0.0/0.0.0.0 http_access allow all # 告诉squid缓存哪些文件 acl QUERY urlpath_regex .html .jpg .css .js .png .htm .ico .jpeg .bmp .txt .json cache allow QUERY cache_peer_access a allow all cache_peer_access b allow all
<br />
2) B服务器squid配置如下:
#监听本机所有接口 http_port 192.168.129.22:80 vhost cache_mem 64 MB cache_dir ufs /var/spool/squid 4096 16 256 cache_effective_user squid cache_effective_group squid dns_nameservers 202.103.24.68 cache_access_log /var/log/squid/access.log cache_log /var/log/squid/cache.log cache_store_log /var/log/squid/store.log visible_hostname 192.168.1.2 cache_mgr abc@126.com icp_port 3130 cache_peer 192.168.1.20 parent 80 0 no-query originserver no-digest name=a cache_peer 192.168.1.30 parent 80 0 no-query originserver no-digest name=b cache_peer 192.168.129.21 sibling 80 3130 name=cache1 cache_peer 192.168.129.22 sibling 80 3130 name=cache2 acl Safe_ports port 80 acl Safe_ports port 3130 cache_peer_domain a www.abc.com cache_peer_domain b www.abc.com acl all src 0.0.0.0/0.0.0.0 http_access allow all # 告诉squid缓存哪些文件 acl QUERY urlpath_regex .html .jpg .css .js .png .htm .ico .jpeg .bmp .txt .json cache allow QUERY cache_peer_access a allow all cache_peer_access b allow all
<br />
场景2
<a href="http://static.oschina.net/uploads/space/2012/0213/132220_TO84_103999.jpg" target="_blank" rel="noopener"><img src="https://www.361way.com/wp-content/uploads/2012/11/squidLoadbalance2.jpg" width="478" height="204" alt="" /></a>
squid1和squid2是姐妹关系,squid和squid1,squid2是父子关系,squid与web也是父子关系.
[IP分配信息]<br />
squid 192.168.5.163
squid1 192.168.5.161
squid2 192.168.5.165
web 192.168.5.162
根据上面的案例,在实际应用中可以实际各种不同应用。
squid1的配置
http_port 80 accel vhost vport icp_port 3130 cache_peer 192.168.5.161 parent 80 0 no-query originserver no-digest name=cache0 cache_peer 192.168.5.161 sibling 80 3130 name=cache1 cache_peer 192.168.5.163 sibling 80 3130 name=cache2 cache_peer 192.168.5.165 sibling 80 3130 name=cache3 cache_peer_domain cache0 www.squid.com acl Safe_ports port 80 acl Safe_ports port 3130
squid2 的配置
http_port 80 accel vhost vport icp_port 3130 cache_peer 192.168.5.165 parent 80 0 no-query originserver no-digest name=cache0 cache_peer 192.168.5.161 sibling 80 3130 name=cache1 cache_peer 192.168.5.163 sibling 80 3130 name=cache2 cache_peer 192.168.5.165 sibling 80 3130 name=cache3 cache_peer_domain cache0 www.squid.com acl Safe_ports port 80 acl Safe_ports port 3130
squid的配置:
http_port 80 accel vhost vport icp_port 3130 cache_peer 192.168.5.163 parent 80 0 no-query originserver no-digest name=cache0 cache_peer 192.168.5.161 sibling 80 3130 name=cache1 cache_peer 192.168.5.163 sibling 80 3130 name=cache2 cache_peer 192.168.5.165 sibling 80 3130 name=cache3 cache_peer_domain cache0 www.squid.com acl Safe_ports port 80 acl Safe_ports port 3130