15

Given the sample location example below, what does -1 mean for expires? Does that mean "never expires" or "never caches"?

# cache.appcache, your document html and data
location ~* \.(?:manifest|appcache|html?|xml|json)$ {
  expires -1;
  access_log logs/static.log;
}

https://github.com/h5bp/server-configs-nginx/blob/b935688c2b/h5bp/location/expires.conf

Jmini
  • 9,189
  • 2
  • 55
  • 77
Joshua Powell
  • 894
  • 1
  • 9
  • 13

3 Answers3

23

According to nginx manual, this directive adds the Expires and Cache-Control HTTP header to the response.

Value -1 means these headers are set as:

Expires: current time minus 1 second

Cache-Control: no-cache

So in summary it instructs the browser not to cache the document.

Community
  • 1
  • 1
Marki555
  • 6,434
  • 3
  • 37
  • 59
8

If expires -1 is used, it means that these pages are never cached. The expire directive instructs the browser to expire file cache after a certain amount of time (or at a certain time). If a negative value is given, there is no caching.

steve klein
  • 2,566
  • 1
  • 14
  • 27
  • 1
    That is not true, it has nothing to do with caching in nginx itself. It just adds a header for the browser - http://nginx.org/en/docs/http/ngx_http_headers_module.html#expires – Marki555 Jun 18 '15 at 19:37
  • Correct it is browser caching - my mistake. I've updated my answer. I am not sure what your separate answer adds though - essentially setting expire to a negative value means there is no caching. – steve klein Jun 18 '15 at 21:49
0

Neither existing answer is strictly true. Setting expires: -1; does indeed make nginx add Cache-Control: no-cache HTTP header. This does not disable caching though. Instead, this makes any cached version "expire" immediately, requiring client (browser) to re-validate cache (if any) each time.

E.g., if client has cached version of resource, it is obligated to verify by doing a request to server, which can include If-None-Match and/or If-Modified-Since headers, in which case, server can reply with 304 Not Modified if the cached resource is still the latest.

JohnLM
  • 195
  • 3
  • 8