實驗環境:實體機(通路測試): willis.example.com 172.25.254.6
varnish緩存端: server1.example.com 172.25.254.1
負載均衡端1: web1.example.com 172.25.254.10
負載均衡端2: web2.example.com 172.25.254.20
實驗内容:1.解壓安裝包
2.配置資料庫資訊
3.安裝varnish,http與php
4.配置varnish
5.通路端測試
6.cdn推送
實驗前提:負載均衡端已配置好虛拟主機
實驗安裝包:bansys.zip
varnish-3.0.5-1.el6.x86_64.rpm
varnish-libs-3.0.5-1.el6.x86_64.rpm
1.解壓安裝包
[root@server1 mnt]# ls
bansys.zip
[root@server1 mnt]# unzip bansys.zip -d /usr/local/lnmp/
2.配置資料庫資訊
[root@server1 mnt]# vim /var/www/html/bansys/config.php #隻保留如下設定,其餘注釋掉
<?php
///////////////////////資料庫資訊/////////////////////////
//資料庫資訊
# $host = "localhost";
# $user = "";
# $passwd = "";
# $dbname = "bansys";
# $conn = mysql_connect($host, $user, $passwd) or die('Unable to connect database...');
# mysql_select_db($dbname,$conn);
# $query = "select ResourceIP from purgeapp_resource";
# $results = mysql_query($query,$conn);
#
# while ($row=mysql_fetch_row($results)) {
# $varnish_host[] = $row[0];
# }
# #mysql_close($conn);
////////////////////////////////////////////////////////
//varnish主機清單
//可定義多個主機清單
$var_group1 = array(
'host' => array('172.25.254.1'),
'port' => '6082',
);
//這個使用了mysql讀取資料
# $var_group3 = array(
# 'host' => $varnish_host,
# 'port' => '6082',
# );
//varnish群組定義
//對主機清單進行綁定
$VAR_CLUSTER = array(
'www.willis.com' => $var_group1,
);
//varnish版本
//2.x和3.x推送指令不一樣
$VAR_VERSION = "3";
?>
3.安裝varnish,http與php
varnish-3.0.5-1.el6.x86_64.rpm varnish-libs-3.0.5-1.el6.x86_64.rpm
[root@server1 mnt]# yum install * -y
[root@server1 mnt]# vim /etc/sysconfig/varnish
VARNISH_LISTEN_PORT=80 ##設定varnish的端口為80
[root@server1 mnt]# /etc/init.d/varnish start
[root@server1 html]# yum install php httpd -y
[root@server1 html]# vim /etc/httpd/conf/httpd.conf
136 Listen 8080
402 DirectoryIndex index.php index.html index.html.var
[root@server1 html]# mv bansys/ upindex
[root@server1 html]# /etc/init.d/httpd start
4.配置varnish
[root@server1 mnt]#vim /etc/varnish/default.vcl
backend web1 {
.host = "172.25.254.20";
.port = "80";
}
backend web2 {
.host="172.25.254.10";
.port="80";
director willislb round-robin {
{ .backend = web1; }
{ .backend = web2; }
sub vcl_deliver{
if(obj.hits>0){
set resp.http.X-Cache="HIT from willis cache";
}
else{
set resp.http.X-Cache="MISS from willis cache";
return(deliver);
}
acl upindex {
"127.0.0.1";
"172.25.254.0"/24;
sub vcl_recv {
if (req.request == "BAN") {
if (!client.ip ~ upindex) {
error 405 "Not allowed.";
ban("req.url ~ " + req.url);
error 200 "ban added";
if (req.http.host ~ "^(www.)?willis.com" ) {
set req.http.host = "www.willis.com";
set req.backend = willislb;
#return(pass); ###有緩存,用來測試緩存平台
elsif (req.http.host ~ "^(www.)?linux.com" ) {
set req.http.host = "www.linux.com";
return(pass);
else {error 404 "willis cache";
5.通路端測試:
[root@willis Desktop]# curl -I http://www.willis.com/index.html
X-Cache: MISS from willis cache
X-Cache: HIT from willis cache
6.cdn推送:
<a href="http://s1.51cto.com/wyfs02/M01/87/58/wKioL1feIBGR8xuLAAB-lrAC4KU771.png" target="_blank"></a>
<a href="http://s4.51cto.com/wyfs02/M00/87/5B/wKiom1feIBLQyxaTAABSpiHZQnA217.png" target="_blank"></a>
<a href="http://s4.51cto.com/wyfs02/M02/87/58/wKioL1feIBKjDgAlAAAyswZs9I8824.png" target="_blank"></a>
推送完之後測試:
本文轉自willis_sun 51CTO部落格,原文連結:http://blog.51cto.com/willis/1853589,如需轉載請自行聯系原作者