Caching POSTs

Laurence Rowe l at lrowe.co.uk
Sun Nov 8 00:44:17 CET 2009


2009/11/6 Rob Ayres <quasirob at googlemail.com>:
> 2009/11/6 Tollef Fog Heen <tfheen at redpill-linpro.com>
>>
>> ]] Rob Ayres
>>
>> | I want to cache POSTs but can't get varnish to do it, is it possible? If
>> it
>> | makes it any easier, all requests through this cache will be of POST
>> type.
>>
>> No, you can't cache POSTs.  It doesn't make any sense to do so.
>>
> We have a processing server and a database server. The processing server
> makes its requests to the database server by means of a POST. There is
> enough duplication in the POST requests to have made it worth having a
> caching server between the two.

As Varnish does not inspect post data, it is impossible to create the
query string for an 'equivalent' GET request (presumably there is
something important in the post data distinguishing these requests.)

If it is the rendering of the POST response page which is time
consuming, you might return an 'X-Accel-Redirect' header pointing to
the result page and run Nginx in front of Varnish.

You might be able to achieve the functionality completely within
Varnish with something like the following (untested), but the
transformed POST to GET request may still have it's postdata hanging
around to confuse things.

sub vcl_recv {
  if(req.restarts == 0) {
     remove req.http.X-Accel-Redirect; # guard against improper use
   } elseif (req.http.X-Accel-Redirect) {
     set req.url = req.http.X-Accel-Redirect;
     set req.request = "GET";
   }
}

sub vcl_fetch {
  if (obj.http.X-Accel-Redirect) {
      set req.http.X-Accel-Redirect = obj.http.X-Accel-Redirect;
      restart;
  }
}

Laurence



More information about the varnish-misc mailing list