5

I use nginx 1.10.1 with config similar to this:

server {

    (...)

    add_header Header1 "value";

    (...)

    # in this location above add_header directive works
    location / {
         uwsgi_pass unix:/var/run/some.sock;

         (...)
    }

    # ..and it this one it doesn't!
    location ~* (^/$|favicon.ico|robots.txt) {
        return 204;
        expires 24h;
        add_header Cache-Control "public";
        etag on;
    }
}

..so my problem is that Header1 is set for requests processed by the 1st location, but not for the 2nd one.

Why?

I have read add_header docs and know that it works by default only for "positive" return codes, but 204 is one of them (I have actually tested changing the code to 200, 404 and it didn't help).

(I have also tried to add always to my add_header Header1 ... but it was a rather desperate try as it shouldn't help - and it didn't.)

Jesse Nickles
  • 264
  • 2
  • 14
Greg Dubicki
  • 1,239
  • 1
  • 17
  • 33

1 Answers1

5

The documentation states:

These directives are inherited from the previous level if and only if there are no add_header directives defined on the current level

The presence of add_header Cache-Control "public"; prevents that block from inheriting add_header Header1 "value";.

Richard Smith
  • 12,834
  • 2
  • 21
  • 29
  • Thank you! I have actually read this part of the doc but got it wrong. I thought that it means that if you use `add_header X Y ; add_header A B` on `http` level and inside it on `location` level use `add_header X Z` then for requests processed by this location you'll get headers `X: Z` and `A: B`. Is there a way to have additive headers in nginx? – Greg Dubicki Oct 18 '16 at 19:22
  • 1
    @GregDubicki You can use `more_set_headers` instead. – Jesse Nickles Sep 01 '22 at 11:34