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.www58058.com/59185

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

相關推薦

  • Linux下編譯內核

                           Linux下編譯內核 內核編譯: 編譯內核就是把內核的相關文件重新生成。   內核編譯前期準備: Linux kernel下載網址:https://www.kernel.or…

    系統運維 2016-09-21
  • locate,find文件查找

    locate | find *** locate 路徑中包含字符串 即匹配;      -n#     指定顯示結果前幾個      -b      只匹配路徑名中的基名      &nbs…

    Linux干貨 2016-08-15
  • Linux運維基礎2

    shell編程

    Linux干貨 2018-03-19
  • debian8下安裝配置部署zabbix3.0

    一、安裝配置zabbix server     web server服務器:172.28.0.187     mysql服務器:172.28.0.237     1、安裝web server(172.28.0.187)  &nbs…

    Linux干貨 2016-05-07
  • MySQL or MariaDB 簡介

    DBMS:數據庫管理系統 RDBMS:關系型數據庫管理系統    總之:他們都是一個數據管理程序;大多都是CS架構,都有專門的通信協議進行數據交換 關系模型:               表(行或者列):二維關系 設計范式:       &…

    Linux干貨 2017-01-12
  • find 命令詳解

    Find 命令詳解 find:   實時查找工具, 通過遍歷指定路徑完成文件查找;   工作特點:     查找速度略慢     精確查找     實時查找 可能只搜索用戶具備讀取和執行權限的目錄   語法:   find [OP…

    2017-04-09
欧美性久久久久