10

In nginx I can use set to define a variable, but is it possible to define a default?

e.g.

set $foo bar if $foo is not defined
  • 2
    What are you trying to accomplish? – Michael Hampton Nov 28 '12 at 17:20
16
map $foo $new_foo {
    default  $foo;
    ''       bar;
}

or

if ($foo = '') {
    set $foo bar;
}
| improve this answer | |
  • 2
    Using the if method I got using uninitialized "foo" variable warning in my log. Can I avoid it? – hiroshi Feb 18 '18 at 6:04
0

This also works:

map $foo $foo {
    default '';
}
map $foo $bar {
    default '';
    'bar' 'bar';
    'foo' $foo;   <----- nginx: [emerg] unknown "foo" variable
}

map $foo $foo {} solves error nginx: [emerg] unknown "foo" variable - just doesn't look right - but it works.

Note: map must be in block http {} (nginx/conf.d)

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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