[edit] proxy_cache_valid

Syntax: proxy_cache_valid [ code ...] time
Default:
Context: http
server
location
Reference: proxy_cache_valid


This directive sets the time for caching different replies. Example:

proxy_cache_valid  200 302  10m;
proxy_cache_valid  404      1m;

sets 10 minutes cache time for replies with code 200 and 302, and 1 minute for 404s.

If only time is specified:

proxy_cache_valid 5m;

then only replies with codes 200, 301 and 302 will be cached.

Also it is possible to cache any replies with parameter "any":

proxy_cache_valid  200 302 10m;
proxy_cache_valid  301 1h;
proxy_cache_valid  any 1m;

Upstream cache-related directives have priority over proxy_cache_valid value, in particular the order is (from Igor):

  1. X-Accel-Expires
  2. Expires/Cache-Control
  3. proxy_cache_valid

The order in which your backend return HTTP headers change cache behaviour. Read this post for details.

You may ignore the headers using

proxy_ignore_headers X-Accel-Expires Expires Cache-Control;

Concerning If-Modified / Last-Modified since behaviour, please remember that by default nginx sends 304 only if L-M == I-M-S. Controlled by directive if_modified_since [off|exact|before]

Note: you must set this option for any persistent caching to occur.


Module: HttpProxyModule