Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm currently trying to get nginx to add a header to the response when it is sending some kind of 50* error. I already have an add_header directive on the http block, and that gets respected for all requests except it seems errors. I also tried the following in one of my vhosts:

location /mediocregopheristhecoolest {
    add_header X-Test "blahblahblah";
    return 502;                                                                                                    

Going to that page gives me a 502, but no header. Is this simply something nginx doesn't do, or am I doing it wrong?

share|improve this question
Could you post the output of $ curl -I <url>//mediocregopheristhecoolest – Chida Aug 17 '12 at 17:50
up vote 15 down vote accepted

The documentation states that add_header "Adds the specified field to a response header provided that the response code equals 200, 204, 206, 301, 302, 303, 304, or 307. A value can contain variables." So it doesn't work with a 502.

I forgot to add that you can use the third party headers more module to add headers to other codes. You'll probably have to recompile to add it, though.

share|improve this answer
See comment of marat: the new nginx version supports setting the header irrespective of response code – Laurens Rietveld Feb 6 '15 at 14:37

Since Nginx 1.7.5 you can use always to add a header irrespective of the response code:

add_header X-Test "blahblahblah" always;
share|improve this answer
Frustratingly, the version in Debian's repos is currently outdated (1.6.x), but nginx maintains their own repository with the latest stable build (1.8.x right now). – kungphu Mar 2 at 0:46

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.