1. 下载解压
cd /usr/local/src/
wget https://codeload.github.com/varnishcache/varnish-cache/zip/master
chmod 775 varnish-cache-master.zip

unzip varnish-cache-master.zip

varnish-cache-master.zip

2. 安装
cd varnish-cache-master
chmod -R 755 *
yum install autoconf automake jemalloc-devel libedit-devel libtool ncurses-devel pcre-devel pkgconfig python-docutils python-sphinx
./autogen.sh
./configure --prefix=/usr/local/varnish PKG_CONFIG_PATH=/usr/lib/pkgconfig
make
make install

3. 配置
cd /usr/local/varnish/
mkdir /var/varnish_cache
mkdir etc
vi etc/web.conf ##详见 web.conf 文件内容
vi /etc/init.d/varnish ##详见 varnish 文件内容
chmod -R 755 /etc/init.d/varnish

4. 启动项
service iptables stop
chkconfig iptables --level 2345 off
chkconfig --add varnish
chkconfig varnish --level 016 on

5. 修改 httpd 的配置
vi /alidata/server/httpd/conf/httpd.conf
Listen8080  ##必须与varnish监听的端口一致

vi /alidata/server/httpd/conf/vhosts/leizhiman.conf 
<VirtualHost *:8080>  ##必须与httpd.conf的端口一致
        DocumentRoot /alidata/www/leizhiman
        ServerName leizhiman.cn
        ServerAlias www.leizhiman.cn
        <Directory "/alidata/www/leizhiman">
            Options FollowSymLinks
            AllowOverride all
            Order allow,deny
            Allow from all
        </Directory>
        ErrorLog "/alidata/log/httpd/leizhiman-error.log"
        CustomLog "/alidata/log/httpd/leizhiman.log" common

</VirtualHost>

service httpd restart

service varnish start

6.测试: 访问 http://www.leizhiman.cn ( 注意: 第一次访问是 varnish-nocache )

实验成功!!

----------------------------------------------------以下是配置文件---------------------------------------------------------

