If-none-match with proxy_cache : properly set headers

Hi,
i struggled a little to get nginx to cache 304 responses from backend
using
proxy_cache.
What happens when configuring proxy_cache is that 304 responses are not
happening because
nginx strips If-None-Match request headers. It is a workaround to
prevent
the client from getting
an empty response event if he did not send If-None-Match in the header.
A better workaround can be :

proxy_cache_key $http_if_none_match$scheme$proxy_host$request_uri;

So that the cache sends 304 if the header is properly set, and 200 if it
isn’t.
Of course one has to kill the first workaround :

proxy_set_header If-None-Match $http_if_none_match;

and cache both responses :

proxy_cache_valid 200 304 1h;

Comments welcome.

Posted at Nginx Forum:

Hello!

On Thu, May 30, 2013 at 06:41:50AM -0400, kapouer wrote:

proxy_cache_key $http_if_none_match$scheme$proxy_host$request_uri;

Comments welcome.

Normally you shouldn’t cache 304 responses from a backend, but
rather cache 200 responses from a backend and let nginx to return
304 by its own. This is how it works by default.

Do you have problems with the default aproach?


Maxim D.
http://nginx.org/en/donation.html

Default approach doesn’t work for me.

Posted at Nginx Forum:

Maxim D. Wrote:


nginx mailing list
[email protected]
nginx Info Page

Yes I do have some problems with this approach. In our nginx config we
use
also option “fastcgi_cache_min_uses 5”. So response body appears in
cache
only after 5 user requests. But for our production purposes it is
extremely
important to return 304 if client response has valid If-None-Match.


Dmitry Sukhov
[email protected]

Posted at Nginx Forum:

It is good to have $request_method in the cache key since HEAD and GET
methods may share same hash for content they point to

Posted at Nginx Forum:

Hello!

On Wed, Jul 16, 2014 at 02:14:54AM -0400, quoter wrote:

only after 5 user requests. But for our production purposes it is extremely
important to return 304 if client response has valid If-None-Match.

Yes, …_cache_min_uses can lead to suboptimal behaviour in such a
case. It is planned to improve things to just pass If-* headers
if caching was disabled due to …_cache_min_uses. No ETA though.


Maxim D.
http://nginx.org/