14

I need to set a variable to a default value if it is not already set. Following config does the job:

if ($foo ~ "^$") {
   set $foo default-value;
}

But it generates runtime warnings:

using uninitialized "foo" variable

Is there a way to test if a variable is set without generating the warning and without turning off the warning globally with:

uninitialized_variable_warn off;
  • try set_if_empty from github.com/openresty/set-misc-nginx-module – SaveTheRbtz Jun 2 '15 at 9:26
  • 2
    Just found this while looking for something similar, but it seems like uninitialized_variable_warn doesn't need to be set globally, you can just set it where you need to use potentially uninitialised variables. I'm using this for passing SSL details to PHP, which will be empty if the page is accessed without SSL. – Haravikk Dec 9 '15 at 14:37
  • @Haravikk it looks like that's not the case nginx: [emerg] "uninitialized_variable_warn" directive is duplicate in – n1te Oct 26 '18 at 2:20
  • Do you still have it set somewhere else? I'm using uninitialized_variable_warn as part of a php.conf that's imported only by locations that support PHP, so I'm definitely using it only in those scopes, while leaving the warning everywhere else. I don't have uninitialized_variable_warn set at any other scope though (letting it default to true). – Haravikk Oct 27 '18 at 8:25
11

I'm using the following to check if a variable exists:

if ($dir = false) {
    set $dir "";
}

If it's not set before it reaches this location block, then it will set it to a blank string. You can just as easily add a string between the quotes. I do not get any errors when doing a configuration test using this.

Please let me know if you're seeing otherwise.

  • I am seeing nginx: [emerg] unknown "dir" variable – Alex Barker Aug 11 '17 at 6:47
  • This false check does not work anymore, please use $foo ~ "^$" – Valdrinit May 2 at 14:18
1

The behaviour seems to have changed or at least be different for query parameter values using $arg_<name>. On nginx 1.13.6, using the following to check if a query value in the ngx variable exists:

if ($arg_test = false) {
    default_type 'text/plain';
    return 404;
}

does not pass to the 404. Using:

if ($arg_test = '') {
    default_type 'text/plain';
    return 404;
}

returns a 404, if the query parameter does not exist. Useful for not hitting upstream if a required query parameter does not exist.

0

If GET QUERY parameter detection

e.g: foo.com?proxy proxy variable exists Do not use $arg_proxy

if ($args ~ '(&|^)proxy([&=]|$)' ) { #exists set $port '8080'; } https://forum.nginx.org/read.php?2,224860,224860

0

An easy option, if you're optionally setting the variable in an included file, is simply to set the default value first.

set $value 'default';
include maybe-set-value.conf;

The second set statement in the conf file will replace the default value.

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.