Overridable header values (with map?)

Maxim Dounin mdounin at mdounin.ru
Thu Nov 30 14:45:07 UTC 2017


Hello!

On Thu, Nov 30, 2017 at 12:28:24AM -0500, Brandon Mintern wrote:

[...]

> This was fine, but I recently came across an interesting use of map
> <https://serverfault.com/a/598106/405305> that I thought I could generalize
> to simplify this pattern. My idea was to do something like:
> 
> # header-vars.conf:
> 
> map $robots $robots_tag {
> 
>     # Disallowed
>     default "noindex, nofollow, nosnippet, noarchive";
>     off     "noindex, nofollow, nosnippet, noarchive";
> 
>     # Allowed
>     on      all;
> }

[...]

> Unfortunately, I was mistaken that I would be able to use an undefined
> variable in the first position of a map directive (I thought it would just
> be empty):
> 
> unknown "robots" variable
> 
> Of course, I can't set a default value for that variable since I'm
> including header-vars.conf at the http level. I'd rather not need to
> include defaults in every server (there are many).
> 
> Does anyone have any suggestions for how I can better solve this problem?

The error in question will only appear if you don't have the 
variable defined at all, that is, it is not used anywhere in your 
configuration.  Using it at least somewhere will resolve the 
error.  That is, just add something like

    set $robots off;

anywhere in your configuration as appopriate (for example, in the 
default server{} block).

Once you will be able to start nginx, you'll start getting 
warnings when the variable is used uninitialized, e.g.:

... [warn] ... using uninitialized "robots" variable ...

These warnings can be switched off using the 
uninitialized_variable_warn directive, see 
http://nginx.org/r/uninitialized_variable_warn.

-- 
Maxim Dounin
http://mdounin.ru/


More information about the nginx mailing list