Varnish-2.1.2 安装与配置

目录

下载:Varnish-2.1.2 安装与配置.pdf

Varnish 2.1.2 安装与配置
Varnish 下载
Varnish 安装

Varnish 配置实例
Varnish 启动与停止,动态加载配置文件
Varnish 日志启动与日志切割
Varnish 缓存清除
Varnish 配置介绍 
定义后端服务器IP和端口
定义访问控制列表,充许那些IP清除varnish 缓存
判断host请求针对那个后端服务器
不充许非访问控制列表的IP进行varnish缓存清除
清除url中有jpg|png|gif等文件的cookie
取消服务器上images目录下所有文件的cookie
获取客户端IP地址
针对请求和url地址判断是否缓存
Web服务器指明不缓存的内容,varnish服务器不缓存
指定fonts目录不缓存
指定要缓存的静态文类型
使用正则表达式指定缓存的内容
添加配置,查看缓存命中情况
根据访问url地址或者目录,转发到不同的后端服务器
定义组,负载均衡+后端web服务器健康检查
防止爬虫,网络蜘蛛访问
防盗连接 
禁止某个目录或者某个url地址访问
Rewrite urls配置 
varnish 优化
Varnish 参考资料 

 

varnish 缓存清除代码(php)

 https://www.chinasa.net/usr/uploads/2010/10/3996505239.rar


varnish 缓存清除界面

clearcache.jpg

 

 

 

环境:Centos 5.4 


Varnish 下载

下载地址:http://sourceforge.net/projects/varnish/files/

Varnish 安装
安装依赖包

# yum install libedit-devel

#  tar zxvf varnish-2.1.2.tar.gz

#  cd varnish-2.1.2

#  ./configure –prefix=/opt/varnish

#  make

#  make install

Varnish 配置实例

通过前端varnish缓存服务器反向代理后端www.bbs.comwww.bbs1.com网站,要求对静态文件js|css|jpg|gif等文件进行缓存7,对网页中加有no-cache头信息页面不缓存。配置文件如下:

#  vi /opt/varnish/etc/varnish/bbs.vcl

backend bbs {
  .host = "192.168.0.144";
  .port = "80";
}
backend bbs1 {
  .host = "192.168.0.155";
  .port = "80";
}
  
acl local {
       "localhost";
       "127.0.0.1";
}
  
sub vcl_recv {
     if (req.http.host ~ "^(www.)?bbs.com$") {
        set req.backend = bbs;
     }
     elsif (req.http.host ~ "^(www.)?bbs1.com$") {
        set req.backend = bbs1;
     }       
      else {
          error 404 "Unknown HostName!";
     }
     if (req.request == "PURGE") {
        if (!client.ip ~ local) {
            error 405 "Not Allowed.";
            return (lookup);
            }
     }
     if (req.request == "GET" && req.url ~ "\.(jpg|png|gif|swf|jpeg|ico)$") {
        unset req.http.cookie;
     }
     if (req.http.x-forwarded-for) {
      set req.http.X-Forwarded-For =
      req.http.X-Forwarded-For ", " client.ip;
     else {
        set req.http.X-Forwarded-For = client.ip;
     }
     if (req.request != "GET" &&
      req.request != "HEAD" &&
      req.request != "PUT" &&
      req.request != "POST" &&
      req.request != "TRACE" &&
      req.request != "OPTIONS" &&
      req.request != "DELETE") {
        return (pipe);
     }
     if (req.request != "GET" && req.request != "HEAD") {
        return (pass);
     }
     if (req.http.Authorization || req.http.Cookie) {
        return (pass);
     }
     if (req.request == "GET" && req.url ~ "\.(php)($|\?)") {
        return (pass);
           }
     return (lookup);
}
  
sub vcl_pipe {
    return (pipe);
}
  
sub vcl_pass {
    return (pass);
}
  
sub vcl_hash {
    set req.hash += req.url;
    if (req.http.host) {
        set req.hash += req.http.host;
    else {
        set req.hash += server.ip;
    }
    return (hash);
}
  
sub vcl_hit {
    if (!obj.cacheable) {
        return (pass);
    }
    return (deliver);
}
  
sub vcl_miss {
    return (fetch);
}
  
sub vcl_fetch {
    if (!beresp.cacheable) {
        return (pass);
    }
    if (beresp.http.Set-Cookie) {
        return (pass);
    }
    if (beresp.http.Pragma ~ "no-cache" ||
 beresp.http.Cache-Control ~ "no-cache" ||
 beresp.http.Cache-Control ~ "private") {
           return (pass);
 }
  
    if (req.request == "GET" && req.url ~ "\.(js|css|mp3|jpg|png|gif|swf|jpeg|ico)$") {
        set beresp.ttl = 7d;
    }
  
    return (deliver);
}
  
sub vcl_deliver {
            set resp.http.x-hits = obj.hits ;
            if (obj.hits > 0) {
                        set resp.http.X-Cache = "HIT cqtel-bbs";
            else {
                set resp.http.X-Cache = "MISS cqtel-bbs";
            }
}
  
sub vcl_error {
    set obj.http.Content-Type = "text/html; charset=utf-8";
    synthetic {"
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
<html>
  <head>
    <title>"} obj.status " " obj.response {"</title>
  </head>
  <body>
    <h1>Error "} obj.status " " obj.response {"</h1>
    <p>"} obj.response {"</p>
    <h3>Guru Meditation:</h3>
    <p>XID: "} req.xid {"</p>
    <hr>
    <address>
       <a href="http://www.bbs.com/">bbs cache server</a>
    </address>
  </body>
</html>
"};
    return (deliver);

}

 

 

Varnish 启动与停止,动态加载配置文件

......