CDN
CDN的全称是Content Delivery Network,即内容分发网络。其目的是通过在现有的Internet中增加一层新的网络架构,将网站的内容发布到最接近用户的网络"边缘",使用户可以就近取得所需的内容,解决Internet网络拥挤的状况,提高用户访问网站的响应速度。从技术上全面解决由于网络带宽小、用户访问量大、网点分布不均等原因所造成的用户访问网站响应速度慢的问题。
easy-cdn它使您能够快速、简单的部署CDN系统,所用工具为squid+bind(view),
物理机一台,三个6.5的虚拟机server1-server3
Server1:
(1)###安装varnish软件包
129 yum install varnish-3.0.5-1.el6.x86_64.rpm varnish-libs-3.0.5-1.el6.x86_64.rpm -y
(2)###配置一个后端服务器:
132 cd/etc/varnish/
133 ls
134 vim default.vcl
7 backend web1{
8 .host = "172.25.21.2";
9 .port = "80";
10 }
(3)###配置 varnish 服务端口:
135 vim/etc/sysconfig/varnish
66 VARNISH_LISTEN_PORT=80
137 /etc/init.d/varnish start ###开启varnish服务
138 netstat -antlp ####查看有没有80端口
截图:
<a href="https://s5.51cto.com/wyfs02/M00/9C/91/wKiom1lyHJDxgzujAAIjKh5jzX8303.png-wh_500x0-wm_3-wmp_4-s_3426906328.png" target="_blank"></a>
Server2:
安装httpd,在共享目录下建立共享文件建立
16 yum install httpd -y
17 /etc/init.d/httpd start
18 cd/var/www/html
19 ls
20 vim index.html
21 cat index.html
截图:
物理机测试:
[root@foundation21~]# vim /etc/hosts ###本地解析截图:
[root@foundation21~]# curl www.westos.org
<h1>server2</h1>
浏览器输入:172.25.21.1
<a href="https://s2.51cto.com/wyfs02/M00/9C/91/wKioL1lyHPagh0uSAAAszYlPjXI905.png-wh_500x0-wm_3-wmp_4-s_1801984989.png" target="_blank"></a>
2.缓存配置:
[root@server1varnish]# vim default.vcl
12 sub vcl_deliver {
13 if (obj.hits > 0) {
14 set resp.http.X-Cache = "HIT from westos cache";
15 }
16 else {
17 set resp.http.X-Cache = "MISS fromwestos cache";
18 }
19 return (deliver);
20 }
[root@server1 varnish]#/etc/init.d/varnish reload
物理机:
###测试缓存命中:
[root@foundation21~]# curl -I 172.25.21.1
HTTP/1.1 200 OK
Server:Apache/2.2.15 (Red Hat)
Last-Modified:Thu, 20 Jul 2017 02:43:21 GMT
ETag:"40026-12-554b6b639ddea"
Content-Type:text/html; charset=UTF-8
Content-Length: 18
Accept-Ranges:bytes
Date: Thu, 20 Jul2017 03:06:35 GMT
X-Varnish:1716317952
Age: 0
Via: 1.1 varnish
Connection:keep-alive
X-Cache: MISS from westos cache #未命中
Date: Thu, 20 Jul2017 03:08:04 GMT
X-Varnish:1716317953 1716317952
Age: 89
X-Cache: HIT from westos cache #命中
### 通过 varnishadm 手动清除缓存:
[root@server1 varnish]#varnishadm ban.url .*$ #清除所有
[root@server1 varnish]#varnishadm ban.url/index.html #清除 index.html 页面缓存
[root@server1varnish]# varnishadm ban.url /admin/$ #清除 admin 目录缓存
3. 定义多个不同域名站点的后端服务器
(1)###定义多个不同域名站点的后端服务器
backend web1 {
.host ="172.25.21.2";
.port ="80";
}
backend web2 {
.host ="172.25.21.3";
[root@server1varnish]# /etc/init.d/varnish reload
Loading vcl from/etc/varnish/default.vcl
Current runningconfig name is reload_2017-07-20T11:05:50
Using new configname reload_2017-07-20T11:22:36
Message fromVCC-compiler:
Unused backendweb2, defined:
('input' Line 11Pos 9)
--------####--
RunningVCC-compiler failed, exit 1
VCL compilationfailed
Command failedwith error code 106
varnishadm -S/etc/varnish/secret -T 127.0.0.1:6082 vcl.load failed
有问题需要进行(2)
(2)#当访问 www.westos.org 域名时从 web1 上取数据,访问 bbs.westos.org 域名时到 web2 取数据, 访问其他页面报错。
sub vcl_recv {
if (req.http.host~ "^(www.)?westos.org") {
set req.http.host= "www.westos.org";
set req.backend =web1;
} elsif(req.http.host ~ "^bbs.westos.org") {
set req.backend =web2;
} else {error 404"westos cache";
Using new configname reload_2017-07-20T11:31:05
VCL compiled.
available 0 boot
available 2 reload_2017-07-20T11:05:50
active 0 reload_2017-07-20T11:31:05
Done
配置文件内容截图:
<a href="https://s4.51cto.com/wyfs02/M01/9C/91/wKioL1lyHeyDo1KcAAD0XlMjfYk186.png-wh_500x0-wm_3-wmp_4-s_2061600261.png" target="_blank"></a>
server3:
10 yum install httpd -y
11 /etc/init.d/httpd start
12 cd /var/www/html
13 ls
14 vim index.html
15 cat index.html
<a href="https://s4.51cto.com/wyfs02/M02/9C/91/wKiom1lyHe2TaypYAABtqoEsvz0105.png-wh_500x0-wm_3-wmp_4-s_2872155601.png" target="_blank"></a>
[root@foundation21~]# curl 172.25.21.1 ##访问ip出错。只能访问域名
<?xmlversion="1.0" encoding="utf-8"?>
<!DOCTYPE htmlPUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<title>404 westos cache</title>
</head>
<body>
<h1>Error 404 westos cache</h1>
<p>westos cache</p>
<h3>Guru Meditation:</h3>
<p>XID: 1716317954</p>
<hr>
<p>Varnish cache server</p>
</body>
</html>
[root@foundation21~]# curl bbs.westos.org
<h1>server3</h1>
4.负载均衡
Server2:
[root@server2html]# mkdir /www/bbs -p
[root@server2html]# mkdir /www/westos
[root@server2html]# cd /www/bbs/
[root@server2bbs]# ls
[root@server2bbs]# vim /etc/httpd/conf/httpd.conf
<a href="https://s1.51cto.com/wyfs02/M00/9C/91/wKiom1lyHjzhCbnDAAGxig8n-js933.png-wh_500x0-wm_3-wmp_4-s_3276063580.png" target="_blank"></a>
<a href="https://s2.51cto.com/wyfs02/M00/9C/91/wKioL1lyHj3wPxjHAAEMjxcjoyc034.png-wh_500x0-wm_3-wmp_4-s_1712115715.png" target="_blank"></a>
[root@server2bbs]# /etc/init.d/httpd restart
Stoppinghttpd: [ OK ]
Startinghttpd: httpd: Could not reliably determine the server's fully qualified domainname, using 172.25.21.2 for ServerName
[ OK ]
[root@server2bbs]# vim /etc/hosts
[root@server2bbs]# vim index.html
[root@server2bbs]# cd /www/westos/
[root@server2westos]# ls
[root@server2westos]# vim index.html
Server1:
[root@server1 varnish]# vim default.vcl
[root@server1 varnish]# /etc/init.d/varnish reload
测试:
本文转自 如何何如 51CTO博客,原文链接:http://blog.51cto.com/12778805/1949906,如需转载请自行联系原作者