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/
rm -fr 70-persistent-net.rules
ip addr
cd/etc/sysconfig/network-scripts/
vim ifcfg-eth0
vim /etc/hosts
cd /etc/ssh/
rm -fr ssh_host_* ##删除ssh所給的鑰匙(如果ssh連接配接出現問題,就在這删除再次連接配接)
iptables -L
cd /etc/sysconfig/
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";
director lb round-robin { #把多個後端聚合為一個組,并檢測後端健康狀況
{.backend = web1;}
{.backend = web2;}
<a href="https://s1.51cto.com/wyfs02/M00/9C/AD/wKioL1l0f26C19AuAAC4oB_cxt4199.png" target="_blank"></a>
#當通路 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 #未命中
Date: Thu, 20 Jul 2017 03:22:29 GMT
X-Varnish: 1895199083 1895199082
Age: 2
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>
DocumentRoot /www/bbs
ServerName bbs.westos.org
DocumentRoot /www/westos
ServerName www.westos.org
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/
cd /www/westos/
/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;
if (req.request == "BAN"){
if (!client.ip ~ westos) {
error 405 "Notallowed.";
}
ban("req.url ~ " +req.url);
error 200 "ban added";
}
# service varnish reload
本文轉自 yab109 51CTO部落格,原文連結:http://blog.51cto.com/12768057/1949519,如需轉載請自行聯系原作者