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

António P. P. Almeida appa at perusio.net
Fri Feb 10 15:05:57 UTC 2012


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;
>
> 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;
> }
>
> 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;
> }
>
> This generic approach is based on the assumptions that the
> content on the backend is posted/modified through the same
> frontend that is proxying and caching it, and that you know
> how to prevent session-specific information from being leaked
> through the cache.
>
> You've been going on about this for two days now, but you still
> haven't managed to explain HOW you check whether the
> content has changed. It's obvious you do know the content
> has changed, but without sharing the details on how and
> where the content on the backend is updated, you can't
> expect a more specific solution.
>

Why all this? The default behavior is for POSTed requests never to be
cached.

http://wiki.nginx.org/HttpProxyModule#proxy_cache_methods

http://wiki.nginx.org/HttpFcgiModule#fastcgi_cache_methods

BTW: Valentin, Maxim, Igor, Andrei, et al

The proxy_cache_methods and fastcgi_cache_methods are missing on the
official docs at http://nginx.org/en/docs/http/ngx_http_proxy_module.html
and http://nginx.org/en/docs/http/ngx_http_fastcgi_module.html
respectively.

--- appa



More information about the nginx mailing list