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 } }
Every NGINX configuration file will be found in the /etc/nginx/ directory, with the main configuration file located in /etc/nginx/nginx. conf . NGINX configuration options are known as “directives”: these are arranged into groups, known interchangeably as blocks or contexts .
Variables are the natural part of the programming languages. These are just containers holding various values in imperative languages like Perl, C/C++, Bourne shell, Perl.
conf . /etc/nginx/conf. d/default. conf is used to configure the default virtual host. For this you can also use sites-available and sites-enabled .
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With