web.conf 文件内容

 # This is a Varnish .x VCL file
 vcl 4.0;

 backend default {
     .host = "127.0.0.1";
     .port = ";  ##必须跟 httpd.conf 的端口一致
     .probe = {
         .url = "/ping";
         .timeout   = 1s;
         .interval  = 10s;
         .window    = ;
         .threshold = ;
     }
     .first_byte_timeout     = 300s;   # How long to wait before we receive a first byte from our backend?
     .connect_timeout        = 5s;     # How long to wait for a backend connection?
     .between_bytes_timeout  = 2s;     # How long to wait between bytes received from our backend?
 }

 backend web1 {
     .host = "127.0.0.1";
     .port = "; ##必须跟 httpd.conf 的端口一致
 }
 ##如果有多个, 就继续加: 比如 web2

 # Below is an example redirector based on round-robin requests
 import directors;
 sub vcl_init {
     new cluster1 = directors.round_robin();
     cluster1.add_backend(web1);    ##必须对应 backend web1 中的 "web1"

     #cluster1.add_backend(web2);
 }

 acl purge {
     # For now, I'll only allow purges coming from localhost
     "127.0.0.1";
     "localhost";
 }

 # Handle the HTTP request received by the client
 sub vcl_recv {
     # Choose the round-robin backend
     #set req.backend_hint = cluster1.backend();

     # Or chose the client-IP backend (sticky sessions)
     #set req.backend_hint = cluster2.backend();

     # shortcut for DFind requests
     if (req.url ~ "^/w00tw00t") {
         return (synth(, "Not Found"));
     }

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

     # Normalize the header, remove the port (in case you're testing this on various TCP ports)
     #set req.http.Host = regsub(req.http.Host, ":[0-9]+", "");
     if (req.http.host ~ "(?i)^(www.)?leizhiman.cn$") {  ##根据域名转发到指定后端服务器
         set req.backend_hint = cluster1.backend();
     }

     # Allow purging
     if (req.method == "PURGE") {
         if (!client.ip ~ purge) {
             # Not from an allowed IP? Then die with an error.
             return (synth(, "This IP is not allowed to send PURGE requests."));
         }

         # If you got this stage (and didn't error out above), purge the cached result
         return (purge);
     }

     # Only deal with "normal" types
     if (req.method != "GET" &&
             req.method != "HEAD" &&
             req.method != "PUT" &&
             req.method != "POST" &&
             req.method != "TRACE" &&
             req.method != "OPTIONS" &&
             req.method != "PATCH" &&
             req.method != "DELETE") {
         /* Non-RFC2616 or CONNECT which is weird. */
         return (pipe);
     }

     # Only cache GET or HEAD requests. This makes sure the POST requests are always passed.
     if (req.method != "GET" && req.method != "HEAD") {
         return (pass);
     }

     # Configure grace period, in case the backend goes down. This allows otherwise "outdated"
     # cache entries to still be served to the user, because the backend is unavailable to refresh them.
     # This may not be desireable for you, but showing a Varnish Guru Meditation error probably isn't either.
     #set req.grace = 15s;
     #if (std.healthy(req.backend)) {
     #    set req.grace = 30s;
     #} else {
     #    unset req.http.Cookie;
     #    set req.grace = 6h;
     #}

     # Some generic URL manipulation, useful for all templates that follow
     # First remove the Google Analytics added parameters, useless for our backend
     if (req.url ~ "(\?|&)(utm_source|utm_medium|utm_campaign|gclid|cx|ie|cof|siteurl)=") {
         set req.url = regsuball(req.url, "&(utm_source|utm_medium|utm_campaign|gclid|cx|ie|cof|siteurl)=([A-z0-9_\-\.%25]+)", "");
         set req.url = regsuball(req.url, "\?(utm_source|utm_medium|utm_campaign|gclid|cx|ie|cof|siteurl)=([A-z0-9_\-\.%25]+)", "?");
         set req.url = regsub(req.url, "\?&", "?");
         set req.url = regsub(req.url, "\?$", "");
     }

     # Strip hash, server doesn't need it.
     if (req.url ~ "\#") {
         set req.url = regsub(req.url, "\#.*$", "");
     }

     # Strip a trailing ? if it exists
     if (req.url ~ "\?$") {
         set req.url = regsub(req.url, "\?$", "");
     }

     # Some generic cookie manipulation, useful for all templates that follow
     # Remove the "has_js" cookie
     set req.http.Cookie = regsuball(req.http.Cookie, "has_js=[^;]+(; )?", "");

     # Remove any Google Analytics based cookies
     set req.http.Cookie = regsuball(req.http.Cookie, "__utm.=[^;]+(; )?", "");
     set req.http.Cookie = regsuball(req.http.Cookie, "_ga=[^;]+(; )?", "");
     set req.http.Cookie = regsuball(req.http.Cookie, "utmctr=[^;]+(; )?", "");
     set req.http.Cookie = regsuball(req.http.Cookie, "utmcmd.=[^;]+(; )?", "");
     set req.http.Cookie = regsuball(req.http.Cookie, "utmccn.=[^;]+(; )?", "");

     # Remove the Quant Capital cookies (added by some plugin, all __qca)
     set req.http.Cookie = regsuball(req.http.Cookie, "__qc.=[^;]+(; )?", "");

     # Remove the AddThis cookies
     set req.http.Cookie = regsuball(req.http.Cookie, "__atuvc=[^;]+(; )?", "");

     # Remove a ";" prefix in the cookie if present
     set req.http.Cookie = regsuball(req.http.Cookie, "^;\s*", "");

     # Are there cookies left with only spaces or that are empty?
     if (req.http.cookie ~ "^\s*$") {
         unset req.http.cookie;
     }

     # Normalize Accept-Encoding header
     # straight from the manual: https://www.varnish-cache.org/docs/3.0/tutorial/vary.html
     if (req.http.Accept-Encoding) {
         if (req.url ~ "\.(jpg|png|gif|gz|tgz|bz2|tbz|mp3|ogg)$") {
             # No point in compressing these
             unset req.http.Accept-Encoding;
         } elsif (req.http.Accept-Encoding ~ "gzip") {
             set req.http.Accept-Encoding = "gzip";
         } elsif (req.http.Accept-Encoding ~ "deflate") {
             set req.http.Accept-Encoding = "deflate";
         } else {
             # unkown algorithm
             unset req.http.Accept-Encoding;
         }
     }

     # Large static files should be piped, so they are delivered directly to the end-user without
     # waiting for Varnish to fully read the file first.
     # TODO: once the Varnish Streaming branch merges with the master branch, use streaming here to avoid locking.
     if (req.url ~ "^[^?]*\.(mp[34]|rar|tar|tgz|gz|wav|zip)(\?.*)?$") {
         unset req.http.Cookie;
         return (pipe);
     }

     # Remove all cookies for static files
     # A valid discussion could be held on this line: do you really need to cache static files that don't cause load? Only if you have memory left.
     # Sure, there's disk I/O, but chances are your OS will already have these files in their buffers (thus memory).
     # Before you blindly enable this, have a read here: http://mattiasgeniar.be/2012/11/28/stop-caching-static-files/
     if (req.url ~ "^[^?]*\.(bmp|bz2|css|doc|eot|flv|gif|gz|ico|jpeg|jpg|js|less|pdf|png|rtf|swf|txt|woff|xml)(\?.*)?$") {
         unset req.http.Cookie;
         return (hash);
     }

     # Send Surrogate-Capability headers to announce ESI support to backend
     set req.http.Surrogate-Capability = "key=ESI/1.0";

     if (req.http.Authorization) {
         # Not cacheable by default
         return (pass);
     }

     return (hash);
 }

 sub vcl_pipe {
     # Note that only the first request to the backend will have
     # X-Forwarded-For set.  If you use X-Forwarded-For and want to
     # have it set for all requests, make sure to have:
     # set bereq.http.connection = "close";
     # here.  It is not set by default as it might break some broken web
     # applications, like IIS with NTLM authentication.

     #set bereq.http.Connection = "Close";
     return (pipe);
 }

 sub vcl_pass {
 #    return (pass);
 }

 # The data on which the hashing will take place
 sub vcl_hash {
     hash_data(req.url);

     if (req.http.host) {
         hash_data(req.http.host);
     } else {
         hash_data(server.ip);
     }

     # hash cookies for requests that have them
     if (req.http.Cookie) {
         hash_data(req.http.Cookie);
     }
 }

 sub vcl_hit {
     return (deliver);
 }

 sub vcl_miss {
     return (fetch);
 }

 # Handle the HTTP request coming from our backend
 sub vcl_backend_response {
     # Pause ESI request and remove Surrogate-Control header
     if (beresp.http.Surrogate-Control ~ "ESI/1.0") {
         unset beresp.http.Surrogate-Control;
         set beresp.do_esi = true;
     }

     # Enable cache for all static files
     # The same argument as the static caches from above: monitor your cache size, if you get data nuked out of it, consider giving up the static file cache.
     # Before you blindly enable this, have a read here: http://mattiasgeniar.be/2012/11/28/stop-caching-static-files/
     if (bereq.url ~ "^[^?]*\.(bmp|bz2|css|doc|eot|flv|gif|gz|ico|jpeg|jpg|js|less|mp[34]|pdf|png|rar|rtf|swf|tar|tgz|txt|wav|woff|xml|zip)(\?.*)?$") {
         unset beresp.http.set-cookie;
     }

     # Sometimes, a  or  redirect formed via Apache's mod_rewrite can mess with the HTTP port that is being passed along.
     # This often happens with simple rewrite rules  and Apache on : on the same box.
     # A redirect can , where it should be :.
     # This may need finetuning on your setup.
     #
     # To prevent accidental replace, we only filter the / redirects for now.
      || beresp.status == ) {
         set beresp.http.Location = regsub(beresp.http.Location, ":[0-9]+", "");
     }

     # Set 2min cache if unset for static files
     if (beresp.ttl <= 0s || beresp.http.Set-Cookie || beresp.http.Vary == "*") {
         set beresp.ttl = 120s;
         set beresp.uncacheable = true;
         return (deliver);
     }

     # Allow stale content, in case the backend goes down.
     set beresp.grace = 6h;

     return (deliver);
 }

 # The routine when we deliver the HTTP request to the user
 # Last chance to modify headers that are sent to the client
 sub vcl_deliver {
     ) {
         set resp.http.X-Cache = "varnish-cached"; ##这里的值是浏览器显示的名称 (见测试那一步的截图)
     } else {
         set resp.http.x-Cache = "varnish-nocache";
     }

     # Remove some headers: PHP version
     unset resp.http.X-Powered-By;

     # Remove some headers: Apache version & OS
     unset resp.http.Server;
     unset resp.http.X-Drupal-Cache;
     unset resp.http.X-Varnish;
     unset resp.http.Via;
     unset resp.http.Link;

     return (deliver);
 }

 sub vcl_synth {
     ) {
         # We use this special error status  to force redirects with  (permanent) redirects
         # To use this, call the following from anywhere  "http://host/new.html"
         set resp.status = ;
         set resp.http.Location = resp.reason;
         return (deliver);
     } elseif (resp.status == ) {
         # And we use error status  to force redirects with a  (temporary) redirect
         # To use this, call the following from anywhere  "http://host/new.html"
         set resp.status = ;
         set resp.http.Location = resp.reason;
         return (deliver);
     }

     return (deliver);
 }

 sub vcl_init {
     return (ok);
 }

 sub vcl_fini {
     return (ok);
 }

