# Setting headers for negative caching

**URL:** <https://www.ruby-forum.com/t/setting-headers-for-negative-caching/190819>\
**Category:** NGINX\
**Created:** [June 28, 2010, 6:55pm UTC](https://www.ruby-forum.com/t/setting-headers-for-negative-caching/190819 "2010-06-28T18:55:00Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![bkirkbri](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/b19c9b/32.png) [@bkirkbri](https://www.ruby-forum.com/u/bkirkbri)\
**Post date:** [June 28, 2010, 6:55pm UTC](https://www.ruby-forum.com/t/setting-headers-for-negative-caching/190819/1 "2010-06-28T18:55:00Z")

</div>

Is it possible to set Cache-Control / Expires headers for 404 responses  
in Nginx?

I’ve tried using an error\_page like this:

```auto
location ^~ /x/ {
    error_page 404 /not_found.html;
}
location /not_found.html {
    internal;

    root /static/errordoc;
    expires 10m;
    add_header Cache-Control "private, max-age=600";
}
```

But no luck.

Thanks in advance!

Posted at Nginx Forum:

> **[Setting headers for negative caching](https://forum.nginx.org/read.php?2,103267,103267#msg-103267)**
>
> Is it possible to set Cache-Control / Expires headers for 404 responses in Nginx? I've tried using an error\_page like this: \[code\] location ^~ /x/ { error\_page 404 /not\_found.html; } location /not\_found.html { internal;

---

<div class="post-metadata">

**Author:** ![bkirkbri](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/b19c9b/32.png) [@bkirkbri](https://www.ruby-forum.com/u/bkirkbri)\
**Post date:** [June 30, 2010, 3:54pm UTC](https://www.ruby-forum.com/t/setting-headers-for-negative-caching/190819/2 "2010-06-30T15:54:46Z")

</div>

On Mon, Jun 28, 2010 at 6:54 PM, bkirkbri [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Is it possible to set Cache-Control / Expires headers for 404 responses  
> in Nginx?

I believe that even if you do, user agents and intermediate caches /  
proxies may decide not to honour them, so it may be pointless.  
Regards

---

<div class="post-metadata">

**Author:** ![bkirkbri](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/b19c9b/32.png) [@bkirkbri](https://www.ruby-forum.com/u/bkirkbri)\
**Post date:** [June 30, 2010, 5:46pm UTC](https://www.ruby-forum.com/t/setting-headers-for-negative-caching/190819/3 "2010-06-30T17:46:02Z")

</div>

## Luca De Marinis Wrote:

> Regards  
> That’s true in some cases, definitely. I’d be interested in a list of  
> which browsers respect Cache-Control for non-200 repsonses…

But we might throw a reverse proxy cache in front of nginx, which would  
respect those headers and take load off the nginx machine. Check out  
[http://degizmo.com/2010/03/25/why-you-should-upstream-cache-your-404s/](http://degizmo.com/2010/03/25/why-you-should-upstream-cache-your-404s/)

Best,  
Brian

Posted at Nginx Forum:  
[http://forum.nginx.org/read.php?2,103267,103933#msg-103933](http://forum.nginx.org/read.php?2,103267,103933#msg-103933)

---

<div class="post-metadata">

**Author:** ![bkirkbri](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/b19c9b/32.png) [@bkirkbri](https://www.ruby-forum.com/u/bkirkbri)\
**Post date:** [June 30, 2010, 6:53pm UTC](https://www.ruby-forum.com/t/setting-headers-for-negative-caching/190819/4 "2010-06-30T18:53:22Z")

</div>

On Wed, Jun 30, 2010 at 5:45 PM, bkirkbri [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> respect those headers and take load off the nginx machine. Â Check out  
> [http://degizmo.com/2010/03/25/why-you-should-upstream-cache-your-404s/](http://degizmo.com/2010/03/25/why-you-should-upstream-cache-your-404s/)

Interesting point, but I believe this is violating standards for  
performance (or maybe not, I don’t know what the http rfc says about  
it), so I’d personally do it only for very good reasons; at our sites  
we don’t get that many 404s so not caching them for us is preferable.  
Anyway if plan on using a reverse proxy it all changes because then  
you have fine control on when to purge a certain url, which obviously  
you don’t have when your headers say “I can be saved by anything in  
between for 20 minutes”. Even then, when I had to instruct a reverse  
proxy, I found it more convenient to use a custom header rather than  
munging cache-control, but my scenario was a bit different (I usually  
want caching to happen on my proxy, unless some conditions are met,  
and never want intermediate caches or UA’s to presume they can cache  
my dynamic content).

Bye

---

<div class="post-metadata">

**Author:** ![bkirkbri](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/b19c9b/32.png) [@bkirkbri](https://www.ruby-forum.com/u/bkirkbri)\
**Post date:** [June 30, 2010, 7:14pm UTC](https://www.ruby-forum.com/t/setting-headers-for-negative-caching/190819/5 "2010-06-30T19:14:27Z")

</div>

## Luca De Marinis Wrote:

> interested in a list of
> 
> you have fine control on when to purge a certain  
> conditions are met,  
> and never want intermediate caches or UA’s to  
> presume they can cache  
> my dynamic content).
> 
> Bye

All good advice, thanks.

For what it’s worth, the HTTP spec does allow caching of 404, 302, 301,  
etc. responses if the Cache-Control header is set explicitly by the  
origin. The spec forbids any caching of these responses without a  
Cache-Control header though, which is in contrast to the allowed  
behavior of caching 200 responses that do not have Cache-Control headers  
for some reasonable amount of time.

Posted at Nginx Forum:  
[http://forum.nginx.org/read.php?2,103267,103983#msg-103983](http://forum.nginx.org/read.php?2,103267,103983#msg-103983)
