base虛擬機的安裝 和 varnish(rhel6.5)

base安裝(操作步驟如下):
  ifconfig
  hostname server1
  vi /etc/sysconfig/network
  vi /etc/yum.repos.d/rhel-source.repo    ##配置yum源
  yum repolist     ##yum的更新
  yum install -y vim lftp
  yum install -y openssh-clients
  cd /etc/udev/
  ls
  cd rules.d/
  ls
  rm -fr 70-persistent-net.rules
  ip addr
  ls
  cd/etc/sysconfig/network-scripts/
  ls
  vim ifcfg-eth0
  vim /etc/hosts
  cd /etc/ssh/
  ls
  rm -fr ssh_host_*    ##刪除ssh所給的鑰匙(如果ssh連接出現問題,就在這刪除再次連接)
  ls
  iptables -L
  cd /etc/sysconfig/
  ls
  rm -fr iptables
  ls
  pwd
  chkconfig iptables off   ##關閉火牆
  vim /etc/sysconfig/selinux   ##在文件中將selinux狀態改爲disabled



vm1(varnish輪詢主機)
     yum install -y *

(安裝varnish.x86_64 0:3.0.5-1.el6 varnish-libs.x86_64 0:3.0.5-1.el6這兩個安裝包下載安裝varnish )
     cd /etc/varnish/
     vim /etc/sysconfig/varnish (設置varnish默認端口是80VARNISH_LISTEN_PORT=80)
     /etc/init.d/varnish reload
     vim /etc/varnish/default.vcl  ##配置varnish
     /etc/init.d/varnish start
     /etc/init.d/varnish reload   


-------------------------------------------------------------------------------------------
###varnish的配置

[root@server1 varnish]# cat default.vcl

###定義多個不同域名站點的後端服務器
backend web1 {
  .host = "172.25.78.2";   ###配置一個後端服務器
  .port = "80";
}
backend web2 {
  .host = "172.25.78.3";
  .port = "80";
}

director lb round-robin {  #把多個後端聚合爲一個組,並檢測後端健康狀況
{.backend = web1;}
{.backend = web2;}
}

wKioL1l0f26C19AuAAC4oB_cxt4199.png

#當訪問 www.westos.org 域名時從 web1 上取數據,訪問 bbs.westos.org 域名時到 web2 取數據,訪問其他頁面報錯。
sub vcl_recv {
if (req.http.host ~ "^(www.)?westos.org") {
set req.http.host = "www.westos.org";
set req.backend = lb;
return (pass);     ##通常爲了方便測試,不進行緩存
} elsif (req.http.host ~ "^bbs.westos.org") {
set req.backend = web1;
} else {error 404 "westos cache";
}
}


###查看緩存命中情況
sub vcl_deliver {
if (obj.hits > 0) {
set resp.http.X-Cache = "HIT from westos cache";
}
else {
set resp.http.X-Cache = "MISS from westos cache";
}
return (deliver);
}

###測試緩存命中
[root@foundation78 ~]# curl -I www.westos.org
HTTP/1.1 200 OK
Server: Apache/2.2.15 (Red Hat)
Last-Modified: Wed, 19 Jul 2017 14:21:56 GMT
ETag: "9f614-20-554ac5ab83cc8"
Content-Type: text/html; charset=UTF-8
Content-Length: 32
Accept-Ranges: bytes
Date: Thu, 20 Jul 2017 03:22:27 GMT
X-Varnish: 1895199082
Age: 0
Via: 1.1 varnish
Connection: keep-alive
X-Cache: MISS from westos cache       #未命中

[root@foundation78 ~]# curl -I www.westos.org
HTTP/1.1 200 OK
Server: Apache/2.2.15 (Red Hat)
Last-Modified: Wed, 19 Jul 2017 14:21:56 GMT
ETag: "9f614-20-554ac5ab83cc8"
Content-Type: text/html; charset=UTF-8
Content-Length: 32
Accept-Ranges: bytes
Date: Thu, 20 Jul 2017 03:22:29 GMT
X-Varnish: 1895199083 1895199082
Age: 2
Via: 1.1 varnish
Connection: keep-alive
X-Cache: HIT from westos cache    ##命中

----------------------------------------------------------------------------------------------------------------------

### 通過 varnishadm 手動清除緩存
# varnishadm ban.url .*$       #清除所有
# varnishadm ban.url /index.html  #清除 index.html 頁面緩存
# varnishadm ban.url /admin/$     #清除 admin 目錄緩存



在vm2上(充當web1):

vim /etc/httpd/conf/httpd.conf

NameVirtualHost *:80(打開此端口)
<VirtualHost *:80>
    DocumentRoot /var/www/html
    ServerName server2

</VirtualHost>

<VirtualHost *:80>
    DocumentRoot /www/bbs
    ServerName bbs.westos.org
</VirtualHost>

<VirtualHost *:80>
    DocumentRoot /www/westos
    ServerName www.westos.org
</VirtualHost>

     yum install -y httpd
     /etc/init.d/httpd start
     cd /var/www/html/
     ls
     vim index.html
     vim /etc/httpd/conf/httpd.conf
     mkdir /www/bbs -p
     mkdir /www/westos
     cd /www/bbs/
     vim index.html
     cd /www/westos/
     vim index.html
     /etc/init.d/httpd restart
     vim /etc/hosts (給www.westos.org和bbs.westos.org作解析)

vm3(充當web2):

    yum install -y httpd
    /etc/init.d/httpd start
    cd /var/www/html/
    vim index.html

在物理機測試:
    vim /etc/hosts  (作解析)
    curl 172.25.78.1
    curl bbs.westos.org
    curl www.westos.org
    curl www.westos.org/index.html

------------------------------------------------------------------------------------------------------------------

###varnish cdn 推送平臺####
CDN推送

1.vim /etc/httpd/conf/httpd.conf
因爲做varnish時候把監聽端口改成了80,所以做cdn推送時候要更改http的監聽端口爲8080

2.yum install unzip.x86_64 -y

3.unzip bansys.zip -d /var/www/html/ 
解壓bansys到html目錄下,可以直接網頁推送(cd /var/www/html/bansys/ mv * ..  rm -fr bansys/)

4.yum install -y php 
因爲cdn裏面的文件是使用php編寫的,需要下載php編譯器(需要安裝 php 支持)

5./etc/init.d/varnish start
  /etc/init.d/httpd start

6.vim /var/www/html/config.php(配置文件更改如下)
*****************************************
*<?php
*   $var_group1 = array(
*   'host' =>array('172.25.78.1'),
*   'port' => '80',
*);
*
*//varnish 羣組定義
*//對主機列表進行綁定
*$VAR_CLUSTER = array(
*   'www.westos.org' =>$var_group1,
*);
*
*//varnish 版本//2.x 和 3.x 推送命令不一樣
*$VAR_VERSION = "3";
*?>
******************************************


#bansys 有兩種工作模式,分別是:telnet 和 http 模式。
#telnet 模式需要關閉 varnish 服務管理端口的驗證,註釋掉/etc/sysconfig/varnish 文件中的 “ -S ${VARNISH_SECRET_FILE}”這行,重啓 varnish 服務即可。
#如果是 http 模式需要對 varnish 做以下設置:

# vim /etc/varnish/default.vcl
acl westos {
        #設置訪問控制
    "127.0.0.1";
    "192.168.0.0"/24;
}
sub vcl_recv {
    if (req.request == "BAN"){
        if (!client.ip ~ westos) {
           error 405 "Notallowed.";
    }
    ban("req.url ~ " +req.url);
    error 200 "ban added";
      }
}
# service varnish reload


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章