Set ttl etc. explicitly using a request header

Comments

5 comments

  • Austin Spires

    If you're already using a proxy to contact a service you don't control, intercepting and rewriting the Surrogate-Control headers leaving the proxy would be my honest recommendation. Of course, I'd recommend OSS Varnish as the proxy system :slight_smile:

  • Rogier Mulhuijzen

    Strictly speaking within VCL, your proposed solution doesn't work straight up. This is because Surrogate-Control is evaluated before vcl_fetch is run.

    However, you can set the TTL based on any header, using a couple of functions. It would look something like:

    if (req.http.Cache-Control ~ "max-age=([0-9]+)") { set beresp.ttl = std.integer2time(std.atoi(re.group.1)); }

    Note, I didn't test this code, but I think it should work.

  • wheresrhys

    thanks @drwilco. Your solution was what I first thought of doing but couldn't find any info on converting strings to times, hence considering using surrogate-control directly. I'll try your solution now

  • wheresrhys

    For reference, here is a working solution https://github.com/Financial-Times/keen-proxy/pull/1

    One thing that held me up for ages - and I'm not sure where this is/should be documented - but req.http.Cache-Control could not be read within vcl_fetch, hence the proprietary Cache-Strategy header

  • Rogier Mulhuijzen

    Were you trying to read Cache-Control in the request or in the origin response?

Please sign in to leave a comment.