手机版
你好,游客 登录 注册
背景:
阅读新闻

CentOS 5.8下Varnish-2.1.5的安装配置

[日期:2013-09-10] 来源:Linux社区  作者:andrewyu [字体: ]

Varnish是一款强大的反向代理加速软件,关于其工作原理可以参考上图,其具体流程及VCL语法我这里就不做说明,网上资料多,大家还可以对照参考其官方网站和《Varnish中文权威指南》。

Varnish中文权威指南 PDF 下载

免费下载地址在 http://linux.linuxidc.com/

用户名与密码都是www.linuxidc.com

具体下载目录在 /2013年资料/9月/10日/CentOS 5.8下Varnish-2.1.5的安装配置

下载方法见 http://www.linuxidc.com/Linux/2013-07/87684.htm

相关阅读:

利用Varnish构建Cache服务器笔记 http://www.linuxidc.com/Linux/2012-07/65234.htm

Varnish 编译安装所需准备 http://www.linuxidc.com/Linux/2012-07/65230.htm

缓存服务Varnish安装配置 http://www.linuxidc.com/Linux/2012-07/65228.htm

利用Varnish和Nginx来使用WebSocket  http://www.linuxidc.com/Linux/2012-05/61214.htm

Linux下Varnish缓存的配置优化 http://www.linuxidc.com/Linux/2012-03/56435.htm

一、安装CentOS5.8系统环境下的依耐关系

12 yum install gcc gcc-c++
yum install automake autoconflibtool ncurses-devel libxslt groff pcre-devel pkgconfig libtool -y

二、下载varnish-2.1.5源码包,并进行编译安装。

cd /usr/local/src
wget  http://repo.varnish-cache.org/source/varnish-2.1.5.tar.gz
tar zxvf varnish-2.1.5.tar.gz
cd varnish-2.1.5.
./autogen.sh

#autogen.sh命令是用来检查软件的依耐关系是否满足,如果报错的话, 则应该如下
正常所示:

+ aclocal
+ libtoolize --copy --force
+ autoheader
+ automake --add-missing --copy --foreign
+ autoconf

继续编译安装:

12 ./configure --prefix=/usr/local/varnish --enable-dependency-tracking --enable-debugging-symbols --enable-developer-warnings -enable-extra-warnings
make && make install && cd ../

三、创建varnish用户和组,以及varnish缓存文件和日志存放目录:

/usr/sbin/groupadd varnish
/usr/sbin/useradd -s /sbin/nologin  -g varnish varnish
mkdir -p /data/varnish/{cache,log}
chown  -R varnish:varnish /data/varnish/{cache,log}

四、我的测试环境是两台Web机器,IP为192.168.1.103(域名为http://www.linuxidc.net)的varnish机器对后端IP为192.168.1.104和192.168.1.105的机器进行反向代理加速,其配置文件/usr/local/varnish/etc/varnish/better.vcl如下所示:

backend rserver1
{
.host ="192.168.1.104";
.port = "80";
.probe = {
.timeout = 5s;          #等待多长时间超时
.interval = 2s;          #检查时间间隔
.window = 10;        #varnish将维持10个sliding windows的结果
.threshold = 8;        #如果是8次.windows检查是成功的,就宣告后端的Web机器
是健康的
}
}
backend rserver2
{
.host ="192.168.1.105";
.port = "80";
.probe = {
.timeout = 5s;     
.interval = 2s;   
.window = 10;     
.threshold = 8;
}
}
#指定一个名为realserver组,使用random机制,权重越大,分配的访问越多,可根据
服务器性能来设定;而round-robin(轮询)机制是不能指定weight的
director realserver random {
{
.backend = rserver1;
.weight = 5;
}
{
.backend = rserver2;
.weight = 6;
}
}
#定义能清理缓存的机器,这里只允许本机能用purge的方式清理
acl purge { 
"localhost"; 
"127.0.0.1"; 
}
sub vcl_recv
{
  if (req.http.host ~"^(.*).linuxidc.net")
  {     
    set req.backend =realserver; 
  }   
    else
    {     
      error 200 "Nocahce for this domain"; 
    }           
      if (req.request =="PURGE")
        {         
          if (!client.ip ~purge)
            {           
                error 405"Not allowed.";         
            } 
          else
            {
                return (pipe); 
            }
}
#获取客户端真实IP地址
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;       
}
#对HTTP协议中的GET、HEAD请求进行缓存,对POST请求透过,让其直接访问后端Web服
务器。之所以这样配置,是因为POST请求一般是发送数据给服务器的,需要服务器接
收、处理,所以不缓存;
if (req.request !="GET" && req.request != "HEAD")
{         
return (pipe); 

if (req.http.Expect)
{       
return (pipe);
}
if (req.http.Authenticate|| req.http.Cookie)
{         
return (pass); 

if (req.http.Cache-Control~ "no-cache")
{       
return (pass); 
}
#对JSP或者PHP文件不缓存
if(req.url ~"\.jsp" || req.url ~ "\.php" )
{         
return (pass); 

else

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 (req.request =="PURGE")

set obj.ttl = 0s;       
error 200"Purged.";
}
if (!obj.cacheable)
{   
return (pass);
}
return (deliver);
}sub vcl_miss

if (req.request =="PURGE")
{   
error 404 "Not incache."; 
}
if (req.http.user-agent ~"spider")
{   
error 503 "Notpresently in cache"; 
}
    return (fetch);
}
sub vcl_fetch
{
if (req.request =="GET" && req.url ~ "\.(txt|js)$")
{   
set beresp.ttl = 3600s; 

else
{   
set beresp.ttl = 30d;
}
if (!beresp.cacheable)
{   
return (pass);

if (beresp.http.Set-Cookie)

return (pass);

return (deliver);
}
sub vcl_deliver {
 if (obj.hits > 0) {
  set resp.http.X-Cache= "HIT  FROM www.linuxidc.net";
 } else {
  set resp.http.X-Cache= "MISS FROM www.linuxidc.net";
 }
return (deliver);
}

linux
相关资讯       Varnish  CentOS 5.8  Varnish安装 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款