varnish 文件内容

 # chkconfig:
 # description: varnish ....
 #!/bin/sh

 start()
 {
         echo -n $"starting varnish..."
         /usr/local/varnish/sbin/varnishd -P /tmp/varnish.pid -a  -T  -f /usr/local/varnish/etc/web.conf -n /var/varnish_cache -s malloc,1G -P client_http11=on  ##启动命令, 注意配置文件路径: /usr/local/varnish/etc/web.conf
         echo
 }

 stop()
 {
         echo -n $"stopping varnish..."
         pkill varnish
         echo
 }

 restart()
 {
    stop

    start
 }

 case "$1" in
 start)
 start
 ;;
 stop)
 stop
 ;;
 restart)
 restart
 ;;
 *)
 echo $"Usage: $0 {start|stop|restart}"
 esac

varnish代理缓存服务器的安装与使用的更多相关文章

  1. nginx反向代理缓存服务器的构建

    一:代理服务可简单的分为正向代理和反向代理: 正向代理:用于代理内部网络对Internet的连接请求(如VPN/NAT),客户端指定代理服务器,并将本来要直接发送给目标Web服务器的HTTP请求先发送 ...

  2. 高性能代理缓存服务器—Squid

    Squid是什么? Squid是一款比较知名的开源代理缓存软件,它不仅可以跑在linux上还可以跑在windows以及Unix上,它的技术已经非常成熟.目前使用Squid的用户也是十分广泛的. Squ ...

  3. Linux平台部署varnish 高性能缓存服务器

    一:varnish部署前准备: 1.1相关软件以及系统,web服务 系统要求:Centos 6(以上) (64位) 相关中间件:varnish-4.0.2 1.2相关系统依赖包安装检查准备 1.2.1 ...

  4. redis(二)redis+TCMALLOC高性能的缓存服务器的安装配置

    安装  1准备编译环境    yum -y install gcc gcc+ gcc-c++ openssl openssl-devel pcre pcre-devel  2 下载源码包(由于goog ...

  5. Varnish http缓存服务器

    http://blog.51cto.com/hexiaoshuai/1909183 https://jefferywang.gitbooks.io/varnish_4_1_doc_zh/content ...

  6. squid代理缓存服务器

    参考文章 http://www.cnblogs.com/mchina/p/3812190.html ;

  7. linux之DNS主域,从域,缓存服务器的架设

    DNS主域,从域,缓存服务器的架设 DNS域名系统 组织域 顶级域  域名解析过程迭代递归 DNS(Domain Name System ) 在Internet中使用IP地址来确定计算机的地址. 为了 ...

  8. 高性能缓存服务器Varnish

    一.Varnish概述 Varnish是一款高性能的.开源的反向代理服务器和缓存服务器,计算机系统的除了有内存外,还有CPU的L1.L2,甚至L3级别的缓存,Varnish的设计架构就是利用操作系统的 ...

  9. Varnish,Nginx搭建缓存服务器

    Varnish,Nginx搭建缓存服务器 一. varnish 1.安装pcre库,兼容正则表达式 # tar -zxvf pcre-8.10.tar.gz # cd pcre-8.10 # ./co ...

随机推荐

  1. 这一次,我连 web.xml 都不要了,纯 Java 搭建 SSM 环境!

    在 Spring Boot 项目中,正常来说是不存在 XML 配置,这是因为 Spring Boot 不推荐使用 XML ,注意,并非不支持,Spring Boot 推荐开发者使用 Java 配置来搭 ...

  2. vue.js移动端app实战1

    本系列将会用vue.js2制作一个移动端的webapp单页面,页面不多,大概在7,8个左右,不过麻雀虽小,五脏俱全,常用的效果如轮播图,下拉刷新,上拉加载,图片懒加载都会用到.css方面也会有一些描述 ...

  3. wxWidgets 安装方法(Windows 8.1 + Visual Studio 2013)

    在windows 8.1上面,搭建基于visual studio 2013的wxWidgets的开发环境,方法如下: 下载  目前最新版本为3.0.0,下载地址: http://sourceforge ...

  4. web.config配置数据库连接 【转】

    http://www.cnblogs.com/breezeblew/archive/2008/05/01/1178719.html 第一种: 取连接字符串 = System.Web.Configura ...

  5. 如何选择Haproxy和Nginx

    对于做软负载,我们都知道主流的方案有LVS.Haproxy.Nginx!那么对于Haproxy和Nginx,我们如何选择呢?回答这个问题之前,我根据个人使用经验来讲下它们的特点! Haproxy特点 ...

  6. java把一个文件的内容复制到另外一个文件

    /** * java把一个文件的内容复制到另外一个文件 */import java.io.File;import java.io.FileInputStream;import java.io.File ...

  7. mbr 备份

    MBR共512字节 (1) 第1-446字节:调用操作系统的机器码. (2) 第447-510字节:分区表(Partition table). (3) 第511-512字节:主引导记录签名(0x55和 ...

  8. Android Touch事件传递机制具体解释 下

    尊重原创:http://blog.csdn.net/yuanzeyao/article/details/38025165 资源下载:http://download.csdn.net/detail/yu ...

  9. HDU 3657 Game(取数 最小割)经典

    Game Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submi ...

  10. Java使用笔记之对象比较

    1.关于java对象的比较,经常会遇见比较某个两个对象的多个属性是否相等,可以通过重写对象equals方法来实现. 比如有两个User,如果姓名和年龄相等的话,我们就可以认为他们重复的数据.那么我们就 ...