CDN的简介:
CDN:内容分发网络。其基本思路是尽可能避开互联网上有可能影响数据传输速度和稳定性的瓶颈和环节,使内容传输得更快、
更稳定。通过在网络各处放置节点服务器所构成的在现有的互联网基础之上的一层智能虚拟网络,CDN系统能够实时地根据
网络流量和各节点的连接、负载状况以及到用户的距离和响应时间等综合信息将用户的请求重新导向离用户最近的服务节点上。
其目的是使用户可就近取得所需内容,解决 Internet网络拥挤的状况,提高用户访问网站的响应速度。
使用CDN的好处:
1. 不用担心自己网站访客,在任何时间,任何地点,任何网络运营商,都能快速打开网站。
2. 各种服务器虚拟主机带宽等采购成本,包括后期运维成本都会大大减少。
3. 给网站直接带来的好处就是:流量,咨询量,客户量,成单量,都会得到大幅度提升。
用varnish实现cdn:
在当前主流的Web架构中,Cache担任着越来越重要的作用。资源的关键。而最近几年由FreeBSD创始人之一Kamp开发的
varnish更是一个不可多得的Web Cache Server。严格意义上说,Varnish是一个高性能的反向代理软件,只不过与其出色的
缓存功能相比,企业更愿意使用其搭建缓存服务器。同时,由于其工作在Web Server的前端,有一部分企业已经在生产环境
中使用其作为旧版本的squid的替代方案,以在相同的服务器成本下提供更好的缓存效果,Varnish更是作为CDN缓存服务器
的可选服务之一。
实验1:查看缓存命中情况:
实验环境:
server1:172.25.38.2这个是varnish端
server2:172.25.38.3这个是后端服务器
1.准备安装包:
varnish-3.0.5-1.el6.x86_64.rpm varnish安装包
varnish-libs-3.0.5-1.el6.x86_64.rpm
这个yum源必须开启apache:
[root@foundation38 yum.repos.d]# netstat -antlp |grep :80
2.在server1虚拟机安装scp命令:
[root@server1 yum.repos.d]# yum whatprovides */scp 查找安装包
Loaded plugins: product-id, subscription-manager
This system is not registered to Red Hat Subscription Management. You can use subscription-manager to register.
rhel-source/filelists_db | 3.8 MB 00:00
openssh-clients-5.3p1-94.el6.x86_64 : An open source SSH client applications
Repo : rhel-source
Matched from:
Filename : /usr/bin/scp
[root@server1 yum.repos.d]# yum install openssh-clients-5.3p1-94.el6.x86_64 安装
3.下载varnish安装包:
4.安装varnish:
[root@server1 ~]# ls
anaconda-ks.cfg install.log.syslog varnish-libs-3.0.5-1.el6.x86_64.rpm
install.log varnish-3.0.5-1.el6.x86_64.rpm
[root@server1 ~]# yum install * -y 安装varnish
5.配置 varnish 服务端口:
[root@server1 ~]# vim /etc/sysconfig/varnish 更改为80端口
[root@server1 ~]# /etc/init.d/varnish reload
6.编辑限制cpu最大处理文件:
[root@server1 ~]# vim /etc/security/limits.conf 在尾部添加的参数是与varnish主配置文件里面对应的
7.在/etc/varnish/default.vcl建立一个后端IP
[root@server1 ~]# vim /etc/varnish/default.vcl 写入的是后端服务器的IP地址
[root@server1 ~]# /etc/init.d/varnish restart
Stopping Varnish Cache: [ OK ]
Starting Varnish Cache: [ OK ]
6.配置一个后端服务器server2:
安装httpd服务:
打开apache服务:
建立默认访问目录:
在真机添加解析,写入的是varnish端,也就是server1的IP地址:
在浏览器访问用IP和域名访问均可以访问默认发布目录:
如果访问不到在server2重启httpd服务,真机关闭防火墙开启httpd服务,server1重启varnish服务
企业6的服务配置/etc/init.d/服务 状态即可.
在varnish端添加查看缓存命中情况:
[root@server1 ~]# vim /etc/varnish/default.vcl
[root@server1 ~]# /etc/init.d/varnish restart
Stopping Varnish Cache: [ OK ]
Starting Varnish Cache: [ OK ]
[root@server1 ~]#
在真机测试:
# curl www.westos.org -I
HTTP/1.1 200 OK
Server: Apache/2.2.15 (Red Hat)
Last-Modified: Sat, 28 Jul 2018 05:20:49 GMT
ETag: "ff417-f-572086431c749"
Content-Type: text/html; charset=UTF-8
Content-Length: 15
Accept-Ranges: bytes
Date: Sat, 28 Jul 2018 07:13:15 GMT
X-Varnish: 1390114403
Age: 0
Via: 1.1 varnish
Connection: keep-alive
X-Cache: MISS from westos cache 未命中
[root@foundation38 企业第一天]# curl www.westos.org -I
HTTP/1.1 200 OK
Server: Apache/2.2.15 (Red Hat)
Last-Modified: Sat, 28 Jul 2018 05:20:49 GMT
ETag: "ff417-f-572086431c749"
Content-Type: text/html; charset=UTF-8
Content-Length: 15
Accept-Ranges: bytes
Date: Sat, 28 Jul 2018 07:13:17 GMT
X-Varnish: 1390114404 1390114403
Age: 1
Via: 1.1 varnish
Connection: keep-alive
X-Cache: HIT from westos cache
命中之后等到Age到了120周期之后会自动变成未命中
Age是缓存周期时间就是varnish里面的age到了120就会改变一次
实验2:定义多个不同域名站点的后端服务器:
配置server3虚拟机:(同样方法进行配置)
[root@foundation38 images]# qemu-img create -f qcow2 -b xfl.qcow2 test3
Formatting 'test3', fmt=qcow2 size=21474836480 backing_file='xfl.qcow2' encryption=off cluster_size=65536 lazy_refcounts=off
安装httpd:
打开httpd:
建立默认发布目录:
当访问 www.westos.org 域名时从 web1 上取数据,访问 bbs.westos.org 域名时到 web2 取数据, 访问其他页面报错。
1.更改配置文件:
[root@server1 ~]# vim /etc/varnish/default.vcl
[root@server1 ~]# /etc/init.d/varnish restart
Stopping Varnish Cache: [ OK ]
Starting Varnish Cache: [ OK ]
在真机添加解析:
在真机测试:
全部都是未命中,因为配置文件中不识别其他IP,只有域名
[root@foundation38 ~]# curl 172.25.38.2/index.html -I
HTTP/1.1 404 westos cache
Server: Varnish
Content-Type: text/html; charset=utf-8
Retry-After: 5
Content-Length: 397
Accept-Ranges: bytes
Date: Sat, 28 Jul 2018 07:32:49 GMT
X-Varnish: 926380693
Age: 0
Via: 1.1 varnish
Connection: close
X-Cache: MISS from westos cache
[root@foundation38 ~]# curl 172.25.38.2/index.html -I
HTTP/1.1 404 westos cache
Server: Varnish
Content-Type: text/html; charset=utf-8
Retry-After: 5
Content-Length: 397
Accept-Ranges: bytes
Date: Sat, 28 Jul 2018 07:32:52 GMT
X-Varnish: 926380694
Age: 0
Via: 1.1 varnish
Connection: close
X-Cache: MISS from westos cache
可以识别www.westos.org,会自动从命中变成未命中
[root@foundation38 ~]# 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 05:20:49 GMT
ETag: "ff417-f-572086431c749"
Content-Type: text/html; charset=UTF-8
Content-Length: 15
Accept-Ranges: bytes
Date: Sat, 28 Jul 2018 07:33:09 GMT
X-Varnish: 926380695
Age: 0
Via: 1.1 varnish
Connection: keep-alive
X-Cache: MISS from westos cache
[root@foundation38 ~]# 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 05:20:49 GMT
ETag: "ff417-f-572086431c749"
Content-Type: text/html; charset=UTF-8
Content-Length: 15
Accept-Ranges: bytes
Date: Sat, 28 Jul 2018 07:33:10 GMT
X-Varnish: 926380696 926380695
Age: 1
Via: 1.1 varnish
Connection: keep-alive
X-Cache: HIT from westos cache
可以识别bbs.westos.org,会自动从命中变成未命中
[root@foundation38 ~]# curl bbs.westos.org/index.html -I
HTTP/1.1 200 OK
Server: Apache/2.2.15 (Red Hat)
Last-Modified: Sat, 28 Jul 2018 07:28:43 GMT
ETag: "ff417-10-5720a2d98d357"
Content-Type: text/html; charset=UTF-8
Content-Length: 16
Accept-Ranges: bytes
Date: Sat, 28 Jul 2018 07:34:42 GMT
X-Varnish: 926380697
Age: 0
Via: 1.1 varnish
Connection: keep-alive
X-Cache: MISS from westos cache
[root@foundation38 ~]# curl bbs.westos.org/index.html -I
HTTP/1.1 200 OK
Server: Apache/2.2.15 (Red Hat)
Last-Modified: Sat, 28 Jul 2018 07:28:43 GMT
ETag: "ff417-10-5720a2d98d357"
Content-Type: text/html; charset=UTF-8
Content-Length: 16
Accept-Ranges: bytes
Date: Sat, 28 Jul 2018 07:34:43 GMT
X-Varnish: 926380698 926380697
Age: 1
Via: 1.1 varnish
Connection: keep-alive
X-Cache: HIT from westos cache
实验3:定义负载均衡:
在server3后端服务器配置阿帕奇配置文件:
[root@server3 html]# vim /etc/httpd/conf/httpd.conf 更改配置文件
[root@server3 html]# /etc/init.d/httpd restart
[root@server3 html]# mkdir /www1
[root@server3 html]# cd /www1/
[root@server3 www1]# ls
[root@server3 www1]# vim index.html
[root@server3 www1]# cat index.html
www.westos.org ->server3
[root@server3 www1]# vim /etc/hosts 添加本地解析
[root@server3 www1]# curl bbs.westos.org 在本机可以查看发布目录中的文件
bbs.westos.html
[root@server3 www1]# curl www.westos.org
www.westos.org ->server3
在server1把多个后端聚合为一个组并检查健康情况:
[root@server1 ~]# vim /etc/varnish/default.vcl
[root@server1 ~]# /etc/init.d/varnish restart
Stopping Varnish Cache: [ OK ]
Starting Varnish Cache: [ OK ]
在真机测试一直是www.westos.org:因为已经进行了缓存
[root@foundation38 ~]# curl www.westos.org
www.westos.org
[root@foundation38 ~]# curl www.westos.org
www.westos.org
[root@foundation38 ~]# curl www.westos.org
www.westos.org
[root@foundation38 ~]# curl www.westos.org
www.westos.org
在server1清理缓存:
通过 varnishadm 手动清除缓存
varnishadm ban.url .*$ 清除所有
varnishadm ban.url /index.html 清除 index.html 页面缓存
varnishadm ban.url /admin/$ 清除 admin 目录缓存
[root@server1 ~]# varnishadm ban.url /index.html
在真机测试会改变因为已经清理了缓存
[root@foundation38 ~]# curl www.westos.org
www.westos.org ->server3
[root@foundation38 ~]# curl www.westos.org
www.westos.org ->server3
为了测试方便,不进行缓存,在server1加入return (pass);意思是不经过缓存直接到后端取数据
[root@server1 ~]# vim /etc/varnish/default.vcl
[root@server1 ~]# /etc/init.d/varnish restart
Stopping Varnish Cache: [ OK ]
Starting Varnish Cache: [ OK ]
[root@server1 ~]#
在真机测试会进行一直转换因为没有进行缓存:
[root@foundation38 ~]# curl www.westos.org
www.westos.org
[root@foundation38 ~]# curl www.westos.org
www.westos.org ->server3
[root@foundation38 ~]# curl www.westos.org
www.westos.org
varnish 推送平台搭建:
保证上一个实验负载均衡可以实现,将server2,server3和主机开启httpd关闭防火墙。server1重启varnish。
1.在server1安装httpd并更改端口为8080,不然会端口冲突:
[root@server1 ~]# yum install httpd -y 安装阿帕奇
[root@server1 ~]# vim /etc/httpd/conf/httpd.conf 编辑配置文件
[root@server1 ~]# /etc/init.d/httpd restart 重启服务
[root@server1 ~]# /etc/init.d/httpd restart
更改8080端口:
2.需要安装php支持才可以:
[root@server1 ~]# yum install -y php
3.下载bansys.zip的压缩包并解压到http的默认发布目录下 :
[root@server1 ~]# ls
anaconda-ks.cfg install.log varnish-3.0.5-1.el6.x86_64.rpm :wq
bansys.zip install.log.syslog varnish-libs-3.0.5-1.el6.x86_64.rpm
[root@server1 ~]# yum install -y unzip 安装解压文件
[root@server1 ~]# unzip bansys.zip -d /var/www/html/ 解压到指定目录
[root@server1 ~]# cd /var/www/html/
[root@server1 html]# ls
bansys
[root@server1 html]# cd bansys/
[root@server1 bansys]# ls
class_socket.php config.php index.php purge_action.php static
[root@server1 bansys]# mv * .. 将所有文件移动到阿帕奇默认发布目录下才可以被访问到
[root@server1 bansys]# ls
[root@server1 bansys]# cd ..
[root@server1 html]# ls
bansys class_socket.php config.php index.php purge_action.php static
4.配置解压后的config.php 文件:
[root@server1 html]# vim config.php 编辑php配置文件
<?php
//varnish主机列表
//可定义多个主机列表
$var_group1 = array(
'host' => array('172.25.38.2'), #varnish主机ip
'port' => '80', # varnish端口
);
//varnish群组定义
//对主机列表进行绑定
$VAR_CLUSTER = array(
'www.westos.org' => $var_group1,
);
//varnish版本
//2.x和3.x推送命令不一样
$VAR_VERSION = "3";
?>
[root@server1 html]# ls
bansys class_socket.php config.php index.php purge_action.php static
[root@server1 html]# /etc/init.d/httpd restart 重启服务
Stopping httpd: [ OK ]
Starting httpd: httpd: Could not reliably determine the server's fully qualified domain name, using 172.25.38.2 for ServerName
[ OK ]
[root@server1 html]# netstat -antlp 可以看到8080端口
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 1232/varnishd
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 909/sshd
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 985/master
tcp 0 0 127.0.0.1:6082 0.0.0.0:* LISTEN 1231/varnishd
tcp 0 0 172.25.38.2:22 172.25.38.250:42018 ESTABLISHED 1034/sshd
tcp 0 0 :::8080 :::* LISTEN 1474/httpd
tcp 0 0 :::80 :::* LISTEN 1232/varnishd
tcp 0 0 :::22 :::* LISTEN 909/sshd
tcp 0 0 ::1:25 :::* LISTEN 985/master
5.在网页可以访问:
6.编辑varnish端配置文件:
[root@server1 html]# cd /etc/varnish/
[root@server1 varnish]# ls
default.vcl secret
[root@server1 varnish]# vim default.vcl 编辑配置文件
[root@server1 varnish]# /etc/init.d/varnish restart
设置访问控制:
#bansys 有两种工作模式,分别是:telnet 和 http 模式。
#telnet 模式需要关闭 varnish 服务管理端口的验证,注释掉/etc/sysconfig/varnish 文件中的 “ -S $
{VARNISH_SECRET_FILE}”这行,重启 varnish 服务即可。
#如果是 http 模式需要对 varnish 做以下设置: