linux 中varnish服务
一、安装varnish
在server1中安装两个包
varnish-3.0.5-1.el6.x86_64.rpm
varnish-libs-3.0.5-1.el6.x86_64.rpm
1.在server1中(varnish)配置varnish服务
vim /etc/sysconfig/varnish
VARNISH_LISTEN_PORT=80 ##varnish监听端口
在server1中安装两个包
varnish-3.0.5-1.el6.x86_64.rpm
varnish-libs-3.0.5-1.el6.x86_64.rpm
1.在server1中(varnish)配置varnish服务
vim /etc/sysconfig/varnish
VARNISH_LISTEN_PORT=80 ##varnish监听端口
![](https://img2018.cnblogs.com/blog/1443263/201809/1443263-20180913202247632-1465685796.png)
vim /etc/varnish/default.vcl
backend default {
.host = "172.25.8.2";
.port = "80";
}
backend default {
.host = "172.25.8.2";
.port = "80";
}
![](https://img2018.cnblogs.com/blog/1443263/201809/1443263-20180913202127714-803275139.png)
然后开启服务
/etc/init.d/varnish start
测试
新开一台虚拟机(server2)
yum install httpd
yum install httpd
/etc/init.d/httpd start
vim /var/www/html/index.html
<h1>www.westos.org-sever2</h1>
vim /var/www/html/index.html
<h1>www.westos.org-sever2</h1>
在浏览器中输入172.25.8.1
会出现server2index.html内容
![](https://img2018.cnblogs.com/blog/1443263/201809/1443263-20180913202234365-1284598061.png)
二、缓存命中
在varnish中:
vim default.vcl
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);
}
vim default.vcl
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);
}
/etc/init.d/varnish reload ##使用reload重新加载
测试
curl www.westos.org
www.westos.org-sever2
vim /etc/hosts
172.25.8.1 server1 www.westos.org www.linux.org westos.org ##加入本地解析
www.westos.org-sever2
vim /etc/hosts
172.25.8.1 server1 www.westos.org www.linux.org westos.org ##加入本地解析
curl -I www.westos.org
![](https://img2018.cnblogs.com/blog/1443263/201809/1443263-20180913202517608-50949820.png)
三、varnishadm 清除缓存
varnishadm ban.url .*$ 清除所有
varnishadm ban.url /index.html 清除 index.html 页面缓存
varnishadm ban.url /admin/$ 清除 admin 目录缓存
四、定义多个不同域名站点的后端服务器
再开启一台虚拟机(server3)
yum install httpd
vim /var/www/html/index.html
<h1>www.linux.org</h1>
在varnish中:
vim default.vcl
vim default.vcl
backend web1 {
.host = "172.25.8.2";
.port = "80";
}
.host = "172.25.8.2";
.port = "80";
}
backend web2 {
.host = "172.25.8.3";
.port = "80";
}
.host = "172.25.8.3";
.port = "80";
}
sub vcl_recv {
if (req.http.host ~ "^(www.)?westos.org") {
set req.http.host = "www.westos.org";
set req.backend = lb;
} elsif (req.http.host ~ "^www.linux.org") {
set req.backend = web2;
} else {error 404 "westos cache";
}
}
if (req.http.host ~ "^(www.)?westos.org") {
set req.http.host = "www.westos.org";
set req.backend = lb;
} elsif (req.http.host ~ "^www.linux.org") {
set req.backend = web2;
} else {error 404 "westos cache";
}
}
![](https://img2018.cnblogs.com/blog/1443263/201809/1443263-20180913202639039-987081277.png)
/etc/init.d/varnish reload 重载
测试:
curl www.westos.org
www.westos.org-sever2
curl westos.org
www.westos.org-sever2
curl www.linux.org
www.linux.org
![](https://img2018.cnblogs.com/blog/1443263/201809/1443263-20180913202653787-1318279146.png)
五、负载均衡
在server3中:
vim /etc/httpd/conf/httpd.conf
vim /etc/httpd/conf/httpd.conf
NameVirtualHost *:80
![](https://img2018.cnblogs.com/blog/1443263/201809/1443263-20180913202720523-1550017627.png)
<VirtualHost *:80>
DocumentRoot /var/www/html
ServerName www.linux.org
</VirtualHost>
<VirtualHost *:80>
DocumentRoot /www1
ServerName www.westos.org
</VirtualHost>
在server3中
mkdir /www1
vim /www1/index.html
<h1>www.westos.org-server3</h1>
mkdir /www1
vim /www1/index.html
<h1>www.westos.org-server3</h1>
/etc/init.d/httpd restart
[root@server3 ~]# curl www.linux.org
<h1>www.linux.org</h1>
[root@server3 ~]# curl www.linux.org
<h1>www.linux.org</h1>
[root@server3 ~]# curl www.westos.org
<h1>www.westos.org-server3</h1>
<h1>www.westos.org-server3</h1>
在varnish中:
vim default.vcl
backend web1 {
.host = "172.25.8.2";
.port = "80";
}
backend web2 {
.host = "172.25.8.3";
.port = "80";
}
.host = "172.25.8.3";
.port = "80";
}
director lb round-robin {
{ .backend = web1; }
{ .backend = web2; }
}
{ .backend = web1; }
{ .backend = 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 ~ "^www.linux.org") {
set req.backend = web2;
} else {error 404 "westos cache";
}
}
if (req.http.host ~ "^(www.)?westos.org") {
set req.http.host = "www.westos.org";
set req.backend = lb;
#return (pass) 加入此行可以更加直观看到实验效果,正常情况下不加
} elsif (req.http.host ~ "^www.linux.org") {
set req.backend = web2;
} else {error 404 "westos cache";
}
}
![](https://img2018.cnblogs.com/blog/1443263/201809/1443263-20180913202822156-2106057223.png)
/etc/init.d/varnish reload
测试:实现负载均衡
当server2的httpd闭
[root@foundation8 ~]# curl westos.org/index.html
<h1>www.westos.org-server3</h1>
[root@foundation8 ~]# curl westos.org/index.html
<h1>www.westos.org-server3</h1>
<h1>www.westos.org-server3</h1>
[root@foundation8 ~]# curl westos.org/index.html
<h1>www.westos.org-server3</h1>
![](https://img2018.cnblogs.com/blog/1443263/201809/1443263-20180913202929790-1396315411.png)
当server3的httpd闭
[root@foundation8 ~]# curl westos.org/index.html
<h1>www.westos.org-sever2</h1>
[root@foundation8 ~]# curl westos.org/index.html
<h1>www.westos.org-sever2</h1>
![](https://img2018.cnblogs.com/blog/1443263/201809/1443263-20180913202918895-580385329.png)
当server2和server3的http都开启
[root@foundation8 ~]# curl westos.org
<h1>www.westos.org-server3</h1>
[root@foundation8 ~]# curl westos.org
<h1>www.westos.org-sever2</h1>
[root@foundation8 ~]# curl westos.org
<h1>www.westos.org-server3</h1>
<h1>www.westos.org-server3</h1>
[root@foundation8 ~]# curl westos.org
<h1>www.westos.org-sever2</h1>
[root@foundation8 ~]# curl westos.org
<h1>www.westos.org-server3</h1>
![](https://img2018.cnblogs.com/blog/1443263/201809/1443263-20180913202906963-715263319.png)
六、cdn推送平台搭建
在varnish主机上
yum install httpd
vim /etc/httpd/conf/http.conf
将监听端口改为8080,防止和varnish主机80端口冲突
vim /etc/httpd/conf/http.conf
将监听端口改为8080,防止和varnish主机80端口冲突
![](https://img2018.cnblogs.com/blog/1443263/201809/1443263-20180913203128138-959662517.png)
/etc/init.d/httpd restart
下载bansys.zip包
yum install php
yum install unzip
unzip bansys.zip -d /var/www/html
cd /var/www/html/bansys
mv * ..
cd ..
vim config.php
/etc/init.d/httpd start
<?php
yum install unzip
unzip bansys.zip -d /var/www/html
cd /var/www/html/bansys
mv * ..
cd ..
vim config.php
/etc/init.d/httpd start
<?php
//varnish主机列表
//可定义多个主机列表
$var_group1 = array(
'host' => array('172.25.8.1'),
'port' => '80',
);
//varnish群组定义
//对主机列表进行绑定
$VAR_CLUSTER = array(
'www.westos.org' => $var_group1,
);
![](https://img2018.cnblogs.com/blog/1443263/201809/1443263-20180913203150464-1715951729.png)
vim /etc/varnish/default.vcl
acl westos {
"127.0.0.1";
"172.25.8.0"/24;
}
"127.0.0.1";
"172.25.8.0"/24;
}
![](https://img2018.cnblogs.com/blog/1443263/201809/1443263-20180913203213555-50451692.png)
sub vcl_recv {
if (req.request == "BAN"){
if (!client.ip ~ westos){
error 405 "Not allowed.";
}
ban ("req.url ~ " + req.url);
error 200 "ban added";
}
if (!client.ip ~ westos){
error 405 "Not allowed.";
}
ban ("req.url ~ " + req.url);
error 200 "ban added";
}
![](https://img2018.cnblogs.com/blog/1443263/201809/1443263-20180913203226197-157220112.png)
![](https://img2018.cnblogs.com/blog/1443263/201809/1443263-20180913203316922-1221802787.png)