cdn 搭建 (server1:172.25.1.1 ) :
[[email protected] ~]# ls
varnish-3.0.5-1.el6.x86_64.rpm varnish-libs-3.0.5-1.el6.x86_64.rpm
[[email protected] ~]# yum install * -y
[[email protected] ~]# cd /etc/varnish/
[[email protected] varnish]# vim /etc/sysconfig/varnish
varnish 实现 CDN 缓存系统构建
[[email protected] varnish]# sysctl -a | grep file
varnish 实现 CDN 缓存系统构建
[[email protected] varnish]# vim /etc/security/limits.conf
       varnish 实现 CDN 缓存系统构建  

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

[[email protected] varnish]# vim default.vcl       //配置后端服务器
varnish 实现 CDN 缓存系统构建
[[email protected] varnish]# /etc/init.d/varnish reload
服务器 1 ( server2:172.25.1.2) :
[[email protected] ~]# yum install -y httpd
[[email protected] ~]# vim /var/www/html/index.html
varnish 实现 CDN 缓存系统构建
[[email protected] ~]# /etc/init.d/httpd start


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

varnish 实现 CDN 缓存系统构建
[[email protected] ~]# 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

varnish 实现 CDN 缓存系统构建
[[email protected] ~]# 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 //重起服务
[[email protected] ~]# 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

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

varnish 实现 CDN 缓存系统构建
页面缓存,清除首页,在 client 查看时 age 是从 0 开始
//清除 index.htmlserver1 端清缓存后:

[[email protected] ~]# 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) :
[[email protected] ~]# yum install -y httpd
[[email protected] ~]# vim /var/www/html/index.html

varnish 实现 CDN 缓存系统构建
[[email protected] ~]# cat /var/www/html/index.html
[<h1>bbs.westos.org</h1>
[[email protected] ~]# /etc/init.d/httpd start
cdn(server1 端 ):
[[email protected] varnish]# vim default.vcl

varnish 实现 CDN 缓存系统构建

[[email protected] varnish]# /etc/init.d/varnish restart
[[email protected] varnish]# /etc/init.d/varnish reload

客户端测试:

//此时会报错,提示 404 (不能以 IP 的方式访问)
varnish 实现 CDN 缓存系统构建
[[email protected] 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
[[email protected] ~]# 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
[[email protected] ~]# 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 :

[[email protected] ~]# vim /etc/httpd/conf/httpd.conf
NameVirtualHost *:80
varnish 实现 CDN 缓存系统构建
varnish 实现 CDN 缓存系统构建
[[email protected] ~]# /etc/init.d/httpd restart
[[email protected] ~]# mkdir /www1
[[email protected] ~]# vim /www1/index.html
[[email protected] ~]# cat /www1/index.html            //编写一个新发布目录

[[email protected] ~]# /etc/init.d/httpd restart
<h1>www.westos.org-server3</h1>
[[email protected] ~]# /etc/init.d/httpd restart
[[email protected] html]# vim /var/www/html/index.html
<h1>www.westos.org-server2</h1>
[[email protected] ~]# /etc/init.d/httpd restart
[[email protected] varnish]# vim default.vcl
varnish 实现 CDN 缓存系统构建
varnish 实现 CDN 缓存系统构建

return (pass); 作用相当于一个负载均衡器。
varnish 实现 CDN 缓存系统构建
注释:加上 pass 时相当于一个负载均衡器,不进行缓存,两个服务器交换使用
在客户端测试如下:
[[email protected] varnish]# /etc/init.d/varnish reload客户端测试:
[[email protected] ~]# curl www.westos.org
<h1>www.westos.org-server2</h1>
[[email protected] ~]# curl www.westos.org
<h1>www.westos.org-server3</h1>
[[email protected] ~]# curl www.westos.org
<h1>www.westos.org-server2</h1>
[[email protected] ~]# curl www.westos.org
<h1>www.westos.org-server3</h1>
[[email protected] ~]# curl www.westos.org
<h1>www.westos.org-server2</h1>
[[email protected] ~]# curl www.westos.org
<h1>www.westos.org-server3</h1>


[[email protected] ~]# 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
[[email protected] ~]# 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 时,再访问正常的服务器进行缓存
[[email protected] varnish]# vim default.vcl                 //将 return (pass);所在的行注释掉

varnish 实现 CDN 缓存系统构建
[[email protected] ~]# curl www.westos.org
<h1>www.westos.org-server2</h1>
[[email protected] ~]# curl www.westos.org
<h1>www.westos.org-server2</h1>
[[email protected] ~]# curl www.westos.org
<h1>www.westos.org-server2</h1>
[[email protected] ~]# curl www.westos.org
<h1>www.westos.org-server2</h1>
[[email protected] ~]# 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
[[email protected] ~]# 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[[email protected] ~]# 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
[[email protected] Desktop]# curl www.westos.org
www.westos.org -server3
//查看服务器是否已经换掉


Varnish cdn 推送平台

[[email protected] varnish]# yum install httpd -y
[[email protected] varnish]#vim /etc/httpd/conf/httpd.conf
varnish 实现 CDN 缓存系统构建
[[email protected] varnish]# /etc/init.d/httpd restart
[[email protected] ~]# yum install php -y
[[email protected] varnish]# yum install -y unzip
[[email protected] home]# ls
bansys.zip
[[email protected] home]# unzip bansys.zip -d /var/www/html/
[[email protected] home]# cd /var/www/html/
[[email protected] html]# ls
Bansys
[[email protected] html]# cd bansys
[[email protected] bansys]# ls
class_socket.php config.php index.php purge_action.php static
[[email protected] bansys]# mv * ..
[[email protected] bansys]# ls
[[email protected] bansys]# cd ..
[[email protected] html]# ls
bansys class_socket.php config.php index.php purge_action.php
static[[email protected] 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";
?>
[[[email protected] html]# /etc/init.d/httpd restart
[email protected] html]# netstat -antlp
[[email protected] html]# vim /etc/varnish/default.vcl
//添加内容:

varnish 实现 CDN 缓存系统构建

[[email protected] html]# /etc/init.d/varnish reload
[[email protected] varnish]# varnishadm ban.url /index.html 清缓存


客户端测试:
发送之前

varnish 实现 CDN 缓存系统构建
[[email protected] ~]# curl www.westos.org/index.html
<h1>www.westos.org-server3</h1>
[[email protected] ~]# curl www.westos.org/index.html
<h1>www.westos.org-server3</h1>
[[email protected] ~]# curl www.westos.org/index.html
<h1>www.westos.org-server3</h1>


发送之后

varnish 实现 CDN 缓存系统构建

varnish 实现 CDN 缓存系统构建
[[email protected] ~]# curl www.westos.org/index.html
<h1>www.westos.org-server2</h1>
[[email protected] ~]# curl www.westos.org/index.html
<h1>www.westos.org-server2</h1>
[[email protected] ~]# curl www.westos.org/index.html
<h1>www.westos.org-server2</h1>
[[email protected] ~]# curl www.westos.org/index.html
//查看时有变化,则推送成功。

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

 

 

相关文章:

  • 2020-04-09
  • 2022-12-23
  • 2021-12-16
  • 2021-08-12
  • 2017-12-07
  • 2021-07-29
猜你喜欢
  • 2021-09-30
  • 2022-01-14
  • 2021-06-01
  • 2021-05-31
  • 2022-01-16
  • 2022-01-10
  • 2022-12-23
相关资源
相似解决方案