简体   繁体   中英

Fastly/Varnish response headers based on data center and cache status

I am trying to create a Varnish response header which would help me debug the state of the caches. I would like to create a header which would display the datacenters the request went through and age of the object. Something like:

X-Fastly-Age: VIE:2311s FRA:0s

Which would mean the object was cached in VIE data center (shield) and in the FRA edge it had to be fetched from VIE .

The second request would return something like:

X-Fastly-Age: VIE:2311s FRA:133s

I tried to set this:

set beresp.http.X-Fastly-Age = beresp.http.X-Fastly-Age " " req.http.edge-geo-datacenter ":" obj.entered;

This would work, but the problem is that:

  • beresp.http cannot be written in vcl_hit
  • obj.entered is not available in vcl_fetch

So basically it seems I don't have a place where to generate this header.

How can this be achieved?

EDIT:

I managed to run this in vcl_deliver :

set resp.http.X-Fastly-Age = resp.http.X-Fastly-Age " " server.datacenter ":" obj.entered;

getting:

> x-fastly-age: (null) DCA:0.001 FRA:0.001

It suffers from two things:

  1. Getting (null) in the first call - is there a way to not prepend the header if it is null?
  2. The edge time (FRA) never changes and stays on 0.001 . So it seems it is cached even with the obj.entered and never changes?

I'm going to approach this purely from a Varnish point of view, since I have no experience with Fastly.

Varnish uses an Age header, which it sets automatically, to determine the age of an object in cache.

For uncached objects, the value of the Age response header is always zero. Non-zero values represent the age of an object accordingly.

And for the null values you're getting: we can use an if/else statement to solve that problem.

Here's some VCL for you:

sub vcl_deliver {
    if(resp.http.X-Fastly-Age) {
        set resp.http.X-Fastly-Age = resp.http.X-Fastly-Age + " " + server.datacenter + ":" + resp.http.Age;
    } else {
        set resp.http.X-Fastly-Age = server.datacenter + ":" + resp.http.Age;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM