实验主机:varnish    172.25.44.1

                    server1    172.25.44.10

                    server2    172.25.44.20

简介:

varnish : varnish是一款高性能的开源HTTP加速器。

实验环境:

主机的iptables处于off状态,setenforce 0

实验流程:

在server1和server2主机上:

        yum install httpd -y

        cd  /var/www/html/

        echo server1(2)\'s page > index.html

        /etc/init.d/httpd start

   浏览器访问主机IP172.25.44.10和172.25.44.20是否成功。

在varnish主机上:

yum install  varnish-3.0.5-1.el6.x86_64.rpm -y                        #从网上下载再安装

yum install varnish-libs-3.0.5-1.el6.x86_64.rpm -y                  #从网上下载再安装

        

            vi /etc/sysconfig/varnish

                VARNISH_LISTEN_PORT=80        #设定varnish的端口为80

            vi /etc/varnish/default.vcl

               backend server1 {

                .host =  "172.25.44.10" ;       #指定apache所在的主机

                .port = "80";                            #apache端口

                }

            /etc/init.d/varnish start                #启动varnish服务

            netstat -antlpe                             #查看80端口是否开启成功

浏览器访问172.25.44.1                        #显示的是server1主机的信息

            vi /etc/varnish/default.vcl            ##设定查看缓存信息

                    sub vcl_deliver {
                    if (obj.hits > 0) {
                    set resp.http.X-Cache = "HIT from varnish cache";    ##缓存命中
                    }
                    else {
                    set resp.http.X-Cache = "MISS from varnish cache";    ##缓存未命中
                    }
                    return (deliver);
                    }

                /etc/init.d/varnish restart                            ##重启varnish服务

                curl -I 172.25.44.30

                    #如果出现X-Cache : MISS from varnish cache  则表示缓存未命中

                    #如果出现X-Cache : HIT from varnish cache   则表示缓存命中

【缓存无法同步问题】

        缓存不过期页面无法刷新,通过varnishadm手动清除缓存

    #     varnishadm ban.url .*$        #清除所有

    #     varnishadm ban.url /index.html        #清除index.html页面缓存

    #     varnishadm ban.url /admin/$            #清除admin目录缓存

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

                vi /etc/varnish/default.vcl

                backend server1 {
                    .host = "172.25.44.20";
                    .port = "80";
                }
                backend westos {
                    .host = "172.25.44.10";
                    .port = "80";
                }

                sub vcl_recv {                ##网页缓存
                    if (req.http.host ~ "^(www.)?westos.com") {    ##访问中是否带www
                    set req.http.host = "www.westos.com";                        都定向到www上
                    set req.backend = westos;            ##访问的主机
                    } elsif (req.http.host ~ "^(www.)?linux.com") {    ##访问中是否带www都定向到www上
                        set req.http.host = "www.linux.com";

                        set req.backend = westos;         
                    } else {error 404 "varnish cache";
                    }
                }

                /etc/init.d/varnish restart            ##重启varnish服务

    在server1主机上:

        vi /etc/httpd/conf/httpd.conf

                
                DocumentRoot /www/westos.com/html
                ServerName www.westos.com
                

                
                DocumentRoot /www/linux.com/html
                ServerName www.linux.com
                

        /etc/init.d/httpd restart

        mkdir -p /www/westos.com/html

        mkdir -p /www/linux.com/html

        echo server1\'s www.westos.com > /www/westos.com/html/index.html

        echo server1\'s www.linux.com > /www/linux.com/html/index.html

        配置做完后在浏览器所在的真实主机上

        vim /etc/hosts

        172.25.44.1        www.westos.com

        172.25.44.1        www.linux.com

         172.25.44.1        server1.example.com

    浏览器访问www.westos.com和www.linux.com

【负载均衡器】  

        在server2主机上配置好http的虚拟主机

配置和server1主机一样

在varnish主机上:

        vi /etc/varnish/default.vcl

            director westoslb round-robin {
                { .backend = server1; }
                { .backend = westos; }
            }
            sub vcl_recv {
                if (req.http.host ~ "^(www.)?westos.org") {
                set req.http.host = "www.westos.org";
                set req.backend = westoslb;
                 return (pass);                ##方便测试,不缓存
                } elsif (req.http.host ~ "^(www.)?linux.com") {

                set req.http.host = "www.linux.com";
                set req.backend = westoslb;
                }

                 elsif (req.http.host ~ "^server1.example.com" ){
                set req.backend = server1;
                }

                else {error 404 "varnish cache";
                }
            }

        /etc/init.d/varnish restart

         浏览器访问网页,并且不断刷新

【varnish cdn推送平台】

        在varnish主机上:

        yum install httpd -y

        vi /etc/httpd/conf/httpd.conf

        Listen 8080    #修改http的端口为8080,因为varnish中使用的端口已经是80端口,如果不修改,则httpd服务起不来。

        /etc/init.d/httpd start

        yum install unzip

        unzip bansys.zip -d /var/www/html         #从网上下载获得

        yum install php -y

        # vi /var/www/html/bansys/config.php    #只保留如下设置,其余注释掉


 ///////////////////////数据库信息/////////////////////////
 //数据库信息
# $host = "localhost";
# $user = "";
# $passwd = "";
# $dbname = "bansys";
# $conn = mysql_connect($host, $user, $passwd) or die('Unable to connect database...');
# mysql_select_db($dbname,$conn);
# $query = "select ResourceIP from purgeapp_resource";
# $results = mysql_query($query,$conn);
#
#  while ($row=mysql_fetch_row($results)) {
#         $varnish_host[] =  $row[0];
#   }
# #mysql_close($conn);
////////////////////////////////////////////////////////

 //varnish主机列表
 //可定义多个主机列表
 $var_group1 = array(
                        'host' => array('172.25.44.1'),
                                                'port' => '6082',
                    );

//这个使用了mysql读取数据                                               
# $var_group3 = array(
#                        'host' => $varnish_host,
#                                               'port' => '6082',                               
#                    );                                          

 //varnish群组定义
 //对主机列表进行绑定
 $VAR_CLUSTER = array(
                         'www.westos.com' => $var_group1,
                     );

 //varnish版本
 //2.x和3.x推送命令不一样
 $VAR_VERSION = "3";
?>

vi /etc/varnish/default.vcl

acl bansys {
        "172.25.44.1";
        "172.25.44.0"/24;
}
sub vcl_recv {
        if (req.request == "BAN") {
        if (!client.ip ~ bansys) {
                error 405 "Not allowed.";
        }
        ban("req.url ~ " + req.url);
                error 200 "ban added";
        }


        if (req.http.host ~ "^(www.)?westos.com") {
        set req.http.host = "www.westos.com";
        set req.backend = westoslb;
        #return (pass);
        } elsif (req.http.host ~ "^(www.)?linux.com") {
        set req.http.host = "www.linux.com";

        set req.backend = westoslb;
        }

         elsif (req.http.host ~ "^server1.example.com" ){
        set req.backend = server1;
        }
        else {error 404 "varnish cache";
        }
}
/etc/init.d/httpd restart

/etc/init.d/varnish restart

在浏览器的真实主机上:

        curl -I http://www.westos.com/index.html

        #如果出现X-Cache : MISS from varnish cache  则表示缓存未命中

        #如果出现X-Cache : HIT from varnish cache   则表示缓存命中

访问172.25.44.1/bansys

redhat6.5系统lnmp架构varnish服务_第1张图片

cdn进行推送刷新缓存

curl -I http://www.westos.com/index.html

#缓存刷新成功后为MISS