Advertisement
shakalandy

varnish config

Mar 26th, 2014
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 5.83 KB | None | 0 0
  1. backend default {
  2.     .host = "127.0.0.1";
  3.     .port = "8080";
  4. }
  5. # Block 2: Define a key based on the User-Agent which can be used for hashing.
  6. # Also set the PS-CapabilityList header for PageSpeed server to respect.
  7. sub generate_user_agent_based_key {
  8.     # Define placeholder PS-CapabilityList header values for large and small
  9.     # screens with no UA dependent optimizations. Note that these placeholder
  10.     # values should not contain any of ll, ii, dj, jw or ws, since these
  11.     # codes will end up representing optimizations to be supported for the
  12.     # request.
  13.     set req.http.default_ps_capability_list_for_large_screens = "LargeScreen.SkipUADependentOptimizations:";
  14.     set req.http.default_ps_capability_list_for_small_screens = "TinyScreen.SkipUADependentOptimizations:";
  15.  
  16.     # As a fallback, the PS-CapabilityList header that is sent to the upstream
  17.     # PageSpeed server should be for a large screen device with no browser
  18.     # specific optimizations.
  19.     set req.http.PS-CapabilityList = req.http.default_ps_capability_list_for_large_screens;
  20.  
  21.     # Cache-fragment 1: Desktop User-Agents that support lazyload_images (ll),
  22.     # inline_images (ii) and defer_javascript (dj).
  23.     # Note: Wget is added for testing purposes only.
  24.     if (req.http.User-Agent ~ "(?i)Chrome/|Firefox/|MSIE |Safari|Wget") {
  25.       set req.http.PS-CapabilityList = "ll,ii,dj:";
  26.     }
  27.     # Cache-fragment 2: Desktop User-Agents that support lazyload_images (ll),
  28.     # inline_images (ii), defer_javascript (dj), webp (jw) and lossless_webp
  29.     # (ws).
  30.     if (req.http.User-Agent ~
  31.         "(?i)Chrome/[2][3-9]+\.|Chrome/[[3-9][0-9]+\.|Chrome/[0-9]{3,}\.") {
  32.       set req.http.PS-CapabilityList = "ll,ii,dj,jw,ws:";
  33.     }
  34.     # Cache-fragment 3: This fragment contains (a) Desktop User-Agents that
  35.     # match fragments 1 or 2 but should not because they represent older
  36.     # versions of certain browsers or bots and (b) Tablet User-Agents that
  37.     # on all browsers and use image compression qualities applicable to large
  38.     # screens. Note that even Tablets that are capable of supporting inline or
  39.     # webp images, e.g. Android 4.1.2, will not get these advanced
  40.     # optimizations.
  41.     if (req.http.User-Agent ~ "(?i)Firefox/[1-2]\.|MSIE [5-8]\.|bot|Yahoo!|Ruby|RPT-HTTPClient|(Google \(\+https\:\/\/developers\.google\.com\/\+\/web\/snippet\/\))|Android|iPad|TouchPad|Silk-Accelerated|Kindle Fire") {
  42.       set req.http.PS-CapabilityList = req.http.default_ps_capability_list_for_large_screens;
  43.     }
  44.     # Cache-fragment 4: Mobiles and small screen Tablets will use image compression
  45.     # qualities applicable to small screens, but all other optimizations will be
  46.     # those that work on all browsers.
  47.     if (req.http.User-Agent ~ "(?i)Mozilla.*Android.*Mobile*|iPhone|BlackBerry|Opera Mobi|Opera Mini|SymbianOS|UP.Browser|J-PHONE|Profile/MIDP|portalmmm|DoCoMo|Obigo|Galaxy Nexus|GT-I9300|GT-N7100|HTC One|Nexus [4|7|S]|Xoom|XT907") {
  48.       set req.http.PS-CapabilityList = req.http.default_ps_capability_list_for_small_screens;
  49.     }
  50.     # Remove placeholder header values.
  51.     remove req.http.default_ps_capability_list_for_large_screens;
  52.     remove req.http.default_ps_capability_list_for_large_screens;
  53. }
  54.  
  55.  
  56. # Drop any cookies sent to Wordpress.
  57. # except when previewing
  58. sub vcl_recv {
  59.     if ( !(req.url ~ "(wp-login|wp-admin|preview=true)") ) {
  60.         unset req.http.cookie;
  61.     }
  62. }
  63.  
  64. sub vcl_hash {
  65.   # Block 3: Use the PS-CapabilityList value for computing the hash.
  66.   hash_data(req.http.PS-CapabilityList);
  67. }
  68.  
  69. # Block 3a: Define ACL for purge requests
  70. acl purge {
  71.   # Purge requests are only allowed from localhost.
  72.   "localhost";
  73.   "127.0.0.1";
  74. }
  75. # Block 3b: Issue purge when there is a cache hit for the purge request.
  76. sub vcl_hit {
  77.   if (req.request == "PURGE") {
  78.     purge;
  79.     error 200 "Purged.";
  80.   }
  81. }
  82.  
  83. # Block 3c: Issue a no-op purge when there is a cache miss for the purge
  84. # request.
  85. sub vcl_miss {
  86.   if (req.request == "PURGE") {
  87.      purge;
  88.      error 200 "Purged.";
  89.   }
  90. }
  91.  
  92. # Block 4: In vcl_recv, on receiving a request, call the method responsible for
  93. # generating the User-Agent based key for hashing into the cache.
  94.  
  95. sub vcl_recv {
  96.   call generate_user_agent_based_key;
  97.   # Block 3d: Verify the ACL for an incoming purge request and handle it.
  98.   if (req.request == "PURGE") {
  99.     if (!client.ip ~ purge) {
  100.       error 405 "Not allowed.";
  101.     }
  102.     return (lookup);
  103.   }
  104.   # Blocks which decide whether cache should be bypassed or not go here.
  105.  
  106.   # Block 5a: Bypass the cache for .pagespeed. resource. PageSpeed has its own
  107.   # cache for these, and these could bloat up the caching layer.
  108.   if (req.url ~ "\.pagespeed\.([a-z]\.)?[a-z]{2}\.[^.]{10}\.[^.]+") {
  109.     # Skip the cache for .pagespeed. resource.  PageSpeed has its own
  110.     # cache for these, and these could bloat up the caching layer.
  111.     return (pass);
  112.   }
  113.  # Block 5b: Only cache responses to clients that support gzip.  Most clients
  114.   # do, and the cache holds much more if it stores gzipped responses.
  115.   if (req.http.Accept-Encoding !~ "gzip") {
  116.     return (pass);
  117.   }
  118. }
  119. # Block 6: Mark HTML uncacheable by caches beyond our control.
  120. sub vcl_fetch {
  121.    if (beresp.http.Content-Type ~ "text/html") {
  122.      # Hide the upstream cache control header.
  123.      remove beresp.http.Cache-Control;
  124.      # Add no-cache Cache-Control header for html.
  125.      set beresp.http.Cache-Control = "no-cache, max-age=0";
  126.    }
  127.    return (deliver);
  128. }
  129.  
  130. # Block 7: Add a header for identifying cache hits/misses.
  131. sub vcl_deliver {
  132.   if (obj.hits > 0) {
  133.     set resp.http.X-Cache = "HIT";
  134.   } else {
  135.     set resp.http.X-Cache = "MISS";
  136.   }
  137. }
  138.  
  139.  
  140. # Drop any cookies Wordpress tries to send back to the client.
  141. # except when previewing
  142. sub vcl_fetch {
  143.     if ( !(req.url ~ "(wp-login|wp-admin|preview=true)") ) {
  144.         unset beresp.http.set-cookie;
  145.     }
  146. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement