2

My nginx server acts as a proxy for another server, which sometimes returns 404s.
I want the 404 responses to be cacheable, just like 200 responses.

However, I cannot use expires, because the documentation states that:

expires works only for 200, 204, 301, 302, and 304 responses.

Note that caching of non-200 responses is perfectly possible according to the HTTP spec.

So how can I set an expiration date on 404 responses?

Ruben Verborgh
  • 171
  • 1
  • 10

1 Answers1

1

I see two options:

  1. Modify another server to provide caching headers.

  2. Use headers_more module. It allows add headers to error http responses, but you'll have to recompile nginx.

  3. Use Lua module.


Original answer

It's impossible to cache error pages on client (browser)

Probably you want to cache response from another server and it possible with proxy_cache directive. Also take a look at proxy_cache_valid directive.

Alexey Ten
  • 7,922
  • 31
  • 35
  • Why would caching error pages on the client be impossible? For instance, it would totally make sense to cache a 410 GONE response. – Ruben Verborgh Mar 06 '14 at 21:37
  • OK, you convinced me. I've changed my mind and the answer. – Alexey Ten Mar 07 '14 at 10:54
  • @RubenVerborgh 410 _is_ cacheable by default; 404 _is not_. – Michael Hampton Mar 07 '14 at 11:11
  • @MichaelHampton Where does it say that 410 is cacheable and 404 not? Note that `expires` (nginx-specific) only works for 200, 204, 301, 302, 304. – Ruben Verborgh Mar 07 '14 at 14:38
  • @alexeyten Thanks, but does `headers_more` allow me to do the date arithmetic and if so, how? – Ruben Verborgh Mar 07 '14 at 14:38
  • @RubenVerborgh [RFC 2616 section 13.4.](http://tools.ietf.org/html/rfc2616#section-13.4) – Michael Hampton Mar 07 '14 at 14:43
  • @MichaelHampton The RFC says "A response received with a status code of 200, 203, 206, 300, 301 or 410 MAY be stored by a cache and used in reply to a subsequent request” but does not say that 404 is not cacheable by default. (But even if it did, that's what I want to do: I want to cache the 404 responses of this server.) – Ruben Verborgh Mar 07 '14 at 14:59
  • @RubenVerborgh Um, yes it does, in the very next paragraph. – Michael Hampton Mar 07 '14 at 15:57
  • @RubenVerborgh, unfortunately, no. See [limitations](http://wiki.nginx.org/HttpHeadersMoreModule#Limitations). – Alexey Ten Mar 07 '14 at 16:38
  • @RubenVerborgh, updated post. You could use Lua module and have all the power of lua to calculate dates. – Alexey Ten Mar 07 '14 at 16:48
  • @MichaelHampton Next paragraph: “A response received with any other status code (e.g. status codes 302 and 307) MUST NOT be returned in a reply to a subsequent request unless there are cache-control directives or another header(s) that explicitly allow it.” So it is cacheable. – Ruben Verborgh Mar 07 '14 at 18:43
  • @alexeyten Thanks but I'd need a more specific answer. It currently explains different ways to add headers, not how to set Expired on a 404 response. – Ruben Verborgh Mar 07 '14 at 18:44
  • 1
    @RubenVerborgh I said it was not cacheable _by default_, which is still true. – Michael Hampton Mar 07 '14 at 18:48