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 23:31
下一篇 2016-11-16 16:47

相关推荐

  • 端口转发实例

    什么是端口转发?      端口转发(Port forwarding),有时被叫做隧道,是安全壳(SSH) 为网络安全通信使用的一种方法。端口转发是转发一个网络端口从一个网络节点到另一个网络节点的行为,其使一个外部用户从外部经过一个被激活的NAT路由器到达一个在私有内部IP地址(局域网内部)上的一个端口。 为什么需要端口转发? …

    Linux干货 2017-04-13
  • Linux 2.6.39-rc3的一个插曲

    2011年4月12日,Linux 2.6.39-rc3发布了,Linus Torvalds写了一个发布邮件,其中包含了一个长长的为这个版本做过贡献的人员名单,这个名单中有很多看上去应该是中国人的名字,我挺为他们感到骄傲的(不知道你是否还记得以前本站的”Linux是由谁写的“)。 不过,没过一会,发现了一个bug,经过大家的调查(2.6.38版没有发现这个问题…

    Linux干货 2016-06-09
  • Hello World

    随笔

    Linux干货 2018-03-26
  • CetnOS6启动流程以及LVM下的fstab及boot恢复实验

    centos6启动流程 主动读取BIOS,通过CMOS加载硬件信息 ,并进行POST,指定第一个可启动的设备 ROM:BIOS,Basic Input and Output System,保存着计算机系统最重要的基本输入输出程序,系统信息设置,开机加电自检程序RAM:保存各项参数的设定 读取第一个可启动设备MBR的引导加载程序(grub)的启动信息 MBR:…

    Linux干货 2017-05-13
  • 排名三剑客的VIM _正确的打开姿势!

    vim程序编辑器        vi:是老式的文字处理器。     vi的使用:【vi分为3种模式一般模式、编辑模式、命令模式】         一般模式:   vi打开文件就直接进入到了一…

    Linux干货 2017-04-10