Backend selection based on integer value in request?

Ross Brown ross at trademe.co.nz
Wed Oct 10 04:45:54 CEST 2012


-----Original Message-----
From: Paul A. Procacci [mailto:pprocacci at datapipe.com] 
Sent: Wednesday, 10 October 2012 2:43 p.m.
To: Ross Brown
Cc: varnish-misc at varnish-cache.org
Subject: Re: Backend selection based on integer value in request?

On Tue, Oct 09, 2012 at 07:58:40PM -0500, Paul A. Procacci wrote:
> You probably want something like the following (untested):
>
> ######################################
> if(req.url ~ "/\d+\.jpg$"){
>   set req.http.X-Image-Int = req.url;
>   regsub( req.http.X-Image-Int, "/(\d+)\.jpg$", "\1");
>   if(std.integer(req.http.X-Image-Int, 0) > 10) {
>     set req.backend = foo;
>   }
>   elseif(std.integer(req.http.X-Image-Int, 0) > 5) {
>     set req.backend = foo2;
>   }
>   remove req.http.X-Image-Int;
> }
> ######################################
>
> ~Paul

Thanks Paul,  that got me on the right track.

// Set header containing image ID
   if (req.url ~ "^/photoserver/thumb/") {
      set req.http.X-Image-ID = regsub(req.url, "^/photoserver/thumb/(\d+)\.jpg$", "\1");
      std.log("ImageID: " + req.http.X-Image-ID);
   }

Then in my backend selection  -

...
   } else if (req.url ~ "^/photoserver/thumb") {
      if (std.integer(req.http.X-Image-ID, 0) > 1000) { // UPDATE WITH REAL VALUE
         set req.backend = b-new;
         std.log("Using new backend based on image ID: " + req.http.X-Image-ID);
      } else {
         set req.backend = b-old;
         std.log("Using old backend based on image ID: " + req.http.X-Image-ID);
      }

Not in Production yet, but it works like a charm in my dev environment :)



More information about the varnish-misc mailing list