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

António P. P. Almeida appa at perusio.net
Fri Feb 10 16:42:51 UTC 2012


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:
>>
>>> If you really want to avoid nginx_ngx_cache_purge at all costs,
>>> you'll have to use something like this to force every POST
>>> method request to refresh the cache:
>>>
>>> proxy_cache zone;
>>>
>>> # Responses for "/blog" and "/blog?action=edit" requests
>>> # are cached under the SAME key
>>> proxy_cache_key $scheme$host$uri;
>>>
>>> # 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.

>>> 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?

--- appa



More information about the nginx mailing list