Varnish+Nginx 配置----Varnish

Varnish+Nginx 配置----Varnish

分類: 緩存2013-04-17 14:53 3746人閱讀 評論(0) 收藏 舉報css

NginxVarnish緩存html

最近項目引入反向代理和緩存,熟悉了一下Squid、Apache、Valish、Nginx,根據項目實際進行選擇,客觀來講,採用Linux系統部署最好,也沒有什麼難度,但實際狀況必須採用Windows系統(本着方案要結合現實的原則,研究要以Windows平臺爲主)。
web


一  Varnish啓動:chrome

[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   cookie

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

相關文章
相關標籤/搜索