varnish 实现 CDN 缓存系统构建

cdn 搭建 (server1:172.25.1.1 ) :
[root@test1 ~]# ls
varnish-3.0.5-1.el6.x86_64.rpm varnish-libs-3.0.5-1.el6.x86_64.rpm
[root@test1 ~]# yum install * -y
[root@test1 ~]# cd /etc/varnish/
[root@test1 varnish]# vim /etc/sysconfig/varnish

[root@test1 varnish]# sysctl -a | grep file

[root@test1 varnish]# vim /etc/security/limits.conf
         

//系统限制值 limits,根据文件/etc/sysconfig/varnish 里面的限制值进行配置,对 cpu不做限制
配置一个后端服务器 ( 在 cdn 上进行配置 )
[root@server1 varnish]# /etc/init.d/varnish start         //开启 varnish 服务

[root@server1 varnish]# vim default.vcl       //配置后端服务器

[root@test1 varnish]# /etc/init.d/varnish reload
服务器 1 ( server2:172.25.1.2) :
[root@test2 ~]# yum install -y httpd
[root@test2 ~]# vim /var/www/html/index.html

[root@test2 ~]# /etc/init.d/httpd start


客户端测试(物理机: 172.25.1.250 ):
[root@foundation1 ~]# vim /etc/hosts //作解析


[root@foundation1 ~]# ping www.westos.org
PING www.westos.org (172.25.1.1) 56(84) bytes of data.
64 bytes from www.westos.org (172.25.1.1): icmp_seq=1 ttl=64 time=0.175
ms
^C
--- www.westos.org ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 999ms
rtt min/avg/max/mdev = 0.145/0.160/0.175/0.015 ms


[root@foundation1 ~]# curl 172.25.1.1 -I
curl: (7) Failed connect to 172.25.1.1:80; No route to host
注意:可能的原因可能是 server1 端:
1.server1 和 server2 的 iptables 没有关,执行/etc/init.d/iptables stop2.查看 varnish 是否在 80 端口的服务 执行 netstat -antlp |grep :80 看 80 端口
是不是 varnish 服务。这种情况说明服务没配好,回去检查服务。
3. ip addr //查看 ip 是否在同网段,网是否可以 ping 通
4. /etc/init.d/varnish stop //关闭服务
/etc/init.d/varnish start //开启服务
/etc/init.d/varnish restart //重起服务
[root@foundation1 ~]# curl 172.25.1.1 -I
HTTP/1.1 200 OK
Server: Apache/2.2.15 (Red Hat)
Last-Modified: Wed, 26 Sep 2018 04:37:35 GMT
ETag: "3fef7-18-576bec8076d35"
Content-Type: text/html; charset=UTF-8
Content-Length: 24
Accept-Ranges: bytes
Date: Wed, 26 Sep 2018 04:55:18 GMT
X-Varnish: 1996073865
Age: 0
Via: 1.1 varnish
Connection: keep-alive
X-Cache: MISS from ying cache



浏览器可以查看到服务器 1 的发布目录如下图:
清缓存( server1 上进行):
1、[root@test1 varnish]# varnishadm ban.url .*$ 清除所有(建议慎用)
2、[root@test1 varnish]# varnishadm ban.url /index.html


页面缓存,清除首页,在 client 查看时 age 是从 0 开始
//清除 index.htmlserver1 端清缓存后:

[root@foundation1 ~]# curl 172.25.1.1 -I
HTTP/1.1 200 OK
Server: Apache/2.2.15 (Red Hat)
Last-Modified: Wed, 26 Sep 2018 04:37:35 GMT
ETag: "3fef7-18-576bec8076d35"
Content-Type: text/html; charset=UTF-8
Content-Length: 24
Accept-Ranges: bytes
Date: Wed, 26 Sep 2018 04:55:18 GMT
X-Varnish: 1996073865
Age: 0
Via: 1.1 varnish
Connection: keep-alive
X-Cache: MISS from ying cache

3、varnishadm ban.url /admin/$ 清除 admin 目录缓存


配置多个不同域名站点的后端服务器:
服务器 2 ( server3:172.25.1.3) :
[root@test3 ~]# yum install -y httpd
[root@test3 ~]# vim /var/www/html/index.html


