一.定义
Varnish是一款高性能、开源的反向代理服务器和缓存服务器,Varnish使用内存缓存文件来减少响应时间和网络带宽消耗。
Varnish的功能与Squid服务器相似,都可以用来做HTTP缓存。
Squid是从硬盘读取缓存的数据,而Varnish把数据存放在内存中,直接从读取内存,避免了频繁在内存、磁盘中交换文件,所以Varnish要相对更高效,但也有缺点,内存中的缓存在服务器重启后会丢失。
二.部署Varnish
环境:三台机器,其中一台(kvm1作为Varnish部署机器)
KVM1:172.25.18.1
KVM2:172.25.18.2
KVM3:172.25.18.3
1.下载安装包(此包需要自性下载kvm1)
下载安装之前徐保证selinux处于关闭状态
[root@server1 mnt]getenforce
Disabled
安装varnish,安装包自行下载
[root@server1 mnt] yum install varnish* -y
2.查看varnish配置文件
[root@server1 mnt]# rpm -qc varnish-3.0.5-1.el6.x86_64
/etc/logrotate.d/varnish
/etc/sysconfig/varnish
/etc/varnish/default.vcl
[root@server1 mnt]# vim /etc/sysconfig/varnish
由图可以看出资源限制为:
NFILES=131072 打开的最大文件数为131072(uliit -n)
MEMLOCK=82000 锁定共享内存大小 为8G(uliit -l)
NPROCS=”unlimited” 最大线程数,无限制 (uliit -u)
(1)需要在资源限制文件/etc/security/limits.conf进行配置
[root@server1 security]# vim /etc/security/limits.conf
安装varnish后系统会自动生成一个varnish用户
(2)磁盘大小修改
二.缓存(一个后端服务)
(1) 修改配置文件配置代理
[root@server1 security]# cd /etc/varnish/
[root@server1 varnish]# ls
default.vcl secret
[root@server1 varnish]# vim default.vcl
修改完成后记得保存:
[root@server1 varnish]# /etc/init.d/varnish start
Starting Varnish Cache:
KVM1作为varnish加速器(缓存),只要在配置文件李配置了后端ip,访问varnish实质时在访问后端,由于后端已添加至varnish,所以会有缓存,再次访问时直接从缓存获取数据,若第一次访问会提示HIT...,若非第一次访问,(之前有缓存),会显示MISS
给KVM2写默认发页面
(2)客户端进行访问:
第一次访问:
[root@foundation18 ~]# curl -I 172.25.18.1
HTTP/1.1 200 OK
Server: Apache/2.2.15 (Red Hat)
Last-Modified: Sat, 13 Apr 2019 06:20:57 GMT
ETag: "20986-10-586636c1b9ad1"
Content-Type: text/html; charset=UTF-8
Content-Length: 16
Accept-Ranges: bytes
Date: Sat, 13 Apr 2019 06:55:51 GMT
X-Varnish: 1803177163
Age: 0
Via: 1.1 varnish
Connection: keep-alive
X-Cache: MISS from westos cache ##第一次访问
第二次访问:
[root@foundation18 ~]# curl -I 172.25.18.1
HTTP/1.1 200 OK
Server: Apache/2.2.15 (Red Hat)
Last-Modified: Sat, 13 Apr 2019 06:20:57 GMT
ETag: "20986-10-586636c1b9ad1"
Content-Type: text/html; charset=UTF-8
Content-Length: 16
Accept-Ranges: bytes
Date: Sat, 13 Apr 2019 06:55:54 GMT
X-Varnish: 1803177164 1803177163
Age: 3
Via: 1.1 varnish
Connection: keep-alive
X-Cache: HIT from westos cache ##第二次访问
(3)清除所有缓存再次查看
1>清除缓存
[root@server1 varnish]# varnishadm ban.url .*$
再次访问:
[root@foundation18 ~]# curl -I 172.25.18.1
HTTP/1.1 200 OK
Server: Apache/2.2.15 (Red Hat)
Last-Modified: Sat, 13 Apr 2019 06:20:57 GMT
ETag: "20986-10-586636c1b9ad1"
Content-Type: text/html; charset=UTF-8
Content-Length: 16
Accept-Ranges: bytes
Date: Sat, 13 Apr 2019 06:59:01 GMT
X-Varnish: 1803177165
Age: 0
Via: 1.1 varnish
Connection: keep-alive
X-Cache: MISS from westos cache
三.配置两个后端服务
kvm1 为varnish,访问kvm2和kvm3
当访问 www.westos.org 域名时从 web1 上取数据,访问 bbs.westos.org 域名时到 web2 取数据,访问其他页面报错
(1)给KVM3配置默认发访问文件
(2)配置客户端本地文件
(3)配置varnish配置文件
添加两个访问后端,当访问 www.westos.org 域名时从 web1 上取数据,访问 bbs.westos.org 域名时到 web2 取数据
(3) 物理机测试
[root@foundation18 ~]# curl bbs.westos.org
<h1>kvm 3 </h1>
[root@foundation18 ~]# curl www.westos.org
<h1>kvm 2 </h1>
四.轮询--负载均衡
减轻后端访问压力
修改配置文件,给www.westos.org添加轮询
backend web1 {
.host = "172.25.18.2";
.port = "80";
}
backend web2 {
.host = "172.25.18.3";
.port = "80";
}
director lb round-robin {
{.backend = web1;}
{.backend = web2;}
}
sub vcl_recv {
if (req.http.host ~ "^(www.)?westos.org") {
set req.http.host = "www.westos.org";
set req.backend = lb;
return (pass)
} elsif (req.http.host ~ "^bbs.westos.org") {
set req.backend = web2;
} else {
error 404 "westos cache";
}
}
服务端测试:
物理机测试(www.westos.org有轮询)
[root@foundation18 ~]# curl www.westos.org
<h1>kvm 2 </h1>
[root@foundation18 ~]# curl www.westos.org
<h1>kvm 3 </h1>
[root@foundation18 ~]# curl bbs.westos.org
<h1>kvm 3 </h1>
[root@foundation18 ~]# curl bbs.westos.org
<h1>kvm 3 </h1>
[root@foundation18 ~]#
五. 推送平台的搭建
(1)下载安装包,将文件发布到web服务器上
[root@foundation18 mnt]# unzip bansys.zip -d /var/www/html/
(2)下载PHP
[root@foundation18 bansys]# yum install -y php
(3)修改配置文件
[root@foundation18 bansys]# pwd
/var/www/html/bansys
[root@foundation18 bansys]# ls
class_socket.php config.php index.php purge_action.php static
[root@foundation18 bansys]# vim config.php
acl westos {
"127.0.0.1";
"172.25.18.0"/24;
}
sub vcl_recv {
if (req.request == "BAN") {
if (!client.ip ~ westos) {
error 405 "Not allowed.";
}
ban("req.url ~ " + req.url);
error 200 "ban added";
}
}
[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
[root@server1 html]# rm -fr bansys/
[root@server1 html]#
[root@server1 html]# ls
class_socket.php config.php index.php purge_action.php static
(4)浏览器访问
物理机器进行访问
[root@foundation18 ~]# curl -I www.westos.org
HTTP/1.1 200 OK
Server: Apache/2.2.15 (Red Hat)
Last-Modified: Sat, 13 Apr 2019 07:19:26 GMT
ETag: "40c13-10-586643d49ab38"
Content-Type: text/html; charset=UTF-8
Content-Length: 16
Accept-Ranges: bytes
Date: Sat, 13 Apr 2019 09:04:17 GMT
X-Varnish: 2114261201
Age: 0
Via: 1.1 varnish
Connection: keep-alive
X-Cache: MISS from westos cache
[root@foundation18 ~]# curl -I www.westos.org
HTTP/1.1 200 OK
Server: Apache/2.2.15 (Red Hat)
Last-Modified: Sat, 13 Apr 2019 07:19:26 GMT
ETag: "40c13-10-586643d49ab38"
Content-Type: text/html; charset=UTF-8
Content-Length: 16
Accept-Ranges: bytes
Date: Sat, 13 Apr 2019 09:04:18 GMT
X-Varnish: 2114261202 2114261201
Age: 2
Via: 1.1 varnish
Connection: keep-alive
X-Cache: HIT from westos cache
推送清除所有缓存
再次访问
[root@foundation18 ~]# curl -I www.westos.org
HTTP/1.1 200 OK
Server: Apache/2.2.15 (Red Hat)
Last-Modified: Sat, 13 Apr 2019 06:20:57 GMT
ETag: "20986-10-586636c1b9ad1"
Content-Type: text/html; charset=UTF-8
Content-Length: 16
Accept-Ranges: bytes
Date: Sat, 13 Apr 2019 09:06:48 GMT
X-Varnish: 2114261204
Age: 0
Via: 1.1 varnish
Connection: keep-alive
X-Cache: MISS from westos cache