Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to define a global variable in nginx conf file

Tags:

nginx

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       }   } 
like image 900
sinory Avatar asked Jan 21 '13 06:01

sinory


People also ask

Where config file Nginx?

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 .

What is a variable in nginx?

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.

What is the use of Conf d in nginx?

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 .


1 Answers

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

like image 156
Michał Kupisiński Avatar answered Sep 19 '22 08:09

Michał Kupisiński