varnish4 基础实战

实验环境

node1 192.168.0.8 varnish服务器

node2 192.168.0.3 动态web

node3 192.168.0.7 静态web

node1安装varnish

##安装varnish yum源 
# wget http://repo.varnish-cache.org/redhat/varnish-4.1.el6.rpm  
# yum install varnish-4.1.el6.rpm 

# yum install jemalloc varnish

varnish服务端配置

# vi /etc/sysconfig/varnish     
    ##修改varnish监听端口为80
    VARNISH_LISTEN_PORT=80

缓存规则配置

# vi /etc/varnish/default.vcl vcl 4.0; 
backend static 
{     
    .host = "192.168.0.7";     
    .port = "80"; 
} 
backend dynamic 
{     
    .host = "192.168.0.3";     
    .port = "80"; 
}  
sub vcl_recv 
{    
    if(req.url ~ "\\.html") 
    {       
        set req.backend=static;    
    }    
    if(req.url ~ "\\.php") 
    {       
        set req.backend=dynamic;
        return(pass);    
     }
}
sub vcl_backend_response { 
        set beresp.ttl = 7200s;
}

启动varnish并加载缓存规则

# service varnish start # varnishadm -S /etc/varnish/secret -T 127.0.0.1:6082 ## 加载vcl文件 varnish> vcl.load default default.vcl  varnish> vcl.use default   varnish> quit

web服务器node2 node3安装httpd

node3 192.168.0.7

# yum install httpd # service httpd start # echo "static 192.168.0.7" > /var/www/html/index.html # echo "dynamic 192.168.0.7" > /var/www/html/index.php

node2 192.168.0.3

# yum install httpd # service httpd start # echo "static 192.168.0.3" > /var/www/html/index.html # echo "dynamic 192.168.0.3" > /var/www/html/index.php

测试

# curl -v 192.168.0.8/index.html
* About to connect() to 192.168.0.8 port 80 (#0)
*   Trying 192.168.0.8... connected
* Connected to 192.168.0.8 (192.168.0.8) port 80 (#0)
> GET /index.html HTTP/1.1
> User-Agent: curl/7.19.7 (x86_64-redhat-linux-gnu) libcurl/7.19.7 NSS/3.14.0.0 zlib/1.2.3 libidn/1.18 libssh2/1.4.2
> Host: 192.168.0.8
> Accept: */*
> 
< HTTP/1.1 200 OK
< Date: Mon, 31 Oct 2016 10:08:52 GMT
< Server: Apache/2.2.15 (CentOS)
< Last-Modified: Mon, 31 Oct 2016 08:43:52 GMT
< ETag: "2c1f-13-540253518f140"
< Content-Length: 19
< Content-Type: text/html; charset=UTF-8
< X-Varnish: 32783 3
< Age: 6288
< Via: 1.1 varnish-v4
< Accept-Ranges: bytes
< Connection: keep-alive
< 
static 192.168.0.7
* Connection #0 to host 192.168.0.8 left intact
* Closing connection #0

# curl -v 192.168.0.8/index.php
* About to connect() to 192.168.0.8 port 80 (#0)
*   Trying 192.168.0.8... connected
* Connected to 192.168.0.8 (192.168.0.8) port 80 (#0)
> GET /index.php HTTP/1.1
> User-Agent: curl/7.19.7 (x86_64-redhat-linux-gnu) libcurl/7.19.7 NSS/3.14.0.0 zlib/1.2.3 libidn/1.18 libssh2/1.4.2
> Host: 192.168.0.8
> Accept: */*
> 
< HTTP/1.1 200 OK
< Date: Mon, 07 Nov 2016 05:03:13 GMT
< Server: Apache/2.2.15 (CentOS)
< Last-Modified: Mon, 07 Nov 2016 02:00:42 GMT
< ETag: "1014db-14-540ac642c51dd"
< Accept-Ranges: bytes
< Content-Length: 20
< Content-Type: text/plain; charset=UTF-8
< X-Varnish: 13
< Age: 0
< Via: 1.1 varnish-v4
< Connection: keep-alive
< 
dynamic 192.168.0.3
* Connection #0 to host 192.168.0.8 left intact
* Closing connection #0

多次访问可发现index.php始终分配到node2,index.html一直分配在node3,表明动态分离成功

访问index.html时X-Varnish头部显示有2个数字,第一个数字是请求的标识ID,第二个数字是缓存的标识ID,表明缓存已命中!

原创文章,作者:N24_lizi1,如若转载,请注明出处:http://www.178linux.com/59185

(0)
N24_lizi1N24_lizi1
上一篇 2016-11-15
下一篇 2016-11-16

相关推荐

  • mongodb 分片集群搭建

    集群架构 本次实验部署3台主机node1、node2、node3,2个副本集db1、db2作为分片,3台主机均有1个配置库实例 操作步骤 安装 node1上执行如下命令,完成后拷贝/mongodb目录到node2、node3 useradd mongodb   mkdir -p /mongodb/data/{…

    Linux干货 2016-11-06
  • 马哥教育网络班21期+第11周课程练习

    1、详细描述一次加密通讯的过程,结合图示最佳。 单向加密:只能加密,不能解密,提取数据指纹(特征码),来保证数据的完整性,如上图的第二步,单向加密的协议有MD5,SHA等 非对称加密:公钥和私钥成对出现,私钥必须本机器保存,用公钥加密的数据,只能使用与之配对儿的私钥解密;反之亦然,数字签名是私钥加密特征码,如上图的第三步;实现对称秘钥交换,如上图第五步 对称…

    Linux干货 2016-08-15
  • 优云Automation:实现IT服务弹性伸缩的利器

    随着互联网业务快速持续增长,IT资源使用量按需变化成为常态,这就要求信息部门能快速响应资源使用的变化要求,对运维提出不小挑战。比如电商、在线教育等企业经常推出一些秒杀、抢红包活动,在特定时间段对资源的利用处于高峰期,之后基本处于空闲。 几年前,我们从申请采购到应用部署上线都只能人工操作,需要提前几个月开始规划。如今通过云平台能很好实现资源按需动态管理,运维人…

    系统运维 2017-03-15
  • 【原创】RHEL7-PPTP-VPN-Server排错

    第一次写博客,明显不知道如何下笔。     昨天6月21日,突然发现往日运行一切正常的pptpvpn服务器怎么也连不上了,错误代码是619。这个错误代码以前并没有见过,于是上google查了一下资料,据说有几种可能: 1,路由器或防火墙干掉了tcp1723; 2,电脑协议栈问题; 3,拨号连接的认证选项有问题; &nb…

    Linux干货 2016-06-23
  • 关于大型网站技术演进的思考(一):存储的瓶颈(1)

    原文出处: 夏天的森林  前不久公司请来了位互联网界的技术大牛跟我们做了一次大型网站架构的培训,两天12个小时信息量非常大,知识的广度和难度也非常大,培训完后我很难完整理出全部听到的知识,今天我换了个思路是回味这次培训,这个思路就是通过本人目前的经验和技术水平来思考下大型网站技术演进的过程。 首先我们要思考一个问题,什么样的网站才是大型网…

    Linux干货 2015-02-26
  • grep、find练习

    1.显示当前系统上root,fedora或user1用户的默认shell [root@study ~]# cat /etc/passwd|grep -E "^(root|fedora|user1)"|cut -d: -f7 /bin/bash /bin/bash /bi…

    Linux干货 2016-11-27