本文只是为了详细了解squid的集群负载均衡的配置而模拟出来的一个实验环境。目的是想深入了解squid反向代理以及squid集群负载均衡的配置。也是对近期squid学习成果的检验,准备的环境比较简单,可能没有实用价值,只是了解其配置思路。

场景1:一家公司要提供互联网服务,为了保证访问性能,采用squid反向代理来实现,来提高性能。想像架构图如下:

1、公司域名www.abc.com,通过DNS轮循的方式,指向A,B两台squid反向代理服务器。

2、A,B squid反向代理服务器的环境如下,他们分别代理内网的两台web服务器(图中的web1,web2), 实现高可用性与负载均衡,

1A:squid反向代理服务器
2IP:192.168.129.21/24(外网ip)
3   192.168.1.1(内网ip)
4
5B:squid反向代理服务器
6IP:192.168.129.22/24(外网ip)
7   192.168.1.2(内网ip)

web1: IP:192.168.1.20

web2: IP:192.168.1.30

squid反向代理负载均衡配置,分别在A,B上安装Squid,配置好相关环境。

1)A服务器squid配置如下:

 1http_port 192.168.129.21:80 vhost     /*监听本机所有接口*/
 2# 缓存目录大小
 3# 注意: 硬盘上的cache空间路径需要手动创建
 4cache_mem 64 MB
 5cache_dir ufs /var/spool/squid 4096 16 256
 6# squid的工作进程采用哪个用户和用户组来启动
 7cache_effective_user squid
 8cache_effective_group squid
 9dns_nameservers 202.103.24.68
10# squid的日志存放路径
11cache_access_log /var/log/squid/access.log
12cache_log /var/log/squid/cache.log
13cache_store_log /var/log/squid/store.log
14# 设置可见域名
15visible_hostname 192.168.1.1
16# 配置管理员邮箱
17cache_mgr abc@126.com
18#将icp通讯端口设置为3130,squid之间缓存同步查询数据时用的。
19icp_port 3130
20#A反向代理的内部服务器(适用于squid 2.6)
21cache_peer 192.168.1.20 parent 80 0 no-query originserver no-digest name=a
22cache_peer 192.168.1.30 parent 80 0 no-query originserver no-digest name=b
23#连接本地的与另一台Squid工作在sibling模式,实现数据的调取(用于jsquid集群环境)
24cache_peer 192.168.129.21 sibling 80 3130 name=cache1
25cache_peer 192.168.129.22 sibling 80 3130 name=cache2
26acl Safe_ports port 80
27acl Safe_ports port 3130
28cache_peer_domain a www.abc.com
29cache_peer_domain b www.abc.com
30# 配置squid访问控制权限
31acl all src 0.0.0.0/0.0.0.0
32http_access allow all
33# 告诉squid缓存哪些文件
34acl QUERY urlpath_regex .html .jpg .css .js .png .htm .ico .jpeg .bmp .txt .json
35cache allow QUERY
36cache_peer_access a allow all
37cache_peer_access b allow all
  1. B服务器squid配置如下:
 1#监听本机所有接口
 2http_port 192.168.129.22:80 vhost
 3cache_mem 64 MB
 4cache_dir ufs /var/spool/squid 4096 16 256
 5cache_effective_user squid
 6cache_effective_group squid
 7dns_nameservers 202.103.24.68
 8cache_access_log /var/log/squid/access.log
 9cache_log /var/log/squid/cache.log
10cache_store_log /var/log/squid/store.log
11visible_hostname 192.168.1.2
12cache_mgr abc@126.com
13icp_port 3130
14cache_peer 192.168.1.20 parent 80 0 no-query originserver no-digest name=a
15cache_peer 192.168.1.30 parent 80 0 no-query originserver no-digest name=b
16cache_peer 192.168.129.21 sibling 80 3130 name=cache1
17cache_peer 192.168.129.22 sibling 80 3130 name=cache2
18acl Safe_ports port 80
19acl Safe_ports port 3130
20cache_peer_domain a www.abc.com
21cache_peer_domain b www.abc.com
22acl all src 0.0.0.0/0.0.0.0
23http_access allow all
24# 告诉squid缓存哪些文件
25acl QUERY urlpath_regex .html .jpg .css .js .png .htm .ico .jpeg .bmp .txt .json
26cache allow QUERY
27cache_peer_access a allow all
28cache_peer_access b allow all

场景2

squid1和squid2是姐妹关系,squid和squid1,squid2是父子关系,squid与web也是父子关系.

[IP分配信息]

1squid 192.168.5.163  
2squid1 192.168.5.161  
3squid2 192.168.5.165  
4web 192.168.5.162  

根据上面的案例,在实际应用中可以实际各种不同应用。

squid1的配置

1http_port 80 accel vhost vport
2icp_port 3130
3cache_peer 192.168.5.161 parent 80 0 no-query originserver no-digest name=cache0
4cache_peer 192.168.5.161 sibling 80 3130 name=cache1
5cache_peer 192.168.5.163 sibling 80 3130 name=cache2
6cache_peer 192.168.5.165 sibling 80 3130 name=cache3
7cache_peer_domain cache0 www.squid.com
8acl Safe_ports port 80
9acl Safe_ports port 3130

squid2 的配置

1http_port 80 accel vhost vport
2icp_port 3130
3cache_peer 192.168.5.165 parent 80 0 no-query originserver no-digest name=cache0
4cache_peer 192.168.5.161 sibling 80 3130 name=cache1
5cache_peer 192.168.5.163 sibling 80 3130 name=cache2
6cache_peer 192.168.5.165 sibling 80 3130 name=cache3
7cache_peer_domain cache0 www.squid.com
8acl Safe_ports port 80
9acl Safe_ports port 3130

squid的配置:

1http_port 80 accel vhost vport
2icp_port 3130
3cache_peer 192.168.5.163 parent 80 0 no-query originserver no-digest name=cache0
4cache_peer 192.168.5.161 sibling 80 3130 name=cache1
5cache_peer 192.168.5.163 sibling 80 3130 name=cache2
6cache_peer 192.168.5.165 sibling 80 3130 name=cache3
7cache_peer_domain cache0 www.squid.com
8acl Safe_ports port 80
9acl Safe_ports port 3130