如何在Nginx conf文件中定义全局变量 [英] How to define a global variable in nginx conf file
问题描述
如何在nginx conf文件中定义全局变量,如何在http块中定义全局变量,下面的所有服务器和位置都可以使用它.
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
}
}
推荐答案
您可以做一些技巧.如果必须从一个http
块中的每个server
块访问此值,则可以使用map
指令.这将如何运作?
map
指令允许您在http
块中的任何位置使用变量,该变量的值将在某些映射键上进行计算.讲故事的例子:
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
*/
}
}
}
那么,现在这对您意味着什么?您可以使用map
指令通过此简单技巧为所有server
块设置全局变量.您可以使用default
关键字为地图值设置默认值.就像这个简单的例子一样:
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;
}
在此示例中,我们根据$host
值计算$my_variable
的值,但是实际上$host
是什么都没有关系,因为我们将始终将 lalalala 设置为该值对于我们的变量,默认情况下,没有其他选项.现在,在代码中的所有地方,您将以与所有其他可用变量相同的方式使用$my_variable
(例如,使用set
指令创建的变量),您将获得 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
为什么这比仅使用set
指令更好?正如文档所说,由于set
指令 nginx设置指令仅可在server, location and if
块内部访问,因此不能用于为许多server
块创建全局变量.
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.
有关map
指令的文档可在此处获得:地图指令
Docs about map
directive are available here: map directive
这篇关于如何在Nginx conf文件中定义全局变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!