Advertisement
Streamingdb-net

default.vcl Varnish wordpress

Aug 27th, 2011
202
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.61 KB | None | 0 0
  1. backend origin {
  2.     .host = "localhost";
  3.     .port = "8080";
  4. }
  5.  
  6.  
  7. sub vcl_recv {
  8.     # only using one backend
  9.    set req.backend = origin;
  10.    
  11.     # set standard proxied ip header for getting original remote address
  12.    set req.http.X-Forwarded-For = client.ip;
  13.    
  14.     # logged in users must always pass
  15.    if( req.url ~ "^/wp-(login|admin)" || req.http.Cookie ~ "wordpress_logged_in_" ){
  16.         return (pass);
  17.     }
  18.        
  19.     # don't cache search results
  20.    if( req.url ~ "\?s=" ){
  21.         return (pass);
  22.     }
  23.  
  24.     # always pass through posted requests and those with basic auth
  25.    if ( req.request == "POST" || req.http.Authorization ) {
  26.          return (pass);
  27.     }
  28.    
  29.     # else ok to fetch a cached page
  30.    unset req.http.Cookie;
  31.     return (lookup);
  32. }
  33.  
  34.  
  35.  
  36. sub vcl_fetch {
  37.  
  38.     # remove some headers we never want to see
  39.    unset beresp.http.Server;
  40.     unset beresp.http.X-Powered-By;
  41.    
  42.     # only allow cookies to be set if we're in admin area - i.e. commenters stay logged out
  43.    if( beresp.http.Set-Cookie && req.url !~ "^/wp-(login|admin)" ){
  44.         unset beresp.http.Set-Cookie;
  45.     }
  46.    
  47.     # don't cache response to posted requests or those with basic auth
  48.    if ( req.request == "POST" || req.http.Authorization ) {
  49.          return (pass);
  50.     }
  51.    
  52.     # Trust Varnish if it says this is not cacheable
  53.    if ( ! beresp.cacheable ) {
  54.          return (pass);
  55.      }
  56.  
  57.     # only cache status ok
  58.    if ( beresp.status != 200 ) {
  59.         return (pass);
  60.     }
  61.  
  62.         # don't cache search results
  63.        if( req.url ~ "\?s=" ){
  64.             return (pass);
  65.         }
  66.    
  67.     # else ok to cache the response
  68.    set beresp.ttl = 24h;
  69.     return (deliver);
  70. }
  71.  
  72.  
  73.  
  74. sub vcl_deliver {
  75.     # add debugging headers, so we can see what's cached
  76.    if (obj.hits > 0) {
  77.         set resp.http.X-Cache = "HIT";
  78.      }
  79.      else {
  80.         set resp.http.X-Cache = "MISS";
  81.     }
  82.     # remove some headers added by varnish
  83.    unset resp.http.Via;
  84.     unset resp.http.X-Varnish;
  85. }
  86.  
  87.  
  88.  
  89. sub vcl_hash {
  90.     set req.hash += req.url;
  91.     # altering hash so subdomains are ignored.
  92.    # don't do this if you actually run different sites on different subdomains
  93.    if ( req.http.host ) {
  94.         set req.hash += regsub( req.http.host, "^([^\.]+\.)+([a-z]+)$", "\1\2" );
  95.     } else {
  96.         set req.hash += server.ip;
  97.     }
  98.     # ensure separate cache for mobile clients (WPTouch workaround)
  99.    if( req.http.User-Agent ~ "(iPod|iPhone|incognito|webmate|dream|CUPCAKE|WebOS|blackberry9\d\d\d)" ){
  100.         set req.hash += "touch";
  101.     }
  102.     return (hash);
  103. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement