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;  
     }  
}  
 
----------------------------------------------------------------

Varnish CentOS 6.4 x64的更多相关文章

  1. Centos 6.5 x64环境下 spark 1.6 maven 编译-- 已验证

    Centos 6.5 x64 jdk 1.7 scala 2.10 maven 3.3.3 cd spark-1.6 export MAVEN_OPTS="-Xmx2g -XX:MaxPer ...

  2. Centos 6.5 X64 环境下编译 hadoop 2.6.0 --已验证

    Centos 6.5 x64 hadoop 2.6.0 jdk 1.7 protobuf-2.5.0 maven-3.0.5 set environment export JAVA_HOME=/hom ...

  3. centos 6.5 x64创建并挂载使用iscsi共享磁盘

    前景摘要:NFS或iSCSI,哪个更好?文件 vs 块NFS使用文件级别的实施,服务器或存储阵列托管整个文件系统,客户到文件系统上读写文件,可以在阵列端对主存储数据进行重复数据删除.iSCSI和FC则 ...

  4. CentOS 6.X x64 编译安装 Countly

    CentOS 6.X  x64 编译安装Countly 安装所需的软件 yum -y install supervisor ImageMagick sendmail 1. 安装 node.js wge ...

  5. CentOS 5.8 x64 源码安装 samba-3.6.9

    环境 CentOS 5.8 X64      wget http://www.samba.org/samba/ftp/stable/samba-3.6.9.tar.gz   tar zxvf samb ...

  6. CentOS 6.6 x64安装TensorFlow

    CentOS 6.6 x64安装TensorFlow升级Python到2.7(系统自带Python版本为2.6) // 安装编译工具 $ yum -y install gcc automake aut ...

  7. CentOS 6.5 x64 安装Tomcat8 并配置两个Tomcat8

    1.首先,安装tomcat的前提是已经配置好jdk环境变量,若没配好可以参考我的上一篇博文:CentOS 6.5 x64安装jdk8,当然也可以通过网络搜索安装步骤~~ 2.下载: 可以通过官网下载: ...

  8. CentOS 6.5 x64 安装jdk8

    1.去官网下载Linux版本的jdk8,我下载的是下面这个 2.下载xftp和xshell来操纵服务器,可以搜索一下下载安装即可,安装完成后,打开xshell,新建链接为你的云服务器的IP地址和密码, ...

  9. Apache Maven 3.0.3 (yum) 安裝 (CentOS 6.4 x64)

    介紹http://maven.apache.org/ Maven是一個專案的開發,管理和綜合工具. 下載http://maven.apache.org/download.cgi 參考http://ma ...

随机推荐

  1. Exams

    Exams time limit per test 1 second memory limit per test 256 megabytes input standard input output s ...

  2. python注意事项

    以下基于python3.4.3 1.python3与python2不兼容 2.python语言正确的缩进很重要!事实上缩进是种语法 C中需要 { } 的的地方,python使用 : +缩进 实现 3. ...

  3. SSL交互过程

    SSL交互过程 HTTPS将HTTP和SSL结合,即加了SSL隧道封装的HTTP,通过SSL对客户端身份和服务器进行验证,对传输的数据进行加密.不同情况下SSL的协商过程存在差异,本节以只验证服务器为 ...

  4. c语言判断打开文件是否为空的方法

    void writeReslut2(char* caseName,double averageTime,double max, double min,int loops,int size){ fpos ...

  5. js数组总结

    <!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content ...

  6. 空指针错误 java.lang.NullPointerException

    使用基本的JAVA数据类型,变量的值要么已经是默认值,如果没有对其正常赋值,程序便 不能通过编译,因此使用基本的JAVA数据类型(double,float,boolean,char,int,long) ...

  7. SQL Server 2012 - 内置函数

    文本函数 --系统函数位置: 可编程性→函数→系统函数 -- 查询ASCII码 select ASCII('a') --查询数值对应的ASCII码 select CHAR(97) --Left . R ...

  8. Warning: Cannot modify header information - headers already sent by ... functions_general.php on line 45

    摘自:有用到 http://blog.csdn.net/besily/article/details/5396268 PHP错误:Warning: Cannot modify header infor ...

  9. SEO优化之 主页上加上nofollow

    <a href=http://www.主页.cn/ rel=”nofollow”>这里是锚文字</a> <光年日志分析系统>来分析抓取比较多的是哪个网页,没用的no ...

  10. 剑指offer 数字在排序数组中出现的次数

    因为有序 所以用二分法,分别找到第一个k和最后一个k的下标.时间O(logN) class Solution { public: int GetNumberOfK(vector<int> ...