add_header is not working in certain locations

Francis Daly francis at
Wed Aug 13 23:33:39 UTC 2014

On Wed, Aug 13, 2014 at 02:28:27AM -0400, bodomic wrote:

Hi there,

> Hi all, I've got strange behavior that I don't understand in two different
> configs. I'll post examples below, in both of them I use add_header two
> times and one of them is not working while second does.
> Example 1:
> Request is: http://hostname/?region=XX 

In this case, for location matching the request is "/", so the best-match
location for that will be used.

> #This location adds header, so it's frankly the only reason I use it in my
> config. If I set location to "~ .*", I'll get header set also.
> location = / {
>   if ($arg_region) {
>      add_header Set-Cookie "__geoip_country__=$arg_region;Path=/";
>   }
>  proxy_pass http://backend; 
> }
> #If I remove the exact match above, I'll not get header set. Even if it's
> the only location in whole config and all site is working from there for
> sure.
>         location / {
>                 if ($arg_region) { add_header Set-Cookie
> "__geoip_country__=$arg_region; path=/" ;}
>                 proxy_pass http://backend;
>         }

It works for me.

In this case, I do get the Set-Cookie header when I have either one of
the two location{} blocks as the only location{} in the configuration. I
think you say that you do not get the Set-Cookie header in the second
case -- can you provide a complete, small, config that shows that?

> Example 2:
> Request is the same and this chain of locations is recommended by drupal
> community.

For these ones, I'm not sure how to set up a simple test.

Note, though, that you are using "if" inside "location". Generally, it is
not wise to do that unless you understand exactly what your configuration
actually means. for details.

Is there a link to this drupal recommendation? It isn't obvious to
me from the first few results for "nginx" or "drupal"

>         location / {
> #Does not work
>                 if ($arg_region) { add_header Set-Cookie
> "__geoip_country__=$arg_region;Path=/"; }
>                 try_files $uri @rewrite;
>         }

That one seems to be "# try_files wont work due to if" on the IfIsEvil page.


Francis Daly        francis at

More information about the nginx mailing list