3

I use varnish with docker - see million12/varnish

GET requests work great !

but i have no idea what i have to set in the settings to cache POST requests.

on google i have found many posts (from 2010 or 2011) where it says that POST requests can not be cached with varnish - is this statement still correct?

Or is there another way to cache POST requests?

here my varnish.vcl settings:

vcl 4.0;
backend default {
    ...
}

# Respond to incoming requests.
sub vcl_recv {
  unset req.http.Cookie;
}

# Set a header to track a cache HIT/MISS.
sub vcl_deliver {
  if (obj.hits > 0) {
    set resp.http.X-Varnish-Cache = "HIT";
  }
  else {
    set resp.http.X-Varnish-Cache = "MISS";
  }
}

# Not cache 400 - 500 status requests
sub vcl_backend_response {
    if (beresp.status >= 400 && beresp.status <= 600) {
        set beresp.ttl = 0s;
  }
}

Thanks for help !

user1199255
  • 51
  • 1
  • 4
  • Why do you want to cache Post requests? I think is conceptually wrong. Take a look at [this](http://stackoverflow.com/questions/626057/is-it-possible-to-cache-post-methods-in-http) – Redithion Jul 21 '15 at 19:02
  • I calculate big things with an internal API in our company. There are almost the same POST requests. And thats why I am looking for a solution to cache POST requests for a better performance – user1199255 Jul 22 '15 at 06:57
  • Wouldn't it be better if you check that in your backend? – Redithion Jul 22 '15 at 14:32
  • Why are those POST requests? Are they modifying any state on the server? – Jakub Zalas Jul 22 '15 at 18:47
  • @Redithion Yes it would be better .. but i have no chance to change that.. my boss don't want to change the Internal API .. yeah stupid i know. Thats why i need an example to cache POST and GET requests with a Docker Container. It does not have to be made with Varnish. If you have an other example - i would also be happy ! – user1199255 Jul 24 '15 at 07:04
  • You may want to check [this](https://medium.com/programming-articles/caching-post-responses-with-nginx-1c0c064bb6b0) – Redithion Aug 04 '15 at 15:26

3 Answers3

6

Edit Dec. 2020:

There is an updated tutorial for this here: https://docs.varnish-software.com/tutorials/caching-post-requests/


There is a Varnish module and tutorial for caching POST requests. This adds the ability to add the post body to the hash key and to pass along the POST request.

The VMOD is available for Varnish 4 releases and includes the following functions:

buffer_req_body(BYTES size): buffers the request body if it is smaller
    than size. This function is a “better” (bug-free**) copy of
    std.CacheReqBody(), so please use this one instead of the one provided
    by the libvmod-std. Please note that retrieving req.body makes it
    possible to retry pass operations(POST, PUT).

len_req_body(): returns the request body length.

rematch_req_body(STRING re): regular expression match on request body.

hash_req_body(): adds body bytes to the input hash key.

https://info.varnish-software.com/blog/caching-post-requests-with-varnish https://github.com/aondio/libvmod-bodyaccess

Note that in the 4.1 branch of this VMOD, the built in std.cache_req_body() is used instead of buffer_req_body(), but a standing bug in Varnish 4.1 breaks the 4.1 branch. https://github.com/varnishcache/varnish-cache/issues/1927

Laizer
  • 5,932
  • 7
  • 46
  • 73
5

Currently Varnish can't cache POST requests.

AFAIK people's tries to cache POST request have failed. It seems that Varnish ends up converting those into GET requests.

Sources:

Redithion
  • 986
  • 1
  • 19
  • 32
0
  • put another ngnx/apache/whatever on an unused port
  • push the POST request to that server
  • over there you forward them to the varnish as a get request and fetch the result
  • display the result via your relay-server

will probably slow down the whole thing a bit - but we are talking dirty workarounds here right? Hope this is not a way too crazy solution..

Hafenkranich
  • 1,696
  • 18
  • 32