linux的varnish服务

varnish是一款专业web缓存服务器,对I/O要求特别高,和nginx不同的是,数据缓存在内存中,因此性能非常好,而nginx是存储在levels=1:2:2....这样的层级结构的文件中,一旦层级量大会非常占用inode,而且nginx的侧重点是反代,不是缓存。

可以作缓存的内容:数据库,流媒体,页面文件,图片,css/html....

varnish的功能:

*1*一套缓存系统,在CDN中扮演web缓存的角色,性能和响应时间更优越,语法简单,可以实现很多定制化,而HAProxy无缓存

*2*反代是基本的功能,自然具备

*3*可以实现动静分离

*4*可以实现负载均衡

linux的varnish服务

varnish搭建:

主机环境:rhel6.5镜像,selinux和iptables都必须是disabled状态

实验虚拟机:

172.25.254.1     varnish(server1)

172.25.254.2    apache(server2)

172.25.254.3   apache(server3)

实验阶段:

 #####1#####

1.配置后端服务器

在server1中下载varnish的两个rpm包

linux的varnish服务

linux的varnish服务

 

2.改变varnish监听端口(80),进行varnish的系统环境设置,即varnish用户的限制:

linux的varnish服务

linux的varnish服务

3.在server2中安装apache软件,开启服务,编写默认发布文件index.html:

linux的varnish服务

linux的varnish服务

linux的varnish服务

linux的varnish服务

4.修改varnish配置文件:

linux的varnish服务

linux的varnish服务

linux的varnish服务

5.需要在物理机的浏览器中进行查询,设置本地解析,在浏览器查询:

*1.在firefox中

linux的varnish服务

*在操作环境中

linux的varnish服务

####2####

(测试缓存命中情况)

1.在server1中更改配置文件

linux的varnish服务

linux的varnish服务

2.在物理机中测试(第一次显示MISS,因为并没有缓存过,第二次显示HIT,说明已经缓存过)

linux的varnish服务

linux的varnish服务

3.手动清除缓存

varnishadm ban.url .*$                         #清除所有缓存

varnishadm ban.url/inddex.html          #清除index.html页面缓存

varnishadm ban.url/admin/$               #清除admin目录缓存

linux的varnish服务

linux的varnish服务

*注意:若是用varnishadm ban.url/index.html清理后,curl -I 172.25.254.1出现的结果会是HIT,而curl -I 172.25.254.1/index.html的结果会是MISS,这是因为它们缓存到cache的地址不同

linux的varnish服务

linux的varnish服务

linux的varnish服务

 

#####5#####

(定义多个域名站点的后端服务器)

1.在server3中安装apache服务,并开启服务:

linux的varnish服务

linux的varnish服务

2.编写server3的index.html文件并重新加载服务:

linux的varnish服务

linux的varnish服务

3.更改server2的index.html文件并重新加载服务:

linux的varnish服务

linux的varnish服务

4.添加物理机域名解析:

linux的varnish服务

5.更改server1varnish配置文件,并重新加载服务:

linux的varnish服务

linux的varnish服务

6.物理机中测试,(curl www.linux.org得到的是server3的文件内容):

linux的varnish服务

#######6######

(定义负载均衡)

1.更改server3的apache配置文件,并新建文件/www1/index.html,重新加载apache服务:

linux的varnish服务

linux的varnish服务

linux的varnish服务

linux的varnish服务

linux的varnish服务

2.配置本地解析:

linux的varnish服务

3.测试本地解析是否生效:

linux的varnish服务

4.更改server1的varnish配置文件(把多个后端聚合为一个组,并检测后端健康状况)并重新加载服务:

linux的varnish服务

linux的varnish服务

5.物理机测试:

*第一遍测试得到server2的文件内容

linux的varnish服务

*清除缓存

linux的varnish服务

*第二遍测试得到server3的文件内容

linux的varnish服务

****还可以更改server1中varnish配置文件,添加return(pass),使得varnish缓存功能失效,客户端每次访问都直接从后端获得

*1.更改server1中varnish配置文件并重新加载:

linux的varnish服务

linux的varnish服务

*2在物理机中测试:

(发现循环得到server2和server3的文件内容)

linux的varnish服务

*3.关闭server2的apache服务:

linux的varnish服务

*4.重新测试:

(只会获得server3的文件内容,并不会报错,这是因为varnish有了健康检查功能)

linux的varnish服务

**开启server2的apache后,又会出现轮循接受

############7##########

(CDN推送平台)

1.在server1中安装apache服务并更改端口,避免冲突:

linux的varnish服务

linux的varnish服务

linux的varnish服务

2.下载bansys.zip包,安装并解压:

linux的varnish服务

linux的varnish服务

linux的varnish服务

3.将解压出来的目录中的文件移到/var/www/html中:

linux的varnish服务

4.安装php服务:

linux的varnish服务

5.开启apache服务:

linux的varnish服务

6.更改php服务配置文件(删除并改写):

linux的varnish服务

linux的varnish服务

7.更改varnish配文件,并重新加载varnish服务:

linux的varnish服务

linux的varnish服务

linux的varnish服务

8.在物理机中测试:

用firefox访问www.westos.org,打开另一个网页,访问172.25.254.1:8080

linux的varnish服务

linux的varnish服务

*第一次在firefox中www.westos.org,显示的是server2中的内容

*可以推送/index.html或者/images/*,本次实验推送/index.html

linux的varnish服务

linux的varnish服务

*第二次在firefox中www.westos.org,显示的是server3中的内容

linux的varnish服务