Cdn

Cdn是一个代理缓存服务器。其实就是减少网络中冗余数据的重复传输。Cdn就是将网站内容发布到最接近用户的网络“边缘”,使用户可以就近快速的取得自己想要的内容。在cdn整体部署的时候包括分布式存储,负载均衡,网络重定向。

传统的网络访问方式

wKioL1eoBfWBf4ZFAAA5zKoz-3M619.png

Cdn加速

cdn加速_第1张图片

Server2  varnish  关闭防火墙,selinux时disabled状态

Server3  apache

Server4  apache

[root@server2 ~]#yum localinstall -yvarnish-3.0.3-1.el6.x86_64.rpm varnish-libs-3.0.3-1.el6.x86_64.rpm

[root@server2 ~]# vim/etc/varnish/default.vcl  ###### 配置一个后端服务器

backend default {

 .host = "172.25.137.3";    #########后端的server的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);

}

[root@server2 ~]# vim/etc/sysconfig/varnish  ####配置varniash服务器监听端口

VARNISH_LISTEN_PORT=80     ########66行,varnish监听的端口

VARNISH_TTL=60               ########缓存的时间    

[root@server2 ~]# /etc/init.d/varnish start

[root@server3 ~]# /etc/init.d/httpd start

[root@server3 html]# vim/var/www/html/index.html  给server3做一个测试页

server3.example.com

浏览器测试页,当访问server2,其实时时访问的后端的server3

cdn加速_第2张图片

[root@foundation137cdn]# curl -I 172.25.137.2  测试缓存

HTTP/1.1200 OK

Server:Apache/2.2.15 (Red Hat)

Last-Modified:Fri, 05 Aug 2016 10:20:17 GMT

ETag:"2087-14-5395069bc66f6"

 

Content-Type:text/html; charset=UTF-8

Content-Length:20

Accept-Ranges:bytes

Date:Fri, 05 Aug 2016 12:51:41 GMT

X-Varnish:565160603

Age:0

Via:1.1 varnish

Connection:keep-alive

X-Cache:MISS from westos cache

 

[root@server2varnish]# varnishadm ban.url .*$ 清空缓存

[root@server2varnish]# varnishadm ban.url /index.html 清空页面缓存

定义多个不同域名站点的后端服务器

 

 

 backend web1 {

    .host = "172.25.137.3";

    .port = "80";

  }

 backend web2 {

    .host = "172.25.137.4";

    .port = "80";

  }

当访问www.westos.org域名时从web1上取数据,访问bbs.redhat.org域名时到web2上取数据,其他报错

subvcl_recv {

        if (req.http.host ~"^(www.)?westos.org") {

                set req.http.host ="www.westos.org";

                set req.backend = web1;

}elsif (req.http.host ~ "^bbs.redhat.org") {

        set req.backend = web2;

        } else {

        error 404 "westos cache";

        }

}

[root@server2varnish]# /etc/init.d/varnish reload

物理界解析

[root@foundation137~]# vim /etc/hosts

wKioL1eoBn3BfNk6AAAbt81w-Ms726.png

cdn加速_第3张图片

cdn加速_第4张图片

[root@server2~]# vim /etc/varnish/default.vcl

directorIB round-robin{            #######把多个后端聚合为一个组,并检测后端健康状态

{.backend = web1; }

{.backend = web2; }

}

 

subvcl_recv {

        if (req.http.host ~"^(www.)?westos.org") {

                set req.http.host ="www.westos.org";

                set req.backend = IB;         ####后端组      

                return (pass);                ####为了测试方便,不进行缓存

}elsif (req.http.host ~ "^bbs.redhat.org") {

        set req.backend = web2;

        } else {

        error 404 "westos cache";

        }

}

 

[root@server2~]# /etc/init.d/varnish reload

 

[root@server4html]# vim /etc/httpd/conf/httpd.conf

wKiom1eoB3bANR5fAAAIrdcNoK8415.png

cdn加速_第5张图片

[root@server4html]# /etc/init.d/httpd restart  

cdn加速_第6张图片

wKiom1eoB8zTNSNKAAASngQpI-0240.png

[root@server2~]# varnishadm ban.url .*$     清空缓存

 

 

 

#####varniashcdn推送平台 (推送主要是清空缓存)

需要安装php ,apache

[root@server2~]# yum install -y httpd -y

[root@server2~]# yum install php -y

[root@server2~]# yum install unzip

[root@server2~]# unzip bansys.zip -d /var/www/html/

[root@server2html]# cd bansys/

[root@server2bansys]# mv * ..

[root@server2html]# vim config.php  文件中的多于的可以注释

cdn加速_第7张图片

对于群组的定义值有定义了群组了才能推送(推送是因为当后端服务器内容更新后,cdn里有健康检查,会进行内容推送) 

 

[root@server2html]# vim /etc/httpd/conf/httpd.conf 修改apache监听的端口,因为varnish监听的端口是80,把apache监听端口改为8080

[root@server2html]# /etc/init.d/httpd start

 

aclwestos {                  ###########设置控制访问

"127.0.0.1";

"172.25.0.0"/24;

}

backendweb1 {

  .host = "172.25.137.3";

  .port = "80";

}

backendweb2 {

  .host = "172.25.137.4";

  .port = "80";

}

 

directorIB round-robin{

{.backend = web1; }

{.backend = web2; }

}

subvcl_deliver {

        if (obj.hits > 0) {

                set resp.http.X-Cache = "HITfrom westos cache";

}

        else {

                set resp.http.X-Cache ="MISS from westos cache";

}

return(deliver);

}

 

subvcl_recv {

        if (req.http.host ~"^(www.)?westos.org") {

                set req.http.host ="www.westos.org";

                set req.backend = IB;

                return (pass);

}elsif (req.http.host ~ "^bbs.redhat.org") {

        set req.backend = web2;

        } else {

        error 404 "westos cache";

        }

    if (req.request == "BAN") {

                if (!client.ip ~ westos) {

                        error 405 "Notallowed.";

                }

                ban("req.url ~ " +req.url);

                error 200 "banadded";

        }

}

[root@server2html]# /etc/init.d/varnish reload

cdn加速_第8张图片

Bansys有两种工作模式,分别是:telnet和http

telnet模式需要关闭 varnish 服务管理端口的验证,注释掉/etc/sysconfig/varnish 文件中的“-S $

{VARNISH_SECRET_FILE}”这行,重启 varnish 服务即可。此实验中是用的http方式

cdn加速_第9张图片

cdn加速_第10张图片