龙空技术网

cdn之高速缓存服务器的搭建和配置教学

飓风科技高防BGP 148

前言:

此时看官们对“mac1011apache配置”大约比较着重,同学们都想要知道一些“mac1011apache配置”的相关资讯。那么小编在网摘上搜集了一些有关“mac1011apache配置””的相关知识,希望你们能喜欢,我们快快来了解一下吧!

一、cdn

1.名词解释

以下引用自百度百科

CDN的全称是Content Delivery Network,即内容分发网络。其基本思路是尽可能避开互联网上有可能影响数据传输速度和稳定性的瓶颈和环节,使内容传输的更快、更稳定。通过在网络各处放置节点服务器所构成的在现有的互联网基础之上的一层智能虚拟网络,CDN系统能够实时地根据网络流量和各节点的连接、负载状况以及到用户的距离和响应时间等综合信息将用户的请求重新导向离用户最近的服务节点上。其目的是使用户可就近取得所需内容,解决 Internet网络拥挤的状况,提高用户访问网站的响应速度。

2.工作原理

CDN的基本原理是广泛采用各种缓存服务器,将这些缓存服务器分布到用户访问相对集中的地区或网络中,在用户访问网站时,利用全局负载技术将用户的访问指向距离最近的工作正常的缓存服务器上,由缓存服务器直接响应用户请求。

二、cdn高速缓存器varnish服务器

1.名词解释

Varnish是一款高性能的开源HTTP加速器

2.工作原理

用户通过浏览器访问http服务器,但是需要先经过http加速器varnish服务器,如果用户访问的内容在varnish的cache中,则直接从varnish服务器返回该访问内容,但是如果不在varnish的cache中,就先从后端的http服务器中取出用户需要访问的内容,并保存在varnish的cache中,以便下次的访问。

3.varnish服务器的搭建以及配置

实验环境:rhel6.5

server1: 172.25.70.1

server2: 172.25.70.2

server3: 172.25.70.3

foundation70.ilt.example.com 172.25.254.70

其中server1作为varnish服务器,server2和server3作为后端的http服务器, foundation70.ilt.example.com作为测试主机

1)安装和配置

varnish-3.0.5-1.el6.x86_64.rpm

[root@server1 ~]# yum install varnish-3.0.5-1.el6.x86_64.rpm  -y1

如果出现下面的报错

只需要安装该依赖包即可

[root@server1 ~]# yum install varnish-libs-3.0.5-1.el6.x86_64.rpm  -y12

注意版本的一致

安装完成后,查看/etc/passwd文件可以发现多了一个varnish用户

varnish:x:498:499:Varnish Cache:/var/lib/varnish:/sbin/nologin1

2)varnish服务器的配置

配置一个后端服务器

[root@server1 ~]# vim /etc/varnish/default.vcl 1
backend web1 {  .host = "172.25.70.2";  .port = "80";}1234

添加查看缓存命中情况的语句块

sub vcl_deliver {if (obj.hits > 0) {set resp.http.X-Cache = "HIT from westos cache";}else {set resp.http.X-Cache = "MISS from westos cache";}return (deliver);}123456789

该语句块的作用是如果对象命中,则返回HIT from westos cache,如果对象不命中,则返回MISS from westos cache

配置 varnish 服务端口

[root@server1 ~]# vim /etc/sysconfig/varnish1
VARNISH_LISTEN_PORT=801

开启varnish服务

[root@server1 ~]# /etc/init.d/varnish start12

3)为server2安装apache服务器,并编辑一个默认发布页面

[root@server2 ~]# yum install httpd -y[root@server2 ~]# vim /var/www/html/index.html<h1>backend web1</h1>[root@server2 ~]# /etc/init.d/httpd  start1234

4)测试

a.在测试机的浏览器上访问varnish服务器的ip

可以看到访问varnish服务器的ip访问的是varnish配置文件中指定的后端http主机的默认发布文件

b.测试缓存命中情况

第一次访问不命中

[root@server2 ~]# curl -I 172.25.70.1HTTP/1.1 200 OKServer: Apache/2.2.15 (Red Hat)Last-Modified: Sun, 11 Feb 2018 04:18:24 GMTETag: "40141-16-564e80d6fcbca"Content-Type: text/html; charset=UTF-8Content-Length: 22Accept-Ranges: bytesDate: Sun, 11 Feb 2018 04:19:15 GMTX-Varnish: 359822168Age: 0Via: 1.1 varnishConnection: keep-aliveX-Cache: MISS from westos cache1234567891011121314

第二次访问命中

