cdn
CDN的全称是Content Delivery Network,即内容分发网络。其基本思路是尽可能避开互联网上有可能影响数据传输速度和稳定性的瓶颈和环节,使内容传输的更快、更稳定。
缓存 –> 加速速度
缓存系统cdn
加速网站发布速度
cdn:
cache
squid –>磁盘,并联,性能最差
varnish –>内存,单点,url哈希,易失性,缓存命中率最高,性能最好
nginx –>第三方模块,插件
SLV 阿里自研,不需要reload,推送即直接同步,ms级别
阿里云
业务逻辑
Tengine(业务) -> cache(缓存) -> 存储
–>推荐:云cdn 云计算 Akamai
varnish
varnish建立
可以从官网获取varnish的安装包
(1)(varnish端)server1:
yum install * -y ##安装varnish
vim /etc/sysconfig/varnish ##主配置文件
VARNISH_LISTEN_PORT=80 ##监听80端口
/etc/init.d/varnish start ##开启varnish
vim /etc/varnish/default.vcl ##默认文件
backend default {
.host = "172.25.32.2"; ##主服务端
.port = "80"; ##端口
}
##查看缓存命中
sub vcl_deliver {
if (obj.hits > 0) {
set resp.http.X-Cache = "HIT from westos cache"; ##hit命中
}
else {
set resp.http.X-Cache = "MISS from westos cache"; ##miss命中第一下
}
return (deliver);
}
/etc/init.d/varnish reload ##重新加载varnish
Loading vcl from /etc/varnish/default.vcl
Current running config name is boot
Using new config name reload_2017-07-20T06:53:13
VCL compiled.
available 0 boot
active 0 reload_2017-07-20T06:53:13
Done ##这里表示重新加载成功
(2)(server服务器端)server2:
yum install httpd -y
service httpd start ##开启httpd服务
vim /var/www/html/index.html ##编辑apache默认发布页
(3)测试端
/etc/hosts
172.25.32.1 server1 www.westos.com
测试:
浏览器访问 172.25.32.1 和 www.westos.org
发现页面是server2的页面
curl www.westos.org -I
MISS未命中
HIT
手动清除缓存
varnishadm ban.url .*$ ##清除所有缓存
curl www.westos.org -I
varnishadm ban.url / ##清除站的缓存
curl www.westos.org -I
varnishadm ban.url /index.html ##清除站的默认访问页面的缓存
curl www.westos.org -I
curl www.westos.org/index.html -I
测试:
curl www.westos.org -I
MISS | HIT
多域名站点
(1)server1(varnish端):
vim /etc/varnish/default.vcl
backend web1 {
.host = "172.25.32.2";
.port = "80";
}
backend web2 {
.host = "172.25.32.3";
.port = "80";
}
sub vcl_recv {
if (req.http.host ~ "^(www.)?westos.org") {
set req.http.host = "www.westos.org"; ##以www.开头或不开头的westos.org默认到www.westos.org
set req.backend = web1; ##在web1上取数据
} elsif (req.http.host ~ "^bbs.westos.org") { ##bbs.westos.org则在web2取数据
set req.backend = web2;
} else {error 404 "westos cache"; ##其他(即ip)则显示404错误
}
}
/etc/init.d/varnish reload ##重新加载varnish服务
(2)测试端
vim /etc/hosts
172.25.32.1 server1.example.com www.westos.org westos.org bbs.westos.org
测试:
[root@foundation32 ~]# curl www.westos.org
<h1>server2</h1>
[root@foundation32 ~]# curl westos.org
<h1>server2</h1>
[root@foundation32 ~]# curl bbs.westos.org
<h1>server3</h1>
[root@foundation32 ~]# curl 172.25.32.1
也可以用浏览器测试:
负载均衡
(1)server1(varnish):
vim /etc/varnish/default.vcl
director lb round-robin {
{ .backend = web1; }
{ .backend = web2; }
}
sub vcl_recv {
if (req.http.host ~ "^(www.)?westos.org") {
set req.http.host = "www.westos.org";
set req.backend = lb; ##www.westos指定lb
return (pass); ##直接varnish到服务端轮询
} elsif (req.http.host ~ "^bbs.westos.org") {
set req.backend = web1; ##bbs指定web1
} else {error 404 "westos cache";
}
}
/etc/init.d/varnish reload
(2)server2(server服务端):
建立apache虚拟主机
vim /etc/httpd/conf/httpd.conf
NameVirtualHost *:80
<VirtualHost *:80>
DocumentRoot /var/www/html
ServerName server2
</VirtualHost>
<VirtualHost *:80> ##bbs虚拟主机
DocumentRoot /www/bbs
ServerName bbs.westos.org
</VirtualHost>
<VirtualHost *:80> ##www.westos虚拟主机
DocumentRoot /www/westos
ServerName www.westos.org
</VirtualHost>
mkdir -p /www/bbs
mkdir /www/westos
vim /www/bbs/index.html
vim /www/westos/index.html
/etc/init.d/httpd restart ##重启httpd服务
(3)server3(server服务端):
vim /var/www/html/index.html
(4)测试
varnishadm ban.url /
curl www.westos.org
curl bbs.westos.org
不加pass时:需要清除缓存才能跳到另一个页面(页面缓存时间是120s)
加了pass时:
varnish cdn推送平台
(1)varnish端(server1):
yum install php -y ##下载php
yum install httpd -y ##下载apache
vim /etc/httpd/conf/httpd.conf ##编辑apache配置文件
Listen 8080 ##使http监听8080端口(varnish已经监听了80端口)
unzip bansys.zip -d /var/www/html/
mv /var/www/html/bansys/* /var/www/html
rm -fr /var/www/html/bansys/
vim /var/www/html/config.php ##编辑推送平台配置
//varnish主机列表
//可定义多个主机列表
$var_group1 = array(
'host' => array('172.25.32.1',),
'port' => '80',
);
//varnish群组定义
//对主机列表进行绑定
$VAR_CLUSTER = array(
'www.westos.org' => $var_group1,
);
//varnish版本
//2.x和3.x推送命令不一样
$VAR_VERSION = "3";
/etc/init.d/httpd start ##开启httpd服务
vim /etc/varnish/default.vcl ##编辑配置文件
需要把pass注释掉
acl westos { ##westos的访问控制
"127.0.0.1";
"172.25.32.0"/24;
}
sub vcl_recv {
if (req.request == "BAN") {
if (!client.ip ~ westos) {
error 405 "Not allowed.";
}
ban("req.url ~ " + req.url);
error 200 "ban added";
}
}
/etc/init.d/varnish reload ##重新加载varnish服务
(2)测试:
浏览器访问 www.westos.org/index.html
浏览器访问 172.25.32.1:8080
点击确认上传后
刷新后