How to define a global variable in nginx conf file

2020-05-17 00:14发布

How to define a global variable in nginx conf file, define a global var in http block,and all servers and locations below can use it.

http{
      some confs
      ...
      //define a global var mabe like
      set APP_ROOT /home/admin
      // and it can be use in all servers and locations below, like
      server {
        root $APP_ROOT/test1
      }

      server {
        root $APP_ROOT/test2
      }
  }

标签: nginx
1条回答
太酷不给撩
2楼-- · 2020-05-17 00:43

You can do a little trick. If this value must be accessible from every server block in one http block you can use map directive. How will this work?
The map directive allows you to use a variable anywhere in an http block which value will be calculated on some map key. All-telling example:

http {

  ...

  /* 
     value for your $my_everywhere_used_variable will be calculated
     each time when you use it and it will be based on the value of $query_string.
  */
  map $query_string $my_everywhere_used_variable {

    /* 
       if the actual value of $query_string exactly match this below then 
       $my_everywhere_used_variable will have a value of 3
    */
    /x=1&y=2&opr=plus     3;

    /* 
       if the actual value of $query_string exactly match this below then
       $my_everywhere_used_variable will have a value of 4
    */
    /x=1&y=4&opr=multi    4;

  /*
    it needs to be said that $my_everywhere_used_variable's value is calculated each
    time you use it. When you use it as pattern in a map directive (here we used the
    $query_string variable) some variable which will occasionally change 
    (for example $args) you can get more flexible values based on specific conditions
  */
  }

  // now in server you can use this variable as you want, for example:

  server {

    location / {
      rewrite .* /location_number/$my_everywhere_used_variable;
      /* 
         the value to set here as $my_everywhere_used_variable will be
         calculated from the map directive based on $query_string value
      */
    }
  }
}

So now, what does this mean for you? You can use the map directive to set a global variable for all server blocks with this simple trick. You can use the default keyword to set a default value for your map value. As in this simple example:

map $host $my_variable {
  default lalalala;
}

In this example we calculate the value of $my_variable on the $host value, but in fact it doesn't matter what $host is because we will always set lalalala as the value for our variable by default and without other options. Now everywhere in your code when you will use $my_variable in the same way as all other available variables (for example created with set directive) you will get value of lalalala

And why is this better than simply using the set directive? Because the set directive, as doc says nginx set directive is only accessible inside server, location and if blocks, so it cannot be used to create global variable for a number of server blocks.

Docs about map directive are available here: map directive

查看更多
登录 后发表回答