[root@server2 ~]# curl -I 172.25.70.1HTTP/1.1 200 OKServer: Apache/2.2.15 (Red Hat)Last-Modified: Sun, 11 Feb 2018 04:18:24 GMTETag: "40141-16-564e80d6fcbca"Content-Type: text/html; charset=UTF-8Content-Length: 22Accept-Ranges: bytesDate: Sun, 11 Feb 2018 04:21:12 GMTX-Varnish: 359822169 359822168Age: 117Via: 1.1 varnishConnection: keep-aliveX-Cache: HIT from westos cache123456789101112131415

5)通过 varnishadm 手动清除缓存

 varnishadm ban.url .*$           清除所有 varnishadm ban.url /index.html   清除 index.html 页面缓存 varnishadm ban.url /admin/$      清除 admin 目录缓存123

6)对多个台后端服务器的配置

[root@server1 ~]# vim /etc/varnish/default.vcl1

定义多个不同域名站点的后端服务器

backend web1 {  .host = "172.25.70.2";  .port = "80";}backend web2 {  .host = "172.25.70.3";  .port = "80";}12345678

当访问 域名是从 web1 上取数据,访问 域名时代 web2 取数据,

访问其他页面报错。

sub vcl_recv {if (req.http.host ~ "^(www.)?server2.org") {set req.http.host = ";;set req.backend = web1;}elsif (req.http.host ~ "^;) {set req.backend = web2;}else {error 404 ;}}1234567891011

重启服务

[root@server1 ~]# /etc/init.d/varnish restart1

7)给测试主机添加域名解析

 vim /etc/hosts 172.25.70.1 server1   12

8)为server3安装apache服务器,并编辑一个默认发布页面

[root@server3 ~]# yum install httpd -y[root@server3 ~]# vim /var/www/html/index.html<h1>backend web2</h1>[root@server3 ~]# /etc/init.d/httpd  start1234

9)在测试主机上

[root@foundation70 0120]# curl ;h1>backend web1</h1>You have new mail in /var/spool/mail/kiosk[root@foundation70 0120]# curl ;h1>backend web2</h1>12345

在浏览器上访问不是在上述配置文件中(/etc/varnish/default.vcl)中指定的域名则会报错

出现上述情况表示测试成功

10)健康检查以及负载均衡

#定义健康检查probe healthcheck {.url = "/index.html"; # 哪个 url 需要 varnish 请求.interval = 5s;       #检查的间隔时间.timeout = 1s;        #等待多长时间探针超时.window = 5;          #维持 5 个 sliding window 的结果.threshold = 3;       #至少有三次 window 是成功的,就宣告 bachend 健康}backend web1 {  .host = "172.25.70.2";  .port = "80";  .probe = healthcheck;}backend web2 {  .host = "172.25.70.3";  .port = "80";  .probe = healthcheck;}#将多个后端聚合成一个名为lb的组,轮叫模式director lb round-robin {{.backend = web1;}{.backend = web2;}}sub vcl_recv {if (req.http.host ~ "^(www.)?server2.org") {set req.http.host = ";;set req.backend = lb;return (pass);  #为了测试方便,不进行缓存。} elsif (req.http.host ~ "^;) {set req.backend = web2;} else {error 404 ;}}12345678910111213141516171819202122232425262728293031323334353637

重启

[root@server1 varnish]# /etc/init.d/varnish restart1

在测试机上访问域名

[root@foundation70 0120]# curl ;h1>backend web1</h1>[root@foundation70 0120]# curl ;h1>backend web2</h1>1234

轮叫模式生效,多个后端在lb组的支配下进行轮循

为了使效果更加明显,在server3上进行虚拟主机的配置,使得在轮循的作用下,访问域名的时候,会对server2上的默认发布文件进行访问,而对server3上servername为的域名所对应的默认发布文件进行轮循访问,那么在访问域名为的域名时,就是对server3上的servername为的域名所对应的默认发布文件进行访问

[root@server3 ~]# vim /etc/httpd/conf/httpd.conf 1
NameVirtualHost *:80<VirtualHost *:80>    DocumentRoot /var/www/html/    ServerName ;/VirtualHost><VirtualHost *:80>    DocumentRoot /www/    ServerName ;/VirtualHost>1234567891011

为虚拟主机域名为的默认发布目录添加默认访问文件

[root@server3 ~]# mkdir /www[root@server3 ~]# vim /www/index.html<h1>backend web1-server3</h1>[root@server3 ~]# /etc/init.d/httpd restart  #重启生效1234

测试

[root@foundation70 0120]# curl ;h1>backend web1</h1>You have new mail in /var/spool/mail/kiosk[root@foundation70 0120]# curl ;h1>backend web1-server3</h1>[root@foundation70 0120]# curl ;h1>backend web2</h1>123456789

测试结果正确!!!

以上测试服务器均为飓风科技提供 需要的朋友可以私聊我

标签: #mac1011apache配置