[root@test3 ~]# cat /var/www/html/index.html
[<h1>bbs.westos.org</h1>
[root@test3 ~]# /etc/init.d/httpd start
cdn(server1 端 ):
[root@test1 varnish]# vim default.vcl

[root@test1 varnish]# /etc/init.d/varnish restart
[root@test1 varnish]# /etc/init.d/varnish reload

客户端测试:

//此时会报错,提示 404 (不能以 IP 的方式访问)

[root@foundation1 Desktop]# curl www.westos.org/index.html -I
HTTP/1.1 200 OK
Server: Apache/2.2.15 (Red Hat)
Last-Modified: Sat, 28 Jul 2018 03:48:35 GMT
ETag: "a0043-f-572071a4cb6e9"
Content-Type: text/html; charset=UTF-8
Content-Length: 15
Accept-Ranges: bytes
Date: Sat, 28 Jul 2018 05:53:00 GMT
X-Varnish: 1621918284 1621918278
Age: 64
Via: 1.1 varnish
Connection: keep-alive
X-Cache: HIT from ying cache
[root@foundation1 ~]# curl bbs.westos.org/index.html -I
HTTP/1.1 200 OK
Server: Apache/2.2.15 (Red Hat)
Last-Modified: Wed, 26 Sep 2018 05:14:18 GMT
ETag: "40008-18-576bf4b6078f0"Content-Type: text/html; charset=UTF-8
Content-Length: 24
Accept-Ranges: bytes
Date: Wed, 26 Sep 2018 13:53:29 GMT
X-Varnish: 1962699923
Age: 0
Via: 1.1 varnish
Connection: keep-alive
X-Cache: MISS from westos cache
[root@foundation1 ~]# curl bbs.westos.org/index.html -I
HTTP/1.1 200 OK
Server: Apache/2.2.15 (Red Hat)
Last-Modified: Wed, 26 Sep 2018 05:14:18 GMT
ETag: "40008-18-576bf4b6078f0"
Content-Type: text/html; charset=UTF-8
Content-Length: 24
Accept-Ranges: bytes
Date: Wed, 26 Sep 2018 13:53:41 GMT
X-Varnish: 1962699924 1962699923
Age: 12
Via: 1.1 varnish
Connection: keep-alive
X-Cache: HIT from westos cache


负载均衡
服务器 2 :

[root@test3 ~]# vim /etc/httpd/conf/httpd.conf
NameVirtualHost *:80


[root@test3 ~]# /etc/init.d/httpd restart
[root@test3 ~]# mkdir /www1
[root@test3 ~]# vim /www1/index.html
[root@test3 ~]# cat /www1/index.html            //编写一个新发布目录

[root@test3 ~]# /etc/init.d/httpd restart
<h1>www.westos.org-server3</h1>
[root@test3 ~]# /etc/init.d/httpd restart
[root@test2 html]# vim /var/www/html/index.html
<h1>www.westos.org-server2</h1>
[root@test2 ~]# /etc/init.d/httpd restart
[root@test1 varnish]# vim default.vcl

return (pass); 作用相当于一个负载均衡器。

注释:加上 pass 时相当于一个负载均衡器,不进行缓存,两个服务器交换使用
在客户端测试如下:
[root@test1 varnish]# /etc/init.d/varnish reload客户端测试:
[root@foundation1 ~]# curl www.westos.org
<h1>www.westos.org-server2</h1>
[root@foundation1 ~]# curl www.westos.org
<h1>www.westos.org-server3</h1>
[root@foundation1 ~]# curl www.westos.org
<h1>www.westos.org-server2</h1>
[root@foundation1 ~]# curl www.westos.org
<h1>www.westos.org-server3</h1>
[root@foundation1 ~]# curl www.westos.org
<h1>www.westos.org-server2</h1>
[root@foundation1 ~]# curl www.westos.org
<h1>www.westos.org-server3</h1>


[root@foundation1 ~]# curl www.westos.org/index.html -I
HTTP/1.1 200 OK
Server: Apache/2.2.15 (Red Hat)
Last-Modified: Wed, 26 Sep 2018 15:35:40 GMT
ETag: "bf406-20-576c7f98990a8"
Accept-Ranges: bytes
Content-Length: 32
Content-Type: text/html; charset=UTF-8
Accept-Ranges: bytes
Date: Wed, 26 Sep 2018 15:45:54 GMT
X-Varnish: 360317503
Age: 0
Via: 1.1 varnish
Connection: keep-alive
X-Cache: MISS from westos cache
[root@foundation1 ~]# curl www.westos.org/index.html -I
HTTP/1.1 200 OK
Server: Apache/2.2.15 (Red Hat)
Last-Modified: Wed, 26 Sep 2018 14:58:48 GMT
ETag: "40007-20-576c775b24887"
Accept-Ranges: bytes
Content-Length: 32
Content-Type: text/html; charset=UTF-8
Accept-Ranges: bytes
Date: Wed, 26 Sep 2018 15:45:56 GMT
X-Varnish: 360317504
Age: 0
Via: 1.1 varnish
Connection: keep-aliveX-Cache: MISS from westos cache
注释:不加 pass 时,会访问第一个缓存的记录,在到达访问限制次数 120 时,再访问并
缓存另外一个正常工作的服务器,当一个服务器坏的时候还会继续访问此服务器的缓存,次
数到达 120 时,再访问正常的服务器进行缓存
[root@test1 varnish]# vim default.vcl                 //将 return (pass);所在的行注释掉


[root@foundation1 ~]# curl www.westos.org
<h1>www.westos.org-server2</h1>
[root@foundation1 ~]# curl www.westos.org
<h1>www.westos.org-server2</h1>
[root@foundation1 ~]# curl www.westos.org
<h1>www.westos.org-server2</h1>
[root@foundation1 ~]# curl www.westos.org
<h1>www.westos.org-server2</h1>
[root@foundation1 ~]# curl www.westos.org/index.html -I
HTTP/1.1 200 OK
Server: Apache/2.2.15 (Red Hat)
Last-Modified: Wed, 26 Sep 2018 15:35:40 GMT
ETag: "bf406-20-576c7f98990a8"
Content-Type: text/html; charset=UTF-8
Content-Length: 32
Accept-Ranges: bytes
Date: Wed, 26 Sep 2018 15:41:46 GMT
X-Varnish: 360317473
Age: 0
Via: 1.1 varnish
Connection: keep-alive
X-Cache: MISS from westos cache
[root@foundation1 ~]# curl www.westos.org/index.html -I
HTTP/1.1 200 OK
Server: Apache/2.2.15 (Red Hat)
Last-Modified: Wed, 26 Sep 2018 15:35:40 GMT
ETag: "bf406-20-576c7f98990a8"
Content-Type: text/html; charset=UTF-8
Content-Length: 32
Accept-Ranges: bytes
Date: Wed, 26 Sep 2018 15:43:45 GMT
X-Varnish: 360317499 360317473
Age: 119
Via: 1.1 varnish
Connection: keep-alive
X-Cache: HIT from westos cache[root@foundation1 ~]# curl www.westos.org/index.html -I
HTTP/1.1 200 OK
Server: Apache/2.2.15 (Red Hat)
Last-Modified: Wed, 26 Sep 2018 14:58:48 GMT
ETag: "40007-20-576c775b24887"
Content-Type: text/html; charset=UTF-8
Content-Length: 32
Accept-Ranges: bytes
Date: Wed, 26 Sep 2018 15:43:47 GMT
X-Varnish: 360317500
Age: 0
Via: 1.1 varnish
Connection: keep-alive
X-Cache: MISS from westos cache
[root@foundation69 Desktop]# curl www.westos.org
www.westos.org -server3
//查看服务器是否已经换掉


Varnish cdn 推送平台

[root@test1 varnish]# yum install httpd -y
[root@test1 varnish]#vim /etc/httpd/conf/httpd.conf

[root@test1 varnish]# /etc/init.d/httpd restart
[root@test1 ~]# yum install php -y
[root@test1 varnish]# yum install -y unzip
[root@test1 home]# ls
bansys.zip
[root@test1 home]# unzip bansys.zip -d /var/www/html/
[root@test1 home]# cd /var/www/html/
[root@test1 html]# ls
Bansys
[root@test1 html]# cd bansys
[root@test1 bansys]# ls
class_socket.php config.php index.php purge_action.php static
[root@test1 bansys]# mv * ..
[root@test1 bansys]# ls
[root@test1 bansys]# cd ..
[root@test1 html]# ls
bansys class_socket.php config.php index.php purge_action.php
static[root@test1 html]# vim config.php
<?php
$var_group1 = array(
);
'host' => array('172.25.1.1),
'port' => '80',
//varnish 群组定义
//对主机列表进行绑定
$VAR_CLUSTER = array(
'www.westos.org' => $var_group1,
);
//varnish 版本
//2.x 和 3.x 推送命令不一样
$VAR_VERSION = "3";
?>
[[root@test1 html]# /etc/init.d/httpd restart
root@test1 html]# netstat -antlp
[root@test1 html]# vim /etc/varnish/default.vcl
//添加内容:



[root@test1 html]# /etc/init.d/varnish reload
[root@test1 varnish]# varnishadm ban.url /index.html 清缓存


客户端测试:
发送之前


[root@foundation1 ~]# curl www.westos.org/index.html
<h1>www.westos.org-server3</h1>
[root@foundation1 ~]# curl www.westos.org/index.html
<h1>www.westos.org-server3</h1>
[root@foundation1 ~]# curl www.westos.org/index.html
<h1>www.westos.org-server3</h1>


发送之后


[root@foundation1 ~]# curl www.westos.org/index.html
<h1>www.westos.org-server2</h1>
[root@foundation1 ~]# curl www.westos.org/index.html
<h1>www.westos.org-server2</h1>
[root@foundation1 ~]# curl www.westos.org/index.html
<h1>www.westos.org-server2</h1>
[root@foundation1 ~]# curl www.westos.org/index.html
//查看时有变化,则推送成功。

若在浏览器没有变化,可能是因为浏览器有自己的缓存

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值