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
?????????
//系統(tǒng)限制值 limits,根據(jù)文件/etc/sysconfig/varnish 里面的限制值進(jìn)行配置,對(duì) cpu不做限制
配置一個(gè)后端服務(wù)器 ( 在 cdn 上進(jìn)行配置 )
[root@server1 varnish]# /etc/init.d/varnish start???????? //開(kāi)啟 varnish 服務(wù)
[root@server1 varnish]# vim default.vcl?????? //配置后端服務(wù)器
[root@test1 varnish]# /etc/init.d/varnish reload
服務(wù)器 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
客戶端測(cè)試(物理機(jī): 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 沒(méi)有關(guān),執(zhí)行/etc/init.d/iptables stop2.查看 varnish 是否在 80 端口的服務(wù) 執(zhí)行 netstat -antlp |grep :80 看 80 端口
是不是 varnish 服務(wù)。這種情況說(shuō)明服務(wù)沒(méi)配好,回去檢查服務(wù)。
3. ip addr //查看 ip 是否在同網(wǎng)段,網(wǎng)是否可以 ping 通
4. /etc/init.d/varnish stop //關(guān)閉服務(wù)
/etc/init.d/varnish start //開(kāi)啟服務(wù)
/etc/init.d/varnish restart //重起服務(wù)
[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
瀏覽器可以查看到服務(wù)器 1 的發(fā)布目錄如下圖:
清緩存( server1 上進(jìn)行):
1、[root@test1 varnish]# varnishadm ban.url .*$ 清除所有(建議慎用)
2、[root@test1 varnish]# varnishadm ban.url /index.html
頁(yè)面緩存,清除首頁(yè),在 client 查看時(shí) age 是從 0 開(kāi)始
//清除 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 目錄緩存
配置多個(gè)不同域名站點(diǎn)的后端服務(wù)器:
服務(wù)器 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
客戶端測(cè)試:
//此時(shí)會(huì)報(bào)錯(cuò),提示 404 (不能以 IP 的方式訪問(wèn))
[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
負(fù)載均衡
服務(wù)器 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??????????? //編寫(xiě)一個(gè)新發(fā)布目錄
[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); 作用相當(dāng)于一個(gè)負(fù)載均衡器。
注釋:加上 pass 時(shí)相當(dāng)于一個(gè)負(fù)載均衡器,不進(jìn)行緩存,兩個(gè)服務(wù)器交換使用
在客戶端測(cè)試如下:
[root@test1 varnish]# /etc/init.d/varnish reload客戶端測(cè)試:
[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 時(shí),會(huì)訪問(wèn)第一個(gè)緩存的記錄,在到達(dá)訪問(wèn)限制次數(shù) 120 時(shí),再訪問(wèn)并
緩存另外一個(gè)正常工作的服務(wù)器,當(dāng)一個(gè)服務(wù)器壞的時(shí)候還會(huì)繼續(xù)訪問(wèn)此服務(wù)器的緩存,次
數(shù)到達(dá) 120 時(shí),再訪問(wèn)正常的服務(wù)器進(jìn)行緩存
[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
//查看服務(wù)器是否已經(jīng)換掉
Varnish cdn 推送平臺(tái)
[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 群組定義
//對(duì)主機(jī)列表進(jìn)行綁定
$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
//添加內(nèi)容:
[root@test1 html]# /etc/init.d/varnish reload
[root@test1 varnish]# varnishadm ban.url /index.html 清緩存
客戶端測(cè)試:
發(fā)送之前
[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>
發(fā)送之后
[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
//查看時(shí)有變化,則推送成功。
若在瀏覽器沒(méi)有變化,可能是因?yàn)闉g覽器有自己的緩存
?
?
總結(jié)
以上是生活随笔為你收集整理的varnish 实现 CDN 缓存系统构建的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: LVS_DR实现(负载均衡)及LVS_D
- 下一篇: 3、LVS_TUN实现负载均衡