关于ACL的知识,大家可以自行百度查阅,其他更多高级选项,请参考官方文档:http://www.squid-cache.org/Doc/config/。
注意:squid2.0 和squid3.0的差别还是很大的,如果配置完,启动squid不正确,请大家多多参考官方文档的相应版本说明。
五、普通代理服务
即标准的、传统的代理服务,需要客户机在浏览器中指定代理服务器的地址、端口。
实验拓扑图如下:
5.1 配置Squid 代理服务器IP地址
将eth1的IP地址修改为200.168.10.1
# ifconfig eth1 200.168.10.1
5.2 编辑squid 主配置文件/etc/squid/squid.conf
http_port 3128 cache_mem 64 MB
maximum_object_size 4 MB
cache_dir ufs /var/spool/squid 100 16 256 access_log /var/log/squid/access.log
acl localnet src 192.168.1.0/24 http_access allow localnet
http_access deny all
visible_hostname squid.david.dev
cache_mgr mchina_tang@qq.com
5.3 初始化
# squid –z
5.4 启动Squid
# /etc/init.d/squid start
5.5 配置Web 服务器
A. 安装Apache
# rpm -qa|grep httpd
# yum -y install httpd
B. 启动Apache并加入开机启动
# /etc/init.d/httpd start
# chkconfig httpd on
C. 创建index.html
# echo "
Squid-Web1/200.168.10.2
" > /var/www/html/index.html
D. 修改Web服务器IP地址
将web服务器的IP地址修改为200.168.10.2
# ifconfig eth0 200.168.10.2
5.6 配置客户端IP地址
5.7 配置浏览器代理
打开浏览器(以IE为例,其他类似),菜单栏 -> 工具 -> Internet 选项 -> 连接 -> 局域网设置 -> 代理服务器,按照以下格式设置。
5.8 测试
测试成功。
5.9 测试错误页面
在Oracle VM VirtualBox里,因为上面手动设置了ip地址,导致外网不能访问,刚好可以测试访问出错的页面。
可以看到在squid 配置文件里设置的参数在错误页面里的显示。
六、透明代理服务
适用于企业的网关主机,客户机不需要指定代理服务器地址、端口等信息,通过iptables将客户机的Web访问数据转交给代理服务程序处理。
实验拓扑图如下:
6.1 修改squid 主配置文件/etc/squid/squid.conf
http_port 3128 transparent cache_mem 64 MB
maximum_object_size 4 MB
cache_dir ufs /var/spool/squid 100 16 256
access_log /var/log/squid/access.log
acl localnet src 192.168.1.0/24
http_access allow localnet
http_access deny all
visible_hostname squid.david.dev
cache_mgr mchina_tang@qq.com
在http_port 3128 后添加transparent 关键字。
6.2 reload
reload 让上面的配置生效。
# /etc/init.d/squid reload
6.3 添加iptables规则,把内部的http请求重定向到3128端口
A. 启动iptables 服务
# /etc/init.d/iptables start
B. 清除现有iptables filter 表规则
# iptables -F
C. 保存iptables 设置
# /etc/init.d/iptables save
D. 查看nat 表设置
# iptables -t nat -L -n
E. 在nat表中新增一条规则
# iptables -t nat -I PREROUTING -i eth0 -s 192.168.1.0/24 -p tcp --dport 80 -j REDIRECT --to-port 3128
F. 保存
G. 设置iptables 开机启动
# chkconfig iptables on
6.4 修改客户端IP地址
将默认网关设置为squid 服务器的内网ip地址。
6.5 在浏览器中,取消代理设置
6.6 测试
透明代理测试成功。
七、反向代理服务
为Internet用户访问企业Web站点提供缓存加速。
实验拓扑图如下:
7.1 关闭防火墙
# /etc/init.d/iptables stop
7.2 修改Web Server 主页
Web1:
# echo "
Squid-Web1/192.168.1.18
" > /var/www/html/index.html
Web2:
# echo "
Squid-Web1/192.168.1.19
" > /var/www/html/index.html
7.3 配置squid
http_port 80 accel vhost
http_access allow all
cache_peer 192.168.1.18 parent 80 0 originserver round-robin weight=1 cache_peer 192.168.1.19 parent 80 0 originserver round-robin weight=1 visible_hostname squid.david.dev
cache_mgr mchina_tang@qq.com
7.4 启动Squid服务
squid启动失败,因为上面设定了squid的监听端口是80,和系统的http服务冲突,所以要将http服务停掉。
7.5 squid 启动失败,将系统开机自启动的apache服务关闭。
7.6 测试
squid 采用了round-robin,所以客户端的访问将轮询两台web服务器,采用 "Ctrl + F5" 来深度刷新测试。
Web1:
Web2:
查看squid 的访问日志。
八、实际应用
下面实验将模拟通过不同的域名访问不同的机器,简单实现企业应用中的负载均衡。客户端在浏览器地址栏中输入www.squid.dev,将访问192.168.1.18这台机器,访问bbs.squid.dev,将访问192.168.1.19这台机器。
实验拓扑图如下:
8.1 修改Web Server 主页
Web1:
# echo "
www.squid.dev/192.168.1.18
" > /var/www/html/index.html
Web2:
# echo "
bbs.squid.dev/192.168.1.19
" > /var/www/html/index.html
8.2 配置Squid
http_port 80 accel vhost
http_access allow all
cache_peer 192.168.1.18 parent 80 0 originserver name=www
cache_peer 192.168.1.19 parent 80 0 originserver name=bbs
cache_peer_domain www www.squid.dev
cache_peer_domain bbs bbs.squid.dev
visible_hostname squid.david.dev
cache_mgr mchina_tang@qq.com
8.3 配置客户端
这里可以使用DNS服务来解析,这里我们为了方便,就在hosts 文件里直接指定。
修改C:\Windows\System32\drivers\etc\hosts 文件
8.4 测试网络情况
8.5 测试www.squid.dev
8.6 测试bbs.squid.dev
8.7 查看squid 访问日志
8.8 查看两台服务器的apache 访问日志
# tailf /var/log/httpd/access.log
测试成功。