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. MapReduce1 工作机制

    本文转自:Hadoop MapReduce 工作机制 工作流程 作业配置 作业提交 作业初始化 作业分配 作业执行 进度和状态更新 作业完成 错误处理 作业调度 shule(mapreduce核心)和 ...

  2. WPS复制时删除超链接

    按Ctrl+A全选,之后再按Ctrl+Shift+F9,即可一次性全部删除超链接.

  3. IOS 暂停和恢复CALayer上的动画(转)

    coreAnimation的动画是存在于CALayer上面的,有些时候需要突然暂停某个组件的动画效果,同时保留当前动画的状态, 如果是用removeAnimation会显得很突兀,不够平滑,所以可以利 ...

  4. DotnetBrowser入门教程-(1)浏览器控件使用

    先简单介绍下DotnetBrowser作为基本浏览器控件的使用: 1.创建基于.net 4.0的桌面项目,如下所示: 2.首次使用的时候在工具栏里添加dotnetbrowser控件,如下图所示: 3. ...

  5. 【Salvation】——人物角色动画实现

    写在前面:这个角色动画主要使用JavaScript编写脚本,在Unity3D游戏引擎的环境中实现. 一.显示角色并实现镜像效果 1.显示贴图: create→cube→修改名称为player,位置归0 ...

  6. 2017.2.28 activiti实战--第六章--任务表单(一)动态表单

    学习资料:<Activiti实战> 第六章 任务表单(一)动态表单 内容概览:本章要完成一个OA(协同办公系统)的请假流程的设计,从实用的角度,讲解如何将activiti与业务紧密相连. ...

  7. PropertyGrid—隐藏某些Public属性

    1.定义一个继承ControlDesigner 的类 public class MyControlDesigner:System.Windows.Forms.Design.ControlDesigne ...

  8. @Cacheable注解在spring3中的使用-实现缓存

    转:  http://blog.csdn.net/chenleixing/article/details/44815443 在软件开发中使用缓存已经有一个非常久的历史了.缓存是一种很好的设计思想,一旦 ...

  9. OCP-1Z0-051-题目解析-第16题

    16. Evaluate the following query: SQL> SELECT promo_name q'{'s start date was }' promo_begin_date ...

  10. HBase笔记

    吴超 1.1 Hbase是Hadoop中的数据库,Hadoop还需要数据库吗?我们学的Hadoop是一个分布式的存储和计算的平台 为什么要在他上面建一个数据库呢,数据库是干什么的呢,数据库是一个管理系 ...