Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在nginx conf文件中定义全局变量_Nginx - Fatal编程技术网

如何在nginx conf文件中定义全局变量

如何在nginx conf文件中定义全局变量,nginx,Nginx,如何在nginx conf文件中定义一个全局变量,在http块中定义一个全局变量,下面的所有服务器和位置都可以使用它 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 {

如何在nginx conf文件中定义一个全局变量,在http块中定义一个全局变量,下面的所有服务器和位置都可以使用它

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
      }
  }

你可以耍点小把戏。如果必须从一个
http
块中的每个
服务器
块访问此值,则可以使用
map
指令。这将如何工作?
map
指令允许您在
http
块中的任何位置使用变量,该变量的值将在某个map键上计算。举个很好的例子:

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
      */
    }
  }
}
那么现在,这对你意味着什么?您可以使用
map
指令,通过这个简单的技巧为所有
server
块设置一个全局变量。您可以使用
default
关键字为地图值设置默认值。在这个简单的例子中:

map $host $my_variable {
  default lalalala;
}
在本例中,我们在
$host
值上计算
$my_variable
的值,但实际上,
$host
是什么并不重要,因为默认情况下,我们总是将LALA设置为变量的值,并且没有其他选项。现在,在代码中的任何地方,当您以与所有其他可用变量相同的方式使用
$my_variable
时(例如使用
set
指令创建),您将获得LALA的值

为什么这比简单地使用
set
指令要好?因为正如doc所说的那样,
set
指令只能在
服务器、位置和if
块中访问,因此它不能用于为许多
服务器
块创建全局变量


这里提供了关于
map
指令的文档:

在服务器{}块中设置的任何内容都是继承的。您的意思是,如果我在第一个服务器中定义了一个var,那么我可以在它下面的所有服务器块中使用它?我知道这已经有8年历史了。我不知道那时候是否管用,但今天不行set指令用于服务器、位置和if块。在http上下文块中不允许。语法:设置$variable值;默认值:-上下文:服务器,位置,如果好的话!我可以使用map指令覆盖全局变量吗?知道如何访问Lua块中的$my_everywhere_used_变量吗?只需在luaBTW中使用
ngx.var.my_everywhere_used_variable
set
指令可以在
http.server
内部使用,但不能在
stream.server
内部使用。这仅在使用变量处理服务器{}块内的连接。它不是一个真正的全局变量,因为您将无法在服务器配置中的任何位置使用它,例如:`map$host$host\u ip{default 1.2.3.4;}server{listen$host\u ip:80;…}`这将产生如下错误:“listen”指令的“$host\u ip:80”中找不到主机