[Catalyst-commits] r8869 - in trunk/examples/CatalystAdvent/root: 2008/pen static/2008/txt

jayk at dev.catalyst.perl.org jayk at dev.catalyst.perl.org
Sun Dec 14 08:36:14 GMT 2008


Author: jayk
Date: 2008-12-14 08:36:14 +0000 (Sun, 14 Dec 2008)
New Revision: 8869

Modified:
   trunk/examples/CatalystAdvent/root/2008/pen/varnish_pt1.pod
   trunk/examples/CatalystAdvent/root/static/2008/txt/catalyst_vcl.txt
Log:
Adjustments based on feedback from Varnish folks. Mostly removal / adjustments
to things that have changed since varnish docs were written.

Modified: trunk/examples/CatalystAdvent/root/2008/pen/varnish_pt1.pod
===================================================================
--- trunk/examples/CatalystAdvent/root/2008/pen/varnish_pt1.pod	2008-12-14 07:40:07 UTC (rev 8868)
+++ trunk/examples/CatalystAdvent/root/2008/pen/varnish_pt1.pod	2008-12-14 08:36:14 UTC (rev 8869)
@@ -258,23 +258,15 @@
 Varnish docs to see all the properties that are available. We'll cover the
 ones we use as we encounter them.
 
- remove req.http.X-Forwarded-For;
- set    req.http.X-Forwarded-For = client.ip;
-
-The req.http variable contains the headers supplied with the request. As you
-probably have already guessed, the above lines remove any existing
-X-Forwarded-For headers on the incoming request and add a new one indicating
-the ip of the client that Varnish is actually talking to.
-
  set req.http.host = "mycatalystsite.com";
 
-This sets the host header to pass on to the backend. This is not strictly
-necessary and can cause trouble if you are fronting several sites with a
-single varnish installation. However, without this header, requests to
-I<mycatalystsite.com> and I<www.mycatalystsite.com> would be treated as
-completely separate by Varnish, potentially doubling every entry in your
-cache. If you are fronting only a single site, normalizing the hostname is a
-good idea.
+The req.http variable contains the headers supplied with the request. This
+sets the host header to pass on to the backend. This is not strictly necessary
+and can cause trouble if you are fronting several sites with a single varnish
+installation. However, without this header, requests to I<mycatalystsite.com>
+and I<www.mycatalystsite.com> would be treated as completely separate by
+Varnish, potentially doubling every entry in your cache. If you are fronting
+only a single site, normalizing the hostname is a good idea.
 
  set req.backend = catalystsite;
 
@@ -297,14 +289,15 @@
 
  if (req.request == "GET" && req.url ~ "^/static/") {
      unset req.http.cookie;
+     unset req.http.Authorization
      lookup;
  }
 
 This tells Varnish if the request is for anything that starts with /static
-that it should remove any cookie header and then look up the request in the
-cache. The B<lookup> line is important. This tells Varnish that it should stop
-executing the vcl_recv routine and look up the item in the cache. This is an
-example of a I<keyword>.
+that it should remove any cookie header or authorization information, and then
+look up the request in the cache. The B<lookup> line is important. This tells
+Varnish that it should stop executing the vcl_recv routine and look up the
+item in the cache. This is an example of a I<keyword>.
 
 Keywords in varnish can be though of as a 'return' for the subroutine combined
 with the value it is returning. If you do not use a keyword somewhere to
@@ -464,7 +457,6 @@
 
  if (obj.http.Cache-Control ~ "max-age") {
      unset obj.http.Set-Cookie;
-     unset req.http.cookie;  
      deliver;
  }
  pass;
@@ -491,26 +483,19 @@
 ensures that if we don't explicitly set the 'max-age' property in our app, the
 item will not be cached. 
 
-=head4 A quick side note: vcl_hit
+=head4 A quick side note: Is it a hit?
 
-There is one little piece of our configuration we have not looked at yet. It
-is the vcl_hit subroutine. Vcl_hit is executed when an item is found in the
-cache, but has not yet been returned to the client. Our vcl_hit routine is
-very simple:
+When you are first working with the cache in place, you will at some point
+want to know if a piece of content you are looking at came from the cache or
+from the backend server. Yes, you could go to the backend server, make the
+request and watch the access logs but there is an easier way.
 
- sub vcl_hit {
-     set    obj.http.X-Varnish-Hit = "HIT";
- }
+If you look at the headers returned on the item in question, you will see a
+header called 'X-Varnish.' That header will contain either one or two numbers
+(separated by a space.) If the 'X-Varnish' header contains two numbers, the
+data came from the cache. If it contains only one, it did not come out of the
+cache. Knowing this piece of information can make your debugging much simpler.
 
-What we do here is set a new custom header before we respond to the client.
-That header indicates that this item was in fact found and returned from the
-cache. You may or may not want to run this way in production. Since it is a
-custom header, browsers and other caches/proxies will ignore it, but it
-provides valuable info when you are debugging wierdness on your site. Note
-also the absense of keywords. Since all we want to do is add a header, after
-we have done so, we let control fall through to Varnish's built-in vcl_hit
-routine.
-
 Basically what we have done with this configuration is require an explicit
 cache 'allow' and what it means is that nothing in our application will be
 cached until we say it should be cached. This is, in my opinion, the only safe

Modified: trunk/examples/CatalystAdvent/root/static/2008/txt/catalyst_vcl.txt
===================================================================
--- trunk/examples/CatalystAdvent/root/static/2008/txt/catalyst_vcl.txt	2008-12-14 07:40:07 UTC (rev 8868)
+++ trunk/examples/CatalystAdvent/root/static/2008/txt/catalyst_vcl.txt	2008-12-14 08:36:14 UTC (rev 8869)
@@ -17,13 +17,6 @@
 
 sub vcl_recv {
 
-    ## Add a unique header containing the client address
-    ## This is important if you do any logging / checking based on IP.
-    ## all your traffic will appear to be from the cache server, this
-    ## gives you a header containing the original requester's IP
-    remove req.http.X-Forwarded-For;
-    set    req.http.X-Forwarded-For = client.ip;
-
     ## This sets the host header for your site - you should set this  
     ## because varnish takes host into consideration when caching.
     ## IE - www.foo.com and foo.com will be treated as separate by the cache
@@ -35,10 +28,9 @@
 
 
     ## this is an example of switching betwen backends based on path
-    #if ( req.url ~ "^/cat-test") {
-    #    set req.backend = catsite;
-    #    set req.http.host = "ccb.dev.ionzero.com";
-    #    #set resp.http.X-Backend = "catsite";
+    #if ( req.url ~ "^/static-test") {
+    #    set req.backend = staticsite;
+    #    set req.http.host = "mystaticsite.com";
     #}
 
     ## Force lookup if the request is a no-cache request from the client
@@ -52,6 +44,7 @@
     ## doesn't change much.
     if (req.request == "GET" && req.url ~ "^/static/") {
         unset req.http.cookie;
+        unset req.http.authorization;
         lookup;
     }
 
@@ -60,6 +53,7 @@
     ## if you use this method.
     #if (req.request == "GET" && req.url ~ "\.(gif|jpg|swf|css|js|png|jpg|jpeg|gif|png|tiff|tif|svg|swf|ico|css|js|vsd|doc|ppt|pps|xls|mp3|mp4|m4a|ogg|mov|avi|wmv|sxw|zip|gz|bz2|tgz|tar|rar|odc|odb|odf|odg|odi|odp|ods|odt|sxc|sxd|sxi|sxw|dmg|torrent|deb|msi|iso|rpm)$") {
     #    unset req.http.cookie;
+    #    unset req.http.authorization;
     #    lookup;
     #}
 
@@ -81,7 +75,8 @@
         pass;
     }
 
-    ## if we have an authorization header, we definitely do not want to be 
+    ## if we have an authorization header, we definitely do not want to 
+    ## cache 
     if (req.http.Authorization) {
         # Not cacheable by default #
         pass;
@@ -127,32 +122,16 @@
     if (obj.http.Set-Cookie) {
         pass;
     }
-
-    ## if the object is specifically saying 'don't cache me' 
-    ## obey it.
-    if(obj.http.Pragma ~ "no-cache" ||
-       obj.http.Cache-Control ~ "no-cache" ||
-       obj.http.Cache-Control ~ "private"){
-            pass;
-    }
     
     ## if the object is saying how long to cache it, you 
     ## can rely on the fact that it is cachable. 
     if (obj.http.Cache-Control ~ "max-age") {
         unset obj.http.Set-Cookie;
-        unset req.http.cookie;  
         deliver;
     }
     pass;
 }
 
-sub vcl_hit {
-    
-    ## make it easy to see whether we got a hit on the cache or not by 
-    ## adding a header indicating a hit.
-    set    obj.http.X-Varnish-Hit = "HIT";
-}
-
 ## If you want a specific error response - you can define it here.
 #sub vcl_error {
 #    set obj.http.Content-Type = "text/html; charset=utf-8";




More information about the Catalyst-commits mailing list