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
//查看时有变化,则推送成功。
若在浏览器没有变化,可能是因为浏览器有自己的缓存
相关文章:

创建第一个 local network(I) - 每天5分钟玩转 OpenStack(80)
在 ML2 配置文件中 enable local network 后,本节将开始创建第一个 local network。 我们将通过 Web GUI 创建第一个 local network。 首先确保各个节点上的 neutorn agent 状态正常。GUI 菜单 为 Admin -> System -> System Infomation -> Neutron Agents…
【Android】AsyncTask异步类
一、关于AysncTask AsyncTask使得多线程编程更加简单,AsyncTask能在后台线程执行异步任务,并且在UI线程更新界面,而不再需要用户去操作Thread和Handler。AysncTask是一个抽象类,类关系如下: public abstract class As…

高手速成android开源项目【blog篇】
主要介绍那些乐于分享并且有一些很不错的开源项目的个人和组织。Follow大神,深挖大神的项目和following,你会发现很多。 一、个人 JakeWharton 就职于SquareGithub地址:https://github.com/JakeWharton代表作:ActionBarSherlock&a…
3、LVS_TUN实现负载均衡
LVS_TUN实现负载均衡 LVS 介绍: LVS 是 Linux Virtual Server 的简称,在实际环境中经常作为 B/S 结构的网络应用中的负载均衡器来使用,工作在 7 层网络模型中的,网络层,也就是通常说的 IP 层,由于数据的处理是在 Linux 内核态完成的,所以相对反向代理服务器来说,性能一般会高一…

【Android】Fragment官方中文文档
以下内容来自Android官方文档。 Fragment 表示 Activity 中的行为或用户界面部分。您可以将多个片段组合在一个 Activity 中来构建多窗格 UI,以及在多个 Activity 中重复使用某个片段。您可以将片段视为 Activity 的模块化组成部分,它具有自己的生命周期…

关于MSSQL导入导出时主键与约束丢失的问题解决
导入数据时,使用默认选项,会丢失主键、约束、默认值等属性,按如下步骤操作:-->导出向导 -->选择数据源 -->选择目的 -->指定表复制或查询:不要使用默认选项,选择“在SQL Server数据库之间复制对…

Java5中的线程池实例讲解
Java5增加了新的类库并发集java.util.concurrent,该类库为并发程序提供了丰富的API多线程编程在Java 5中更加容易,灵活。本文通过一个网络服务器模型,来实践Java5的多线程编程,该模型中使用了Java5中的线程池,阻塞队列…
LNMP架构的搭建
LNMP 架构的搭建 基础架构图 环境: server5: nginx mysql php //需要的安装包 (蓝色为解压后的文件) [roottest5 ~]# /etc/init.d/iptables stop //关掉防火墙 MYSQL 源码安装 [roottest6 ~]#yum install -y gcc gcc-c make ncurses-devel bison opens…

NSString属性什么时候用copy,什么时候用strong?
我们在声明一个NSString属性时,对于其内存相关特性,通常有两种选择(基于ARC环境):strong与copy。那这两者有什么区别呢?什么时候该用strong,什么时候该用copy呢?让我们先来看个例子。 示例 我们定义一个类…

hihocoder 1152 Lucky Substrings
#1152 : Lucky Substrings 时间限制:10000ms单点时限:1000ms内存限制:256MB描述 A string s is LUCKY if and only if the number of different characters in s is a fibonacci number. Given a string consisting of only lower case letters, output all its lucky non-empt…

随笔,记2014忆往昔岁月
博客园开通了一年多,这是第一篇博客。在此记下我的第一篇博客,同时,回忆过去几年自己的工作所得所想所感。 大学毕业,工作两年半了,做过很多事,比较杂,做过需求,做过设计,…

PHP相关关系及定义
CGI(是一种协议): 是为了保证web server传递过来的数据是标准格式的,方便CGI程序的编写者。 web server(如nginx)是内容的分发者。 处理静态页面: 如果请求/index.html,web server就可以解…

Apache优化:修改最大并发连接数
http://www.365mini.com/page/apache-concurrency-configuration.htm Apache是一个跨平台的web服务器,由于其简单高效、稳定安全的特性,被广泛应用于计算机技术的各个领域。现在,Apache凭借其庞大的用户数,已成为用户数排名第一的…

黑马程序员___Java基础[02-Java基础语法](一)
Java语言基础组成 一、关键字 1)定义:被Java语言赋予了特殊含义的单词 2)特点:关键字中所有字母均为小写 3)作用及分类: 下面是Java语言保留专用的50个关键字: 用于定义数据类型的关键字(12个):…
NSLog打印自定义对象
我们在开发中,如果直接使用NSLog打印对象,则会打印对象的指针(如下图) 但我们常常希望打印的是对象的属性的值,因此我们需要重写自定义类的description方法(打印日志时,对象会收到description消…
数据库的安装与管理
数据库的安装与管理 1.mysql数据库的安装 yum install mariadb-server -y systemctl start mariadb ##开启数据库 netstat -antlupe | grep mysql ##查看端口 vim /etc/my.cnf ##修改配置文件。添加skip-networking1 systemctl restart mariadb ##重起服务 netstat -antlupe |…

SQL性能优化没有那么神秘
经常听说SQL Server最难的部分是性能优化,不禁让人感到优化这个工作很神秘,这种事情只有高手才能做。很早的时候我在网上看到一位高手写的博客,介绍了SQL优化的问题,从这些内容来看,优化并不都是一些很复杂的问题&…

腾讯云无法绑定公网IP问题解释与解决方案。
腾讯云无法绑定公网IP问题解释与解决方案。 http://blog.csdn.net/chenggong2dm/article/details/51475222 解释:公网IP并不直接配置在服务器上,而是在服务器外部的路由上,通过某种映射连接。 解决方案:绑定0.0.0.0 posted on 201…
iOS Category小举例
(一)Category作用:Category可以向已存在的类添加新的方法,或者覆盖原来类中已经存在的方法,从而扩展已有类(在Java中为了实现类似功能,一般是创建子类) (二)C…
memcache
nginxphpmemcache缓存 图解: [roottest5 ~]# /etc/init.d/iptables stop [roottest5 ~]# nginx [roottest5 ~]# /etc/init.d/php-fpm start Starting php-fpm done [roottest5 ~]# tar zxf memcache-2.2.5.tgz [roottest5 ~]# cd memcache-2.2.5 [roottest5…

iOS中KVO模式的解析与应用
最近老翁在项目中多处用到了KVO,深感这种模式的好处。现总结如下: 一、概述 KVO,即:Key-Value Observing,它提供一种机制,当指定的对象的属性被修改后,则对象就会接受到通知。简单的说就是每次指定的被观察…

C 到C++的升级
C所有的变量都可以在需要使用时再定义。 C语言中的变量都必须在作用域开始的位置定义。 register 关键字请求编译器将局部变量存储于寄存器中 在C语言无法获取register 变量的地址 在C中可以取得 register 变量的地址 C编译器有自己的优化方式,所以几乎不用registe…

SET QUOTED_IDENTIFIER OFF语句的作用
先看下面几个sql语句 1SETQUOTED_IDENTIFIER ON2SELECT*FROM"USER" WHEREanetasp34SETQUOTED_IDENTIFIER ON5SELECT*FROM[USER]WHEREanetasp67SETQUOTED_IDENTIFIER OFF8SELECT*FROM[USER]WHEREa"netasp" 910SETQUOTED_IDENTIFIER OFF11SELECT*FROM[USE…
proxy实现 mysql 读写分离
实现 mysql 读写分离 图解: 环境: iptables 和 selinux 关闭 proxy:test2 172.25.1.2 Master: test3 172.25.1.3 Slave:test4 172.25.1.4 环境已经实现 test3(master) 和 test4(slave) 的主从复制 Server2: [roottest2 ~]# ls mysql-proxy-0.8.5-linux-el6-x86-64bit.tar.gz …

iOS开发中用到的一些第三方库
下面是我在开发中用到的一些优秀的iOS第三方开源库: 1.AFNetworking(网络请求,类似的还有ASIHTTPRequest) https://github.com/AFNetworking/AFNetworking/ 2.MBProgressHUD(提示框) https://github.…

小图标外链API
网页上有些分享的小图标,比如分享到facebook,weibo,qq空间等功能的时候,图标以前一般是自己做一个css sprite。当一个网站的图标变了的时候,比如facebook变成assbook的时候,你就要修改这个css sprite。费时…

转载JQuery 获取设置值,添加元素详解
转载原地址 http://www.cnblogs.com/0201zcr/p/4782476.html jQuery 获取内容和属性 jQuery DOM 操作 jQuery 中非常重要的部分,就是操作 DOM 的能力。 jQuery 提供一系列与 DOM 相关的方法,这使访问和操作元素和属性变得很容易。 提示:DOM …
mysql 主从复制 和基于gtid的mysql主从复制
主从复制 原理: mysql 无需借助第三方工具,而是其自带的同步复制功能,另外一点,mysql 的主从 复制并不是从硬盘给上文件直接同步,而是逻辑的 binlog 日志同步到本地的应用执行的过 程。 数据从一个 mysql 数据库(master)复制到另一个 mysql 数据库(slave),在 master 与 slave …

使用read write 读写socket
一旦,我们建立好了tcp连接之后,我们就可以把得到的fd当作文件描述符来使用。 由此网络程序里最基本的函数就是read和write函数了。 写函数: ssize_t write(int fd, const void*buf,size_t nbytes); write函数将buf中的nbytes字节内容写入文件…
iOS从通讯录中选择联系人
有时候APP需要用户输入一位联系人的姓名和电话,除了用户手动输入,一般也允许用户从通讯录中选择一位联系人(图1),下面的代码就是使用系统的<AddressBookUI/AddressBookUI.h>库实现这一需求。 图1 完整代码&…