Get origin server as variable


#1

I have several origin servers, and some conditions to select some. Now I’d need to know which origin server served my request, but cannot find VCL variable for this. Based on description req.backend seemed to be promising, but when I put this to response header then I see something like cluster_dir_chash_stockholm_bma, which seems to be similar to X-Served-By:cache-bma7029-BMA and is therefore useless. I’d need to get name of my origin server, something like “F_MyServer1” or IP of it.


#2

I remember running into the same issue a while ago. I ended up putting the following in vcl_fetch:

set beresp.http.X-Backend-Name = beresp.backend.name;

This will automatically become a response header visible to the client.


#3

To add to the advice, you can unset that variable in vcl_deliver with the following custom vcl:

sub vcl_deliver {
#FASTLY deliver

  if (!req.http.Fastly-FF) {
    unset resp.http.X-Backend-Name;
  }

  return(deliver);
}

You can also capture the variable in vcl_fetch as a request header and use in logging:

``
set req.http.X-Backend-Name = beresp.backend.name;


Here is the help guide with details around [Tracking your origin's name, ip, and port](https://docs.fastly.com/guides/performance-tuning/tracking-your-origins-name-ip-and-port).

#4

Thanks, it worked finally also without custom VCL, just in headers section. I tried to put it to Response header, but this gave error (beresp not available), the trick was to put it to the Cache header, and it seems this goes to the Response also. It is not completely clear from the docs what is “Cache header” exactly.