7

I understand that varnish caches "GET" and "HEAD" requests by default.

My backend servers fail when I do a get request that is too long, so I made them respond to POST instead of GET. This works great, but I need a reverse proxy which can be configured to cache POST responses just like GET.

Are there any reverse proxies that can cache these post requests?

nurettin
  • 378
  • 2
  • 10
  • I'm curious why the servers do not fail if you use POST in stead of GET ... If it's because of Varnish' timeout, you can bump the first_byte_timeout and between_bytes_timeout to something that exceed the expected processing time somewhat. It's probably better than tinkering with caching POST requests. – Kvisle Dec 22 '12 at 00:02
  • Well it has been 10+ years, but the question got attention so I will just respond for reference. It was because I was sending large paragraphs to a text-to-speech engine and browsers at the time placed arbitrary limits to get requests. So I had to change from get to post. But that caused too much work on the backend because people were requesting the same text over and over again so I was looking for a cache solution and nginx worked fine. But apparently this was "bad design because it doesn't comply with REST" or whatever. – nurettin Jan 13 '23 at 09:25

2 Answers2

15

It seems nginx does cache POST requests if you specify it.

proxy_cache_methods POST; # GET HEAD
proxy_cache_key "$uri|$request_body";
client_max_body_size 10k; # 413
nurettin
  • 378
  • 2
  • 10
4

Reverse proxies cache responses, not requests. In any case, it's not realistic to cache a response to a POST request. The HTTP specification really doesn't allow for it, since the very act of sending a POST request is supposed to invalidate any cache for that URL, and the response is non-cacheable by default. You are supposed to send a redirect as a response to the POST request if you want the response cached.

Michael Hampton
  • 244,070
  • 43
  • 506
  • 972
  • 5
    I wasn't looking for HTTP compliance, just trying to get my back-end servers working. Users won't know about the black magic that goes on inside. – nurettin Dec 22 '12 at 07:23
  • 1
    Some web services such as SOAP use POST requests for everything. It is reasonable to cache POST responses as long as you know what you're doing. – Kris Peeling Jun 17 '19 at 15:19
  • @KrisPeeling OK, then, add _poorly designed protocols notwithstanding_... – Michael Hampton Jun 17 '19 at 17:24