『1』CDN:内容分发网络,避开网络上影响数据传输速度和稳定的环节,使传输更快更稳定。
实验原理:
client -->虚拟机1 --> 虚拟机2,用户通过虚拟机1去访问虚拟机2,如果虚拟机1有客户需要的资源,则返回给客户,若没有,则去虚拟机2去取资源,然后存入虚拟机1,这样提高了访问速度,降低了虚拟机2的压力。
实验步骤:
虚拟机1:
1、yum install gcc varnish-3.0.5-1.el6.x86_64.rpm varnish-libs-3.0.5-1.el6.x86_64.rpm -y #安装软件
2、vim /etc/sysconfig/varnish
60 VARNISH_LISTEN_PORT=80 #更改默认端口
95 VARNISH_TTL=120 #缓存保留时间
3、vim /etc/varnish/default.vcl
backend default { #配置后端服务器
.host = "172.25.34.2"; #设置为虚拟机2的ip地址
.port = "80";
}
sub vcl_deliver { #查看缓存命中情况
if (obj.hits > 0) {
set resp.http.X-Cache = "HIT from westos cache";
}
else {
set resp.http.X-Cache = "MISS from westos cache";
}
return (deliver);
}
4、/etc/init.d/varnish restart #重启服务
虚拟机2:
1、yum install httpd -y
2、写个html文件并发布
物理机:
1、vim /etc/hosts
虚拟机1ip www.iop.com #作解析
测试:
(1)在物理机中 ping www.iop.com #会显示虚拟机1的IP
(2)在物理机中 curl www.iop.com #可看到server2中提供的信息
(3)在物理机中 curl -I www.iop.com #可观察到是否抓取到对象,同时能看到缓存的存在时间
###################
虚拟机1:
通过 varnishadm 手动清除缓存
varnishadm ban.url .*$ #清除所有
vanishadm ban.url /index.html #清除 index.html 页面缓存
varnishadm ban.url /admin/$ #清除 admin 目录缓存
###################
『2』冗余备份与对用多台不同服务器的转轮调度:
虚拟机1:
1、vim /etc/varinsh/default.vcl
backend web1 {
.host = "172.25.34.2";
.port = "80";
}
backend web2 {
.host = "172.25.34.3";
.port = "80";
}
director lb round-robin{ #轮转调度算法
{.backend = web1;}
{.backend = web2;}
}
sub vcl_recv { #控制接收
if (req.http.host ~ "^(www.)?xinhao.com") {
set req.http.host = "www.xinhao.com";
set req.backend = lb;
return (pass); #不做缓存,正式不会这么做,此处方便测试
} elsif (req.http.host ~ "^bbs.xinhao.com") {
set req.backend = web2;
} else {error 404 "westos cache";
}
}
重启varnish服务
虚拟机二不用进行操作
虚拟机3:
1、安装http服务
2、修改配置文件,设置虚拟主机:
vim /etc/httpd/conf/httpd.conf
<VirtualHost *:80>
DocumentRoot /var/www/html
ServerName bbs.xinhao.com
</VirtualHost>
<VirtualHost *:80>
DocumentRoot /www
ServerName www.xinhao.com
</VirtualHost>
3、mkdir /www
4、vim /www/index.html
5、/etc/init.d/httpd restart
物理机:对新添加的bbs.iop.com进行解析
测试:
(1)物理机中多次 curl www.iop.com
(2)物理机中 curl bbs.iop.com