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?

有帮助吗?

解决方案

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";
    }
  }
  # ...
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top