How to cache req.body and then use it in response for coming requests in varnish

Guillaume Quintard guillaume at
Mon Aug 12 06:03:45 UTC 2019


There are a few things to do here:
- make sure that if req.method == "POST", you return(hash) from vcl_recv
(otherwise, the builtin.vcl (
will pass)
- but before that, add 'set req.http.x-method = req.method' at the top of
vcl_recv, and 'if (bereq.http.x-method == "POST") {set bereq.method =
"POST"; }' at the top of vcl_backend_fetch, because for cacheable request,
varnish will want to set the method to "GET", so we need to reset it back

On top of that:
- you can use req.hash_always_miss (man vcl) to push new content (and avoid
just getting the cached response)
- make sure you only cache when the response is OK, otherwise a wrong POST
request will mask the previous valid ones
- if you are acting upon the URL of the resource itself, I'd say it should
be a PUT/PATCH, rather than a POST, but that's just pedantry at this point
- realize that the content cached is the response returned by the backend,
not the body of the request itself. They may be the same, but maybe not
(there are ways to do it though)

Hope that helps
Guillaume Quintard

On Sun, Aug 11, 2019 at 10:42 PM Yuan Zafir <eyz12315 at> wrote:

> Hi,I am trying to configure varnish as a simple cache service, which
> means the content posted by clients could be cached as an object. The
> content chould be returned in responses for the coming GET requests(with
> same url...). I am not sure wether this can be realized by VCL
> configuration.
> Any recommendation for this situation? Thanks for any help!
> I was noticed vmod-bodyaccess can cache req.body and then it could be used
> in bereq(
> but I do not know how to put it into responses for clients.
> thanks!
> Zafir
> ------------------------------
> eyz12315 at
> _______________________________________________
> varnish-misc mailing list
> varnish-misc at
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <>

More information about the varnish-misc mailing list