Varnish+Nginx 配置----Varnish

Varnish+Nginx 配置----Varnish

分类: 缓存2013-04-17 14:53 3746人阅读 评论(0) 收藏 举报

NginxVarnish缓存

最近项目引入反向代理和缓存,熟悉了一下Squid、Apache、Valish、Nginx,根据项目实际进行选择,客观来说,采用Linux系统部署最好,也没有什么难度,但实际情况必须采用Windows系统(本着方案要结合现实的原则,研究要以Windows平台为主)。


一  Varnish启动:

[plain] view plaincopy

  1. e:  

  2. cd e:\varnish\bin  

  3. set PATH=%CD%;%PATH%  

  4. varnishd -a :7575 -T :11212 -f /etc/varnish_cst_cfg.vcl -s file,E:/var/varnish/tg/cache/jjtg,1024M -p thread_pool_max=5000 -p thread_pool_min=100   

  5. REM pause  

二  Varnish配置:

[plain] view plaincopy

  1. # This is a basic VCL configuration file for varnish.  See the vcl(7)  

  2. # man page for details on VCL syntax and semantics.  

  3. #   

  4. # Default backend definition.  Set this to point to your content  

  5. # server.  

  6.    

  7. backend tgweb {  

  8.      .host = "192.168.45.45";  

  9.      .port = "7574";  

  10.      .connect_timeout = 20s;  

  11.      .first_byte_timeout = 20s;  

  12.      .between_bytes_timeout = 20s;  

  13.  }  

  14.   

  15. #允许刷新缓存的规则  

  16. #acl purgeAllow {  

  17. #     #只能本机进行刷新  

  18. #     "localhost";  

  19. #}  

  20.    

  21. # Below is a commented-out copy of the default VCL logic.  If you  

  22. # redefine any of these subroutines, the built-in logic will be  

  23. # appended to your code.  

  24.   

  25. sub vcl_recv {  

  26.     #判断请求主机,跳转到相应后端服务器  

  27.     if(req.http.host ~ "^(.*)(tg.stockjyb.com:7575)")  

  28.     {  

  29.         set req.backend=tgweb;  

  30.     }else{  

  31.         error 408 "Hostname not found";   

  32.     }  

  33.       

  34.     #grace缓存过期仍存放  

  35.     # 若backend是健康的,则仅grace 5s,如果backend不健康,则grace 1m。  

  36.     # 这里,5s的目的是为了提高高并发时的吞吐率;  

  37.     # 1m的目的是,backend挂了之后,还能继续服务一段时间,期望backend挂的不要太久。。。  

  38.     if (req.backend.healthy) {  

  39.         set req.grace = 5s;  

  40.     } else {  

  41.         set req.grace = 1m;  

  42.     }  

  43.   

  44.     #刷新缓存的处理  

  45.     #if (req.request == "PURGE"){  

  46.     #    if(!client.ip ~ purgeAllow) {  

  47.     #            error 405 "Not allowed.";  

  48.     #    }  

  49.     #    #转到hit或者miss处理  

  50.     #    return (lookup);  

  51.     #}  

  52.   

  53.     #移除一些特定格式的cookie  

  54.     if (req.url ~ "^(.*)\.(jpg|png|gif|jpeg|flv|bmp|gz|tgz|bz2|tbz|js|css|html|htm)($|\?)" ) {  

  55.          #移除cookie,以便能缓存到varnish  

  56.          unset req.http.cookie;  

  57.     }  

  58.   

  59.    #Accept-Encoding 是浏览器发给服务器,声明浏览器支持的编码类型的  

  60.    #修正客户端的Accept-Encoding头信息  

  61.    #防止个别浏览器发送类似 deflate, gzip  

  62.     if (req.http.Accept-Encoding) {  

  63.         if (req.url ~ "^(.*)\.(jpg|png|gif|jpeg|flv|bmp|gz|tgz|bz2|tbz)($|\?)" ) {  

  64.             remove req.http.Accept-Encoding;  

  65.         }else if (req.http.Accept-Encoding ~ "gzip"){  

  66.             set req.http.Accept-Encoding = "gzip";  

  67.         } else if (req.http.Accept-Encoding ~ "deflate"){  

  68.             set req.http.Accept-Encoding = "deflate";  

  69.         } else if (req.http.Accept-Encoding ~ "sdch"){  

  70.             #chrome新增加的压缩  

  71.             set req.http.Accept-Encoding = "sdch";  

  72.         }else {  

  73.             remove req.http.Accept-Encoding;  

  74.         }  

  75.     }  

  76.         

  77.     #首次访问增加X-Forwarded-For头信息,方便后端程序获取客户端ip  

  78.     if (req.restarts == 0) {  

  79.         if (req.http.x-forwarded-for) {  

  80.             set req.http.X-Forwarded-For =  

  81.             req.http.X-Forwarded-For + ", " + client.ip;  

  82.         } else {  

  83.             set req.http.X-Forwarded-For = client.ip;  

  84.         }  

  85.     }  

  86.   

  87.    #if (req.request != "GET" &&  

  88.    #    req.request != "HEAD" &&  

  89.    #    req.request != "PUT" &&  

  90.    #    req.request != "POST" &&  

  91.    #    req.request != "TRACE" &&  

  92.    #    req.request != "OPTIONS" &&  

  93.    #    req.request != "DELETE") {  

  94.    #    /* Non-RFC2616 or CONNECT which is weird. */  

  95.    #    return (pipe);  

  96.    #}  

  97.      if (req.request != "GET" && req.request != "HEAD") {  

  98.          /* We only deal with GET and HEAD by default */  

  99.          return (pass);  

  100.      }  

  101.      if (req.http.Authorization) {  

  102.          /* Not cacheable by default */  

  103.          return (pass);  

  104.      }  

  105.      #js,css文件都有Cookie,不能每次都去后台服务器去取  

  106.      #if (req.http.Cookie) {  

  107.      #    /* Not cacheable by default */  

  108.      #    return (pass);  

  109.      #}  

  110.       

  111.      #如果请求的是动态页面直接转发到后端服务器  

  112.      if (req.url ~ "^(.*)\.(aspx|asmx|ashx)($|.*)") {  

  113.           return (pass);  

  114.      }  

  115.      return (lookup);  

  116.  }  

  117.    

  118.  sub vcl_pipe {  

  119.      # Note that only the first request to the backend will have  

  120.      # X-Forwarded-For set.  If you use X-Forwarded-For and want to  

  121.      # have it set for all requests, make sure to have:  

  122.      # set bereq.http.connection = "close";  

  123.      # here.  It is not set by default as it might break some broken web  

  124.      # applications, like IIS with NTLM authentication.  

  125.      return (pipe);  

  126.  }  

  127.    

  128. #放过,让其直接去后台服务器请求数据  

  129.  sub vcl_pass {  

  130.      return (pass);  

  131.  }  

  132.    

  133.  sub vcl_hash {  

  134.      hash_data(req.url);  

  135.      if (req.http.host) {  

  136.          hash_data(req.http.host);  

  137.      } else {  

  138.          hash_data(server.ip);  

  139.      }  

  140.      #支持压缩的要增加,防止发送给不支持压缩的浏览器压缩的内容  

  141.      if(req.http.Accept-Encoding){  

  142.           hash_data(req.http.Accept-Encoding);  

  143.      }  

  144.      return (hash);  

  145.  }  

  146.    

  147. #缓存服务器lookup查找命中:hit  

  148.  sub vcl_hit {  

  149.      #刷新缓存的请求操作,设置TTL为0,返回处理结果代码  

  150.      #if (req.request == "PURGE") {  

  151.      #     set obj.ttl = 0s;  

  152.      #     error 200 "Purged.";  

  153.      # }  

  154.   

  155.      //#缓存服务器命中后(查找到了)  

  156.      return (deliver);  

  157.  }  

  158.    

  159. #缓存服务器lookup查找没有命中:miss  

  160.  sub vcl_miss {  

  161.     #刷新缓存的请求操作,  

  162.     #if (req.request == "PURGE") {  

  163.     #    error 404 "Not in cache.";  

  164.     #}  

  165.   

  166.     //#缓存服务器没有命中(去后台服务器取)  

  167.      return (fetch);  

  168.  }  

  169.    

  170. #从后台服务器取回数据后,视情况是否进行缓存  

  171.  sub vcl_fetch {  

  172.   

  173.     #如果请求的是动态页面直接发转发  

  174.     #动态请求回来的,一定要放在前面处理  

  175.     if (req.url ~ "^(.*)\.(aspx|asmx|ashx)($|.*)") {  

  176.         set beresp.http.Cache-Control="no-cache, no-store";  

  177.         unset beresp.http.Expires;  

  178.         return (deliver);  

  179.     }  

  180.   

  181.     # 仅当该请求可以缓存时,才设置beresp.grace,若该请求不能被缓存,则不设置beresp.grace  

  182.     if (beresp.ttl > 0s) {  

  183.         set beresp.grace = 1m;  

  184.     }    

  185.   

  186.      if (beresp.ttl <= 0s ||  

  187.          beresp.http.Set-Cookie ||  

  188.          beresp.http.Vary == "*") {  

  189.             /*  

  190.              * Mark as "Hit-For-Pass" for the next 2 minutes  

  191.              */  

  192.             set beresp.ttl = 120 s;  

  193.             #下次请求时不进行lookup,直接pass  

  194.             return (hit_for_pass);  

  195.      }  

  196.   

  197.     #设置从后台服务器获得的特定格式文件的缓存TTL  

  198.     if (req.url ~ "^(.*)\.(pdf|xls|ppt|doc|docx|xlsx|pptx|chm|rar|zip)($|\?)")       

  199.     {  

  200.         #移除服务器发送的cookie   

  201.         unset beresp.http.Set-Cookie;  

  202.         #加上缓存时间  

  203.         set beresp.ttl = 30d;  

  204.         return (deliver);  

  205.     }else if(req.url ~ "^(.*)\.(bmp|jpeg|jpg|png|gif|svg|png|ico|txt|css|js|html|htm)($|\?)"){  

  206.         #移除服务器发送的cookie   

  207.         unset beresp.http.Set-Cookie;  

  208.         #加上缓存时间  

  209.         set beresp.ttl = 15d;  

  210.         return (deliver);  

  211.     }else if(req.url ~ "^(.*)\.(mp3|wma|mp4|rmvb|ogg|mov|avi|wmv|mpeg|mpg|dat|3pg|swf|flv|asf)($|\?)"){  

  212.         #移除服务器发送的cookie   

  213.         unset beresp.http.Set-Cookie;  

  214.         #加上缓存时间  

  215.         set beresp.ttl = 30d;  

  216.         return (deliver);  

  217.     }  

  218.   

  219.     #从后台服务器返回的response信息中,没有缓存的,不缓存  

  220.     if (beresp.http.Pragma ~"no-cache" || beresp.http.Cache-Control ~"no-cache" || beresp.http.Cache-Control ~"private") {  

  221.             return (deliver);  

  222.     }  

  223.     return (deliver);  

  224.  }  

  225.    

  226. #缓存服务器发送到客户端前调用  

  227.  sub vcl_deliver {  

  228.     #下面是添加一个Header标识,以判断缓存是否命中。  

  229.     if (obj.hits > 0) {  

  230.         set resp.http.X-Cache = "HIT from TG.varnish-cache.jjcj.com";  

  231.        #set resp.http.X-Varnish = "HIT from TG.varnish-cache.jjcj.com";  

  232.     } else {  

  233.         set resp.http.X-Cache = "MISS from TG.varnish-cache.jjcj.com";  

  234.        #set resp.http.X-Varnish = "MISS from TG.varnish-cache.jjcj.com";  

  235.     }  

  236.     #去掉不是必须的header  

  237.     unset resp.http.Vary;  

  238.     unset resp.http.X-Powered-By;  

  239.     unset resp.http.X-AspNet-Version;  

  240.     return (deliver);  

  241.  }  

  242.    

  243.  sub vcl_error {  

  244.      set obj.http.Content-Type = "text/html; charset=utf-8";  

  245.      set obj.http.Retry-After = "5";  

  246.      synthetic {"  

  247.  <?xml version="1.0" encoding="utf-8"?>  

  248.  <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"  

  249.   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">  

  250.  <html>  

  251.    <head>  

  252.      <title>"} + obj.status + " " + obj.response + {"</title>  

  253.    </head>  

  254.    <body>  

  255.      <h1>Error "} + obj.status + " " + obj.response + {"</h1>  

  256.      <p>"} + obj.response + {"</p>  

  257.      <h3>Guru Meditation:</h3>  

  258.      <p>XID: "} + req.xid + {"</p>  

  259.      <hr>  

  260.      <p>Varnish cache server</p>  

  261.    </body>  

  262.  </html>  

  263.  "};  

  264.      return (deliver);  

  265.  }  

  266.    

  267.  sub vcl_init {  

  268.     return (ok);  

  269.  }  

  270.    

  271.  sub vcl_fini {  

  272.     return (ok);  

  273.  }  



转载于:https://my.oschina.net/epiclight/blog/465731

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值