Old thread: Cache for non-cookie users and fresh for cookie users

Max nginxyz at mail.ru
Fri Feb 10 17:47:49 UTC 2012

10 февраля 2012, 20:43 от António P. P. Almeida <appa at perusio.net>:
> On 10 Fev 2012 15h41 WET, nginxyz at mail.ru wrote:
> >
> > 10 февраля 2012, 19:06 от António P. P. Almeida <appa at perusio.net>:
> >> On 10 Fev 2012 14h45 WET, nginxyz at mail.ru wrote:
> >>> # Turn caching on for POST method request responses as well
> >>> proxy_cache_methods GET HEAD POST;
> You're saying the POST is a valid method for caching, i.e., POSTed
> requests get cached.

The proxy_cache_methods directive is used to select the request methods
that will have their responses cached. By default, nginx caches only
GET and HEAD request method responses, which is why I added the
POST request method to the list - so that POST method request responses
would be cached as well. This is necessary if you want to make sure the
cache is instantly refreshed whenever you update something through a
POST method request. Without this directive here, you would update
the content on the backend, but the frontend would hold and serve
the stale content from the cache until its validity expired instead
of the new, updated content that's on the backend.

> >>> location / {
> >>> recursive_error_pages on;
> >>> error_page 409 = @post_and_refresh_cache;
> >>>
> >>> # Redirect POST method requests to @post_and_refresh_cache
> >>> if ($request_method = POST) { return 409; }
> >>>
> >>> # Process GET and HEAD method requests by first checking
> >>> # for a match in the cache, and if that fails, by passing
> >>> # the request to the backend
> >>> proxy_pass $scheme://backend;
> >>> }
> Here you do an internal redirect to @post_and_refresh_cache via
> error_page when the request method is a POST.

> >>> location @post_and_refresh_cache {
> >>>
> >>> proxy_cache_bypass "Never check the cache!";
> >>>
> >>> # Pass the POST method request directly to the backend
> >>> # and store the response in the cache
> >>> proxy_pass $scheme://backend;
> >>> }
> Here you bypass the cache and proxy_pass the request to a backend.

> AFAICT you're replicating the *default* behaviour which is to not
> cache in the case of POST requests. Is it not?

The default behaviour is not to cache POST method request responses,
but I turned caching of POST method request responses ON, so I had
to make sure the cache is bypassed for POST method requests (but
not for GET or HEAD method requests!). All POST method requests
are passed on to the backend without checking for a match in the
cache, but - CONTRARY to the default behavior - all POST method
request responses are cached.

Without the @post_and_refresh_cache location block and without
the proxy_cache_bypass directive, nginx would check the cache
and return the content from the cache (put there by a previous
GET request response, for example) and would not pass the POST
method request on to the backend, which is definitely not what
you want in this case.


More information about the nginx mailing list