Ordinarily, you will not want to cache POST
requests. RFC 2616 does allow, though, for the idea that responses to a POST
may sometimes be cacheable.
Generally speaking, whether you consider POST
cacheable will depend on what happens on the application back-end in response to that POST
- if something is changed as a result then you'll almost certainly not want to cache, whereas if POST
is being used to submit (say) search criteria, then you may be happy caching it.
My usecase was the one described above - on a search page, the search query is submitted via AJAX using POST - this means that if you navigate off the page and then click back, you need to wait for the search to run again. A short cache life on the request helps mitigate that.
In order to cache POST
requests in NGinx you need to do two things
- Tell Nginx to consider the method
POST
cacheable
- Include the request body (i.e. the data submitted via
POST
in the cache key
The latter of these is important - it means you'll have a different cached entity for different submissions. However, you should be aware that POST
bodies can be quite large, so calculating the cache key for these could be quite expensive (i.e. there's a potential DoS vector here)
Read more…