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

相关推荐

  • ThirdWeek_SecondDay

    Python学习笔记整理

    Linux干货 2017-10-09
  • DNS域名解析系统搭建(BIND)

        【本文导航】    零、准备工作    一、根域服务器配置    二、com顶级域配置    三、linux.com域主服务器配置(DNS1)    四…

    Linux干货 2016-12-21
  • iptables实现地址转换

    NAT:(工作在网络和传输层) 过载技术 Basic NAT:静态NAT     一个内部主机,分配一个外网地址 NAPT:动态NAT,网络地址端口转换;net会话表     源地址转换:SNAT 用于内网主机访问互联网    &…

    Linux干货 2016-10-31
  • 第七周练习

    1、创建一个10G分区,并格式为ext4文件系统; (1) 要求其block大小为2048, 预留空间百分比为2, 卷标为MYDATA, 默认挂载属性包含acl; (2) 挂载至/data/mydata目录,要求挂载时禁止程序自动运行,且不更新文件的访问时间戳; 1.[root – gwx ~]#>fdisk /dev/sdb2.W…

    Linux干货 2016-12-11
  • wed服务基础·httpd基础配置详解

    一、 Web Service基础: service:计算机后台提供的功能或计算机可以提供的某一种功能 Web Service本质:通过网络调用其它网站的资源 根据来源的不同,分为两种服务: 本地服务:使用同一台机器提供的服务,不需要网络 网络服务:使用另一台机器提供的服务,需要网络   IANA互联网地址授权机构(Internet Assigned…

    2017-06-09
  • 网络接口配置–Bonding

    网络接口配置–Bonding Bonding        就是讲到快网卡绑定到同一IP地址对外服务,可以实现高可用或者负载均衡。当然,直接给两块网卡设置同一IP地址是不可能的。通过bonding,虚拟一块网卡对外提供连接,物理网卡被修改为同一MAC地址。 一 Bonding …

    Linux干货 2017-05-07