varnish

主機環境: rhel6 selinux and iptables disabled
實驗主機: 192.168.122.10   varnish       server1.example.com
               192.168.122.2    apache           server2.example.com

                192.168.122.3    apache         server3.example.com php


 

VCL 處理流程圖 html


1:安裝
       http://repo.varnish-cache.org/redhat/varnish-3.0/el6/x86_64/ # yum localinstall varnish-3.0.5-1.el6.x86_64.rpm    varnish-libs-3.0.5-1.el6.x86_64.rpm 2:配置: 將其監聽端口修改成80端口以響應apache         # vim /etc/sysconfig/varnish             # # a host name, an IPv4 dotted quad, or an IPv6 address in brackets.             # VARNISH_LISTEN_ADDRESS=             VARNISH_LISTEN_PORT=80 # vim /etc/varnish/default.vcl         ###配置一個後端服務器         backend web1 {     .host = "192.168.122.2";     .port = "80";     } # /etc/init.d/varnish start ##查看緩存命中狀況 sub vcl_deliver { if (obj.hits > 0) { set resp.http.X-Cache = "HIT from data cache"; } else { set resp.http.X-Cache = "MISS from dat cache"; } return (deliver); } # service varnish reload [root@server1 ~]# curl -I 192.168.122.10 HTTP/1.1 200 OK Server: Apache/2.2.15 (Red Hat) Last-Modified: Mon, 29 Jun 2015 12:41:02 GMT ETag: "10c5-14-519a76705da97" Content-Type: text/html; charset=UTF-8 Content-Length: 20 Accept-Ranges: bytes Date: Mon, 29 Jun 2015 12:52:41 GMT X-Varnish: 933387577 Age: 0 Via: 1.1 varnish Connection: keep-alive X-Cache: MISS from data cache    #未命中 [root@server1 ~]# curl -I 192.168.122.10 HTTP/1.1 200 OK Server: Apache/2.2.15 (Red Hat) Last-Modified: Mon, 29 Jun 2015 12:41:02 GMT ETag: "10c5-14-519a76705da97" Content-Type: text/html; charset=UTF-8 Content-Length: 20 Accept-Ranges: bytes Date: Mon, 29 Jun 2015 12:53:09 GMT X-Varnish: 933387578 933387577 Age: 28 Via: 1.1 varnish Connection: keep-alive X-Cache: HIT from data cache   #命中 ###經過 varnishadm 手動清除緩存 # varnishadm ban.url .*$ #清除全部 # varnishadm ban.url /index.html #清除 index.html 頁面緩存 # varnishadm ban.url /admin/$ #清除 admin 目錄緩存 ###定義多個不一樣域名站點的後端服務器 backend web1 {   .host = "192.168.122.2";   .port = "80"; } backend web2 {   .host = "192.168.122.3";   .port = "80"; } #當訪問 www.westos.org 域名時從 web1 上取數據,訪問 bbs.westos.org 域名時到 web2 取數據, 訪問其餘頁面報錯。 sub vcl_recv { if (req.http.host ~ "^(www.)?plumxx.org") { set req.http.host = "www.plumxx.org"; set req.backend = web1; } elsif (req.http.host ~ "^bbs.plumxx.org") { set req.backend = web2; } else {error 404 "plumxx.cache"; } } # service varnish reload 當咱們直接訪問其ip時,就會出現以下報錯 以域名的形式訪問 ###定義負載均衡 director lb round-robin {          #定義其名稱爲lb {.backend = web1;} {.backend = web2; } } sub vcl_recv { if (req.http.host ~ "^(www.)?plumxx.org") { set req.http.host = "www.plumxx.org"; set req.backend = lb;                                       #注意這塊兒要返回剛纔定義的lb } elsif (req.http.host ~ "^bbs.plumxx.org") { set req.backend = web2; } else {error 404 "plumxx.cache"; } } 在server3上咱們須要修改其配置文件 [root@server3 ~]# vim /etc/httpd/conf/httpd.conf # NameVirtualHost *:80     # 打開其虛擬端口 同時在最後一行加入 <VirtualHost *:80>     DocumentRoot /var/www/html/     ServerName  www.plumxx.org </VirtualHost> <VirtualHost *:80>     DocumentRoot /var/www/vhost1     ServerName bbs.plumxx.org </VirtualHost> [root@server3 ~]# mkdi /var/www/vhost1/ service     httpd     start # service varnish reload ###varnish cdn 推送平臺 須要安裝 php 支持 # unzip bansys.zip -d /var/www/html vim /var/www/html/config.php  $var_group1 = array(                         'host' => array('192.168.122.10'),                                                 'port' => '8080',               #由於80端口被佔用這裏定義其端口爲8080 $var_group3 = array(                         'host' => $varnish_host,                                                 'port' => '6082',                                    ); 同時修改server1上的apache端口 [root@server1 ~]# vim /etc/httpd/conf/httpd.conf #Listen 12.34.56.78:80 Listen 8080 service    http      restart #bansys 有兩種工做模式,分別是:telnet 和 http 模式。 #telnet 模式須要關閉 varnish 服務管理端口的驗證,註釋掉/etc/sysconfig/varnish 文件中的「-S $ {VARNISH_SECRET_FILE}」這行,重啓 varnish 服務便可。 #若是是 http 模式須要對 varnish 作如下設置 acl plumxx { "127.0.0.1"; "192.168.122.0"/24; } sub vcl_recv { if (req.request == "BAN") { if (!client.ip ~ plumxx) { error 405 "Not allowed."; } ban("req.url ~ " + req.url); error 200 "ban added"; } } # service varnish reload
相關文章
相關標籤/搜索