Varnish 网站动静分离

定义后端主机列表:

vim /etc/varnish/default.vcl

# Default backend definition. Set this to point toyour content server.

backend websrv1 {

    .host ="192.168.2.223";

    .port ="80";

    .probe = {

        .url ="/index.htm";

        }

}

backend websrv2 {

    .host ="192.168.2.224";

    .port ="80";

    .probe = {

        .url ="/index.htm";

        }

}

 

sub vcl_recv {                            #通过正则表达式进行读写分离

    if (req.url~ "(?!)\.(jpg|png|gif|css|htm)$"){      #jpg等图片去访问websrv2,其它去访问websrv1

        setreq.backend_hint = websrv2;

       } else {

        setreq.backend_hint = websrv1;

        }

}

 

动态加载vcl文件

[root@node230 ~]# varnishadm -S /etc/varnish/secret -T192.168.2.230:6082

vcl.list

200       

active          0boot

 

vcl.load test1 test.vcl

200        

VCL compiled.

vcl.use test1

200        

VCL 'test1' now active

vcl.list

200       

available       0boot

active          0test1

 

backend.list

200       

Backend name                   Refs   Admin     Probe

default(192.168.2.223,,80)     1     probe      Healthy (no probe)

websrv1(192.168.2.223,,80)     1     probe      Healthy 5/8

websrv2(192.168.2.224,,80)     1     probe      Healthy 5/8

 

通过浏览器查看动静分离效果

http://192.168.2.230:6081

 

 

+++++++++++++++++++++++++++++++++++++++++++

基于Varnish VCL实现动静分离

如果用户请求的是图片那么则发给静态服务器,反之,如果是动态内容则发送给动态服务器

首先需要定义两个backend

backendimg_server1{

        .host="10.0.10.63";

        .port="8080";

}

 

backendapp_server1{

        .host="10.0.10.62";

        .port="8080";

}

 

sub vcl_recv {

   if(req.url ~"index.html$") {

    return(pass);

        }

   if(req.url ~ "\.php$") {

           set req.backend = app_server1;

       } else {

           set req.backend = img_eserver1;   

       }

}

 

sub vcl_deliver {

   setreq.http.X-Forward-For = client.ip;

   if (obj.hits > 0){

        set resp.http.X-Cache= "HITfrom"+" "+ server.ip;

        } else {

               setresp.http.XCahche ="MISS";

        }

}

 

varnish>vcl.loadtest6 ./test.vcl

200       

VCL compiled.

varnish>vcl.usetest6

200

 

 

分割线
感谢打赏
江西数库信息技术有限公司
YWSOS.COM 平台代运维解决方案
 评论
 发表评论
姓   名:

Powered by AKCMS