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

Guillaume Quintard guillaume at varnish-software.com
Mon Aug 12 06:03:45 UTC 2019


Hi,

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 (
https://github.com/varnish/Varnish-Cache/blob/4.1/bin/varnishd/builtin.vcl#L63)
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 live.cn> 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(
> https://info.varnish-software.com/blog/caching-post-requests-with-varnish),
> but I do not know how to put it into responses for clients.
>
> thanks!
> Zafir
> ------------------------------
> eyz12315 at live.cn
> _______________________________________________
> varnish-misc mailing list
> varnish-misc at varnish-cache.org
> https://www.varnish-cache.org/lists/mailman/listinfo/varnish-misc
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://www.varnish-cache.org/lists/pipermail/varnish-misc/attachments/20190811/327ca883/attachment.html>


More information about the varnish-misc mailing list