zoukankan      html  css  js  c++  java
  • Varnish CentOS 6.4 x64

     CentOS 6.4 x64 Varnish 安装配置

     
     
    Varnish的官方网址为http://varnish-cache.org
     
    首先下载Varnish 稳定版本3.0.3
     
    wget http://repo.varnish-cache.org/source/varnish-3.0.3.tar.gz
     
     
    Varnish 2+ 版本以上的需要安装 pcre 支持正则规则
     
    tar zxvf pcre-8.10.tar.gz
    cd pcre-8.10/
    ./configure
    make 
    make install
     
     
    准备完成以后,下面先创建Varnish 运行用户
     
    useradd -s /sbin/nologin varnish
    mkdir -p /opt/local/varnish/cache
    mkdir -p /opt/local/varnish/log
    chown -R varnish:varnish /opt/local/varnish/cache
    chown -R varnish:varnish /opt/local/varnish/log
     
    接下来..安装 Varnish
     
    tar zxvf varnish-3.0.3.tar.gz
    cd varnish-3.0.3
    export PKG_CONFIG_PATH=/usr/local/lib/pkgconfig
    ./configure -prefix=/opt/local/varnish --enable-debugging-symbols --enable-extra-developer-warnings --enable-
     
    dependency-tracking
    make
    make install
     
     
    安装完毕!!
     
     
    配置Varnish
     
    cd /opt/local/varnish/etc/varnish
    mv default.vcl default.vclbak
    vim default.vcl
     
    ---------------------------------------------------------------------------------------
     
    backend default {
         .host = "10.6.0.10";
         .port = "80";
         .connect_timeout = 4s;
         .first_byte_timeout = 5s;
         .between_bytes_timeout = 20s;
     }
     
    acl purge {
            "localhost";
            "127.0.0.1";
     sub vcl_recv {
     
     
            if (req.request == "PURGE") {
                    if (!client.ip ~ purge) {
                            error 405 "Not allowed.";
                    }
                    return(lookup);
            }
     
            if (req.http.host ~ "^(.*)?.qq.com$") {
                    set req.backend = default;
                    if (req.request != "GET" && req.request != "HEAD") {
                            return(pipe);
                    }
                    else {
                            return(lookup);
                    }
            }
            else {
                    error 404 " Cache Server";
                    return(lookup);
            }
     
            if (req.request == "GET" && req.url ~ ".(png|swf|txt|png|gif|jpg|css|js|htm|html)$") {
                    unset req.http.cookie;
            }
     
         if (req.restarts == 0) {
      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") {
             /* Non-RFC2616 or CONNECT which is weird. */
             return (pipe);
         }
         if (req.request != "GET" && req.request != "HEAD") {
             /* We only deal with GET and HEAD by default */
             return (pass);
         }
         if (req.http.Authorization || req.http.Cookie) {
             /* Not cacheable by default */
             return (pass);
         }
         return (lookup);
     }
     
    sub vcl_pipe {
         return (pipe);
     }
     
    sub vcl_pass {
         return (pass);
     }
     
    sub vcl_hash {
         hash_data(req.url);
         if (req.http.host) {
             hash_data(req.http.host);
         } else {
             hash_data(server.ip);
         }
         return (hash);
     }
     
    sub vcl_hit {
         return (deliver);
     }
     
    sub vcl_miss {
         return (fetch);
     }
     
    sub vcl_fetch {
         if (beresp.ttl <= 0s ||
             beresp.http.Set-Cookie ||
             beresp.http.Vary == "*") {
      /*
       * Mark as "Hit-For-Pass" for the next 2 minutes
       */
      set beresp.ttl = 120 s;
      return (hit_for_pass);
         }
         return (deliver);
     }
     
    sub vcl_deliver {
            set resp.http.x-hits = obj.hits ;
            if (obj.hits > 0) {
                    set resp.http.X-Cache = "HIT qq.com";
            }
            else {
                    set resp.http.X-Cache = "MISS qq.com";
            }
    }
     
     sub vcl_error {
         set obj.http.Content-Type = "text/html; charset=utf-8";
         set obj.http.Retry-After = "5";
         synthetic {"
     <?xml version="1.0" encoding="utf-8"?>
     <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
      "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
     <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>
         <p>Varnish cache server</p>
       </body>
     </html>
     "};
         return (deliver);
     }
     
     sub vcl_init {
      return (ok);
     }
     
     sub vcl_fini {
      return (ok);
     }
     
    ---------------------------------------------------------------------------------------
     
     
    配置完成以后....下面启动 Varnish
     
    ./varnishd -n /opt/local/varnish/cache -f /etc/varnish/default.vcl -s file,/opt/local/varnish/cache/cache.data,5G -u varnish -w 2,65535,60 -T 127.0.0.1:2002 -a 0.0.0.0:80 -f /opt/local/varnish/etc/varnish/default.vcl -p thread_pool_min=200 -p thread_pool_max=4000 -p thread_pools=4 -p thread_pool_add_delay=2 -p listen_depth=4096 -p lru_interval=20
     
     
         
     
     
     
    -s malloc,2G  
    -s选项用来确定Varnish使用的存储类型和存储容量,这里使用的是malloc类型(malloc是一个C函数,用于分配内存空间),2G 定义多
     
    少内存被malloced。
     
    -T 127.0.0.1:2000 
    Varnish基于文本方式的一个管理接口,启动后可以在不停止Varnish的情况下来管理Varnish。管理端口2000可以指定。因为不是任何人
     
    都可以访问Varnish管理端口,所以这里推荐只监听本机端口。
     
    -a 0.0.0.0:80 
    -a选项表示Varnish监听所有IP发给80端口的HTTP请求。
     
    -f /opt/local/varnish/etc/varnish/default.vcl
    -f 指定varnish的配置文件
     
    varnishncsa -n /opt/local/varnish/cache
    varnishncsa用来将Varnish访问日志写入日志文件
     
    -F 
    在后台运行
     
    -b address:port 
    命令用于指定后台服务器地址及其端口
     
     
    -d 
    使用debug模式
     
     
    -P file 
    指定PID文件
     
    -p param=value 
    服务器参数,用来优化性能
     
    -s kind[,storageoptions] 
    缓存内容存放方式
     
    -s file
    使用文件做为缓存,其路径、大小等。
     
     
     
     
     
     
    ==================================启动日志=======================================
     
    ./varnishncsa -n /opt/local/varnish/cache/ -w /opt/local/varnish/log/varnish.log &  
     
     
     
    每天日志分割脚本
     
    ---------------------------------------------------------------------------------
    #!/bin/sh
    logs_path=/opt/local/varnish/log
    vlog=${logs_path}/varnish.log
    date=$(date -d "yesterday" +"%Y-%m-%d")
    pkill -9 varnishncsa
    mkdir -p ${logs_path}/$(date -d "yesterday" +"%Y")/$(date -d "yesterday" +"%m")/
    mv /opt/local/varnish/log/varnish.log ${logs_path}/$(date -d "yesterday" +"%Y")/$(date -d "yesterday" +"%m")/varnish-${date}.log
    /opt/local/varnish/bin/varnishncsa -n /opt/local/varnish/cache/ -w /opt/local/varnish/log/varnish.log &
     
    ----------------------------------------------------------------------------------
     
    crontab -e          添加切割时间
     
     
     
    ----------------------------------------------------------------------------------
     
    清除部分缓存
     
    /opt/local/varnish/bin/varnishadm -T 127.0.0.1:2002 purge "req.http.host ~ www.qq.com$ && req.url ~ /static/image/1.jpg"
     
    清除 www.qq.com/static/image/1.jpg  这个url 的缓存
     
     
     
     
    清除所有缓存
     
    /opt/local/varnish/bin/varnishadm -T 127.0.0.1:2002 url.purge *$
     
     
     
    -----------------------------------------------------------------------------------
     
    查看连接数与命中率
     
    /opt/local/varnish/bin/varnishstat -n /opt/local/varnish/cache/
     
     
     
    -n 为 .vsm 文件路径
     
     
    ------------------------------------------------------------------------------------
     
     
     
     
     
     
     
    =======================================================================================
     
    管理工具 Varnishtop 介绍
     
    这个工具用于读取共享内存的日志,适当使用一些过滤选项如-I,-i,-X和-x,可以连续不断地显示大部分普通日志。Varnishtop可以按
     
    照使用要求显示请求的内容、客户端、浏览器等一些其他日志里的信息。
    比如:
    使用varnishtop -i rxurl查看客户端请求的url次数;
     
    使用Varnishtop -i txurl查看请求后端服务器的url次数;
     
    使用Varnishtop -i Rxheader -I Accept-Encoding查看接收到的头信息中有多少次包含
     
     
     
     
    管理工具 Varnishhist 介绍
     
    用于读取Varnishd共享内存段的日志,并生成一个连续的柱状图。Varnishhist用于显示最后N个请求的处理情况。如果缓存命中则标
     
    记"|",如果缓存没有命中则标记"#"符号。
     
     
     
     
    管理工具 Varnishstat 介绍
     
    用于查看Varnish计数丢失率、命中率、存储信息、创建线程、删除对象等。
     
    =======================================================================================
     
     
     
     
     
     
     
     
     
     
     
     
     
     
    PS: 1. HTML页面的http头信息中常带有no-cache头,不缓存的问题。
     
    修改Varnish配置文件,要去掉http头信息中的里no-cache头:
     
    ----------------------------------------------------------------
    sub vcl_fetch {  
            if (req.url ~ "html$") {  
               set beresp.ttl = 10m;  
               set beresp.do_gzip = true;  
               unset beresp.http.Cache-Control;  
               unset beresp.http.Pragma;  
               set beresp.http.Cache-Control = "max-age=60";  
               unset beresp.http.Expires;  
              }  
    }  
    -----------------------------------------------------------------
     
     
    如果html页面带有cookie,还需要在sub vcl_recv { } 配置中添加如下内容:
     
    ----------------------------------------------------------------
     
    sub vcl_recv {  
         if (req.request == "GET" && req.url ~ ".(js|css|html|jpg|png|gif|swf|jpeg| ico)$") {  
            unset req.http.cookie;  
         }  
    }  
     
    ----------------------------------------------------------------
  • 相关阅读:
    聊聊WS-Federation
    用双十一的故事串起碎片的网络协议(上)
    责任链模式的使用-Netty ChannelPipeline和Mina IoFilterChain分析
    最小化局部边际的合并聚类算法(中篇)
    最小化局部边际的合并聚类算法(上篇)
    UVaLive 7371 Triangle (水题,判矩形)
    UVaLive 7372 Excellence (水题,贪心)
    POJ 3312 Mahershalalhashbaz, Nebuchadnezzar, and Billy Bob Benjamin Go to the Regionals (水题,贪心)
    UVa 1252 Twenty Questions (状压DP+记忆化搜索)
    UVa 10817 Headmaster's Headache (状压DP+记忆化搜索)
  • 原文地址:https://www.cnblogs.com/jicki/p/5546914.html
Copyright © 2011-2022 走看看