Question

Depending on the request uri I want to forward the requests with varnish to different servers using data from a mysql database. The database contains 2 fields: name and base_url. If the request goes to /forwards/%name% I want to forward the request to the server/backend defined by base_url. Me first try was to set an existing backend with VRT_SetHdr, but this does not work:

VRT_SetHdr(sp, HDR_REQ, "\010backend:", my_backend, vrt_magic_string_end);

Maybe it's possible to set a header like X-backend and then do the selection with ordinary VCL?

I also want to define the backends on runtime using the same data from mysql. Is this possible anyway?

Was it helpful?

Solution

As you say, you can switch backends in VCL if a header is set, provided that every backends are pre-declared in VCL:

vcl_recv {
  # ...
  if ( req.http.X-backend ) {
    set req.backend = req.http.X-backend;
  }
  # ...
}

EDIT: As @Bhaskar has pointed in his comment, an additional if is needed for each backend due to varnish structure assignment restrictions. Something like:

vcl_recv {
  # ...
  if ( req.http.X-backend ) {
    if ( req.http.X-backend == "predefined" ) {
      set req.backend = "predefined";
    }
  }
  # ...
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top