Search code examples
varnishvarnish-vcl

How to instruct varnish to generate the cache key based on response header data


I need to do cache key generation/store based on the response header received from the backend not based on the URL requested from the client-side.

The main reason for doing so is: I have a backend logic to reply to the client with some different data if requested things are not available.

Ex: Request: example.com/foo/102030?names=test1 now, my backend checks for is test1 is present for 102030, if not it checks whether 102030 has a special tag = Y : basically tells that have to look for some other matching object.

so, in that case, backed reply's to clients with data again, which can be accessible with example.com/foo/000000?names=test1.

So, now the problem is if some other request comes with example.com/foo/000000?names=test1, varnish considers this as a different request based on URL but in actuality, I need to serve the same data which is already present in Cache with example.com/foo/000000?names=test1.

Currently, I do Ban using some regex syntax from the backend, so in that case, I can easily invalidate the object which stored with /foo/000000?names=test1 not the other one.

So, is there a way through which I can store the cache key based on the response header info?


Solution

  • There's no way you can do this unfortunately. Only request information can be used to create the cache key.

    That is by design, because incoming requests only have their own request properties they can present to Varnish to identify the resource they wish to retrieve.