Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

Using nginx 1.2.1 I am able to add multiple headers using add_header as follows:

server {
    listen       80;
    server_name  localhost;
    root /var/www;
    add_header Name1 Value1;      <=== HERE
    add_header Name2 Value2;      <=== HERE

    location / {
        echo "Nginx localhost site";
    }
}


GET /
HTTP/1.1 200 OK
Name1: Value1
Name2: Value2

However I soon as I use the add_header directive inside location, the other add_header directives under server are ignored

server {
    listen       80;
    server_name  localhost;
    root /var/www;
    add_header Name1 Value1;      <=== HERE
    add_header Name2 Value2;      <=== HERE

    location / {
        add_header Name3 Value3;  <=== HERE
        add_header Name4 Value4;  <=== HERE
        echo "Nginx localhost site";
    }
}


GET /
HTTP/1.1 200 OK
Name3: Value3
Name4: Value4

The documentation says that both server and location are valid context and doesn't state that using add_header in one prevents using it in the other.

Q1: Do you know if this is a bug or the intended behaviour and why?

Q2: Do you see other options to get this fixed than using the HttpHeadersMoreModule module?

share|improve this question

1 Answer 1

This is expected behaviour. The add_header directives, much like all other array-type directives in nginx, are inherited from the previous level if and only if there are no add_header directives defined on the current level.

If you want nginx to add all headers in the location /, you have to list them all in location /, i.e.

location / {
    add_header Name1 Value1;
    add_header Name2 Value2;
    add_header Name3 Value3;
    add_header Name4 Value4;
    return 200 "";
}

If you need a way to list large subset of headers in a single place, you may use include directive for this. See docs here.

share|improve this answer
    
I only have add_header inside the location directive, but I still don't see this header added - any ideas? –  aditya menon Sep 29 '13 at 13:54
    
@adityamenon Do you have only one add_header? If yes, then make sure a response code is one from docs, see nginx.org/r/add_header. –  Maxim Dounin Oct 4 '13 at 17:53
    
Hi Maxim, I had a couple different issues, I finally have a config that works: stackoverflow.com/a/19135714/443219 –  aditya menon Oct 5 '13 at 8:50

Your Answer

 
discard

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.