Backend selection based on integer value in request?

Paul A. Procacci pprocacci at datapipe.com
Wed Oct 10 08:08:41 CEST 2012


> 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 :)

Glad that was helpful.  Just be careful to ensure your req.url matches
what you expect it to match.

For instance:

'/photoserver/thumb/test.jpg'

The above matches your regex.  You probably want to refine your regex to
the following:

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

...

} else if (req.url ~ "^/photoserver/thumb/(?:\d+)\.jpg$") {
  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);
  }
}

Naturally, you know your application best, so the above may not actually matter,
but I figured I would throw it out there just in case.

~Paul

________________________________

This message may contain confidential or privileged information. If you are not the intended recipient, please advise us immediately and delete this message. See http://www.datapipe.com/legal/email_disclaimer/ for further information on confidentiality and the risks of non-secure electronic communication. If you cannot access these links, please notify us by reply message and we will send the contents to you.



More information about the varnish-misc mailing list