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
//查看時有變化,則推送成功。
若在瀏覽器沒有變化,可能是因為瀏覽器有自己的緩存
?
?
總結
以上是生活随笔為你收集整理的varnish 实现 CDN 缓存系统构建的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: LVS_DR实现(负载均衡)及LVS_D
- 下一篇: cifs------网络文件系统(1)