# Why the status code restriction on add\_header?

**URL:** <https://www.ruby-forum.com/t/why-the-status-code-restriction-on-add-header/237902>\
**Category:** NGINX\
**Created:** [December 5, 2013, 11:55am UTC](https://www.ruby-forum.com/t/why-the-status-code-restriction-on-add-header/237902 "2013-12-05T11:55:22Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![musicdenotation](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ea666f/32.png) [@musicdenotation](https://www.ruby-forum.com/u/musicdenotation)\
**Post date:** [December 5, 2013, 11:55am UTC](https://www.ruby-forum.com/t/why-the-status-code-restriction-on-add-header/237902/1 "2013-12-05T11:55:22Z")

</div>

Hello,  
I’m trying to add some custom headers to a 403 response, but had a hard  
time doing so. Looking through the docs, it seems add\_header "Adds the  
specified field to a response header provided that the response code  
equals  
200, 201, 204, 206, 301, 302, 303, 304, or 307. "

I was wondering what the reasoning was behind this pre-set list of  
status  
codes and why it isn’t possible for example to add a header to a 403 or  
404  
response. Also, if there is any workaround that would make this  
possible,  
I’d be interested to hear it.

Thanks,

Rich.

---

<div class="post-metadata">

**Author:** ![indigoi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/b3f665/32.png) [@indigoi](https://www.ruby-forum.com/u/indigoi)\
**Post date:** [December 5, 2013, 2:36pm UTC](https://www.ruby-forum.com/t/why-the-status-code-restriction-on-add-header/237902/2 "2013-12-05T14:36:40Z")

</div>

Hello!

On Thu, Dec 05, 2013 at 11:54:56AM +0100, Richard S. wrote:

> Hello,  
> I’m trying to add some custom headers to a 403 response, but had a hard  
> time doing so. Looking through the docs, it seems add\_header "Adds the  
> specified field to a response header provided that the response code equals  
> 200, 201, 204, 206, 301, 302, 303, 304, or 307. "
> 
> I was wondering what the reasoning was behind this pre-set list of status  
> codes and why it isn’t possible for example to add a header to a 403 or 404  
> response. Also, if there is any workaround that would make this possible,  
> I’d be interested to hear it.

The “add\_header” directive, much like “expires”, is intended to be  
used to add headers to positive responses, like “Cache-Control”  
and so on. Adding headers to error responses can easily end up in  
unexpected behaviour, e.g., due to an error response being cached  
for a year if you’ll add an “expires 1y” to your static files  
location (assuming you’ll only add new files and it’s safe). Due  
to the above headers are only added to response codes in a limited  
list.

As of now, there is no easy way to add response headers to  
arbitrary responses. You’ll have to do it either with embedded  
perl or with 3rd party modules.

–  
Maxim D.  
[http://nginx.org/en/donation.html](http://nginx.org/en/donation.html)
