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 缓存系统构建相关推荐
- CDN缓存系统——varnish之清除缓存
目录 一.varnish进程的工作模式 二.缓存 三.手动清理缓存 四.图形化清理缓存 1.安装bansys安装包(帮助清除varnish内存) 2.修改apache使用的端口 3.修改varnish ...
- linux cdn服务器,wdcdn系统,CDN缓存系统,CDN加速系统,多节点CDN自架系统,CDN安装配置部署--Linux解决方案,技术支持与培训,服务器架构,性能优化,负载均衡,集群分流...
WDCDN缓存加速管理系统,CDN软件,CDN管理平台,CDN解决方案 wdCDN是一套基于squid架构开发的CDN缓存加速系统及管理系统,高效,稳定,安全. 可帮助大中小站长或大中小企业低成本,快 ...
- Varnish搭建cdn缓存
CDN的全称是Content Delivery Network,即内容奋发网络.其基本思路是尽可能避开互联网上有可能影响数据传输速度和稳定性的瓶颈和环节,使内容传输的更快.更稳定.通过在网络各处放置节 ...
- 企业CDN缓存系统varnish——搭建一个最基本的varnish系统
前面我先给大家讲述一下基本原理和思想,帮助大家彻底理解什么是varnish加速器 虽然互联网企业大多数使用nginx这个功能比较多的服务器,但是单学一个varnish服务器可以让大家彻底理解原理 1. ...
- 多节点CDN缓存加速系统wdcdn2.4版本发布(20111213)
多节点CDN缓存加速系统wdcdn2.4版本发布(20111213) Wdcdn是一套基于Linux+apache+squid架构开发的CDN缓存加速系统及管理系统 可帮助大中小站长或大中小企业低成本 ...
- varnish缓存服务器构建疑问
标题索引 追朔原因 实验分解 抓包分析 追朔原因[此文需要待续] 当下是互联网时代也是CDN缓存时代,缓存可以提供互联网80%流量,因此缓存的构建和提高缓存的命中率是互联网行业必不可少的方式和手段,另 ...
- CDN缓存加速系统wdcdn2.7版本发布
Wdcdn是一套基于Linux+apache+squid架构开发的CDN缓存加速系统及管理系统 可帮助大中小站长或大中小企业低成本,快速简单,构建自己的CDN网络及服务器群,提供更好的在线服务,更快速 ...
- CDN缓存加速系统wdcdn2.6版本发布(20120213)
Wdcdn是一套基于Linux+apache+squid架构开发的CDN缓存加速系统及管理系统 可帮助大中小站长或大中小企业低成本,快速简单,构建自己的CDN网络及服务器群,提供更好的在线服务,更快速 ...
- CDN缓存加速系统wdcdn3.0版本发布(20120720)
Wdcdn是一套基于Linux+apache+squid架构开发的CDN缓存加速系统及管理系统 可帮助大中小站长或大中小企业低成本,快速简单,构建自己的CDN网络及服务器群,提供更好的在线服务,更快速 ...
最新文章
- FastDFS为什么要结合Nginx?
- reactjs 兄弟通信,父子通信
- 处理大并发之四 libevent demo详细分析(对比epoll)
- Wannafly挑战赛17 - 走格子(模拟)
- ORA-01180: can not create datafile 1 :解决一例
- HDLBits答案(12)_Verilog移位寄存器
- Silver Cow Party POJ - 3268(dijkstra+反向交换)
- 网管型工业交换机如何创建网络冗余
- hdu 1512 Monkey King 左偏树
- 使用Hanlp加载大字典
- C#:Interval Helper
- Java运行时数据区及对象的分配
- nanopi制作个人服务器,NanoPi K2 服务器系统镜像
- Android 布局优化之include与merge
- 电影下载、播放、制作、转换、各类问题全攻略
- 【C++】快速幂与矩阵快速幂
- 离线地图数据tpk的制作
- css改变透明背景png图片的图标颜色
- 求余数(c++基础)
- 1788. 牛为什么过马路
热门文章
- java相关网络协议无响应_java网络协议有哪些
- arc diff 指定版本号_Phabricator客户端安装
- This is probably not a problem with npm. There is likely additional logging output above
- MFCard:易用的信用卡支付集成类库
- 《UG NX8.0中文版完全自学手册》一第1章 UG NX 8.0简介
- oracle11g的安装
- linux 中root用户与普通用户的切换
- 如何优化数据中心虚拟机布局
- 什么是CPI指数和GDP
- 适合小小白的完整建设流程