四、配置Varnish
4.1 修改Management进程配置文件
Management进程的配置文件为/etc/varnish/varnish.params。我们先来修改一下这个文件,主要是修改端口和缓存类型及大小:
VARNISH_ADMIN_LISTEN_PORT=6082
VARNISH_STORAGE="file,/data/cache,1G"

4.2 修改Varnish总配置文件
Varnish的总配置文件为/etc/varnish/default.vcl,我们来修改一下这个配置文件:
1)第一段
① 定义一个健康监测:
vcl 4.0; #指定版本
import directors; #加载后端的轮询模块
probe backend_healthcheck { #设置名为backend_healthcheck的健康监测
.url = "/index.html";
.window = 5; #窗口
.threshold = 2; #门槛
.interval = 3s;
.timeout = 1s;
}
② 设置后端server:
backend web1 {
.host = "192.168.37.222";
.port = "80";
.probe = backend_healthcheck;
}
backend web2 {
.host = "192.168.37.100";
.port = "80";
.probe = backend_healthcheck;
}
③ 配置后端集群事件
sub vcl_init {
new img_cluster = directors.round_robin();
img_cluster.add_backend(web1);
img_cluster.add_backend(web2);
}
acl purgers { # 定义可访问来源IP,权限控制
"127.0.0.1";
"172.17.0.0"/16;
}
2)第二段——定义引擎
① 定义vcl_recv引擎
定义不认识的头部请求直接扔后端的pass
sub vcl_recv {
if (req.method == "GET" && req.http.cookie) {
return(hash); //处理完recv 引擎,给下一个hash引擎处理
}
if (req.method != "GET" &&
req.method != "HEAD" &&
req.method != "PUT" &&
req.method != "POST" &&
req.method != "TRACE" &&
req.method != "OPTIONS" &&
req.method != "PURGE" &&
req.method != "DELETE") {
return (pipe); //除了上边的请求头部,通过通道直接扔后端的pass
}
定义index.php通过特殊通道给后端的server,不经过缓存
if (req.url ~ "index.php") {
return(pass);
}
定义删除缓存的方法
if (req.method == "PURGE") { //PURGE请求的处理的头部,清缓存
if (client.ip ~ purgers) {
return(purge);
}
}
为发往后端主机的请求添加X-Forward-For首部
if (req.http.X-Forward-For) { //为发往后端主机的请求添加X-Forward-For首部
set req.http.X-Forward-For = req.http.X-Forward-For + "," + client.ip;
} else {
set req.http.X-Forward-For = client.ip;
}
return(hash);
}
② 定义vcl_hash 引擎,后没有定义hit和Miss的路径,所以走默认路径
sub vcl_hash {
hash_data(req.url);
}
③ 定义要缓存的文件时长
sub vcl_backend_response { //自定义缓存文件的缓存时长,即TTL值
if (bereq.url ~ "\.(jpg|jpeg|gif|png)$") {
set beresp.ttl = 30d;
}
if (bereq.url ~ "\.(html|css|js)$") {
set beresp.ttl = 7d;
}
if (beresp.http.Set-Cookie) { //定义带Set-Cookie首部的后端响应不缓存,直接返回给客户端
set beresp.grace = 30m;
return(deliver);
}
}
④ 定义deliver 引擎
sub vcl_deliver {
if (obj.hits > 0) { //为响应添加X-Cache首部,显示缓存是否命中
set resp.http.X-Cache = "HIT from " + server.ip;
} else {
set resp.http.X-Cache = "MISS";
}
unset resp.http.X-Powered-By; //取消显示php框架版本的header头
unset resp.http.Via; //取消显示varnish的header头
}
本步骤完成。
五、开启服务的顺序
1)先开启后端server事先搭建好的lnmp web服务
systemctl start nginx
systemctl start php-fpm
systemctl start mariadb
2)再开启varnish缓存服务器
service varnish start
3)开启主从的keepalived,提供VIP
service keepalived start
4)开启haproxy服务
service haproxy start
本步骤完成。
缓存服务器Varnish概念篇 http://www.linuxidc.com/Linux/2014-05/101389.htm
缓存服务器Varnish概念篇 http://www.linuxidc.com/Linux/2014-05/101389.htm
Varnish Cache 的架构笔记 http://www.linuxidc.com/Linux/2013-10/91016.htm
CentOS 5.8下Varnish-2.1.5的安装配置 http://www.linuxidc.com/Linux/2013-09/89916.htm
RedHat脚本改用CentOS源更新安装Nginx、PHP 5.3、Varnish http://www.linuxidc.com/Linux/2012-07/65801.htm
利用Varnish构建Cache服务器笔记 http://www.linuxidc.com/Linux/2012-07/65234.htm
缓存服务Varnish安装配置 http://www.linuxidc.com/Linux/2012-07/65228.htm
Varnish 编译安装所需准备 http://www.linuxidc.com/Linux/2012-07/65230.htm
Linux下Varnish缓存的配置优化 http://www.linuxidc.com/Linux/2012-03/56435.htm
Varnish 的详细介绍:请点这里
Varnish 的下载地址:请点这里
