Detailed methods nginx variables (1)

Nginx configuration file used is a miniature programming language, many real-world Nginx configuration file is actually a small one program. Of course, it is not the "Turing complete" Putting aside, at least according to my observation, it is greatly affected by Perl and Bourne Shell both languages ​​in the design. At this point, compared to other Apache and Lighttpd Web server configuration notation, it can not be said to be a major feature of the Nginx. Since it is a programming language, and ultimately, the general will "variable" this kind of thing (of course, Haskell so strange except for the functional language). Familiar with Perl, Bourne Shell, C / C ++ programming language commands friend must know, that white is the variable to store "value" of the container. The so-called "value" in many programming languages, either 3.14 this value may also be the string hello world, even like arrays, hash tables such complex data structures. However, in the Nginx configuration variables can only store one type of value, because there is only one type of value that string. For example, our nginx.conf file has the following line configuration:
set $a "hello world";
We used the standard ngx_rewrite module set configuration instructions for variable assignment $ a has been. In particular, we put the string hello world assigned to it. We see in front Nginx variable names have a $ symbol, which is a requirement on the notation. Nginx Nginx all the variables in the configuration file are required to bring along on $ prefix references. This representation and Perl, PHP these languages ​​are similar. Although the $ prefix such variables make modifications orthodox Java and C # programmers uncomfortable, but the benefits of this representation method is obvious, that can be directly embedded into the constant string variable to construct a new string:
set $a hello;
set $b "$a, $a";
Here we Nginx by existing variable value of $ a, $ b to form a variable value, so after two instructions executing the order, the value is $ a hello, and the value of $ b is hello, hello. This kind of technology is called "variable interpolation" (variable interpolation) in the Perl world, it makes a special string concatenation operator is no longer so necessary. Here we also wish to use this term. Let's look at a more complete configuration examples:
server {
listen 8080;
location /test {
set $foo hello;
echo "foo: $foo";
}
}
This example is omitted nginx.conf profile outermost http configuration blocks, and the block configuration events. The use curl HTTP client requests the / test interfaces on the command line, we can get
$ curl 'http://localhost:8080/test'
foo: hello
Here we use a third-party modules ngx_echo echo configuration instruction value of $ foo variable as an output thereof in response to the current request. We see, echo parameter configuration directives also support "variable interpolation." However, it should be noted that not all of the configuration instructions support "variable interpolation." Indeed, whether to allow instruction parameters "variable interpolation", depending on the implementation of the command module. If we want to output the string contains "dollar sign" ($) directly with the echo command, then there is no way to escape special characters $ off it? The answer is no (at least to the latest stable version of Nginx 1.0.10). Fortunately, we can bypass this limitation by not support such "variable interpolation" module configuration directives specially constructed value of the $ Nginx variable, and then use this variable in the echo. Look at the following example:
geo $dollar {
default "$";
}
server {
listen 8080;
location /test {
echo "This is a dollar sign: $dollar";
}
}
Test results are as follows:
$ curl 'http://localhost:8080/test'
This is a dollar sign: $
This uses a standard configuration command module ngx_geo geo provided to the variable $ dollar given the string "$", so we need to use the dollar sign in place below, on a direct reference to our variable $ dollar on it. In fact, most conventional usage ngx_geo module is assigned to the specified Nginx variables based on IP address of the client, and here just borrowing it to "unconditionally" to our variable $ dollar given to "dollar sign" this value. When the "variable interpolation" context, there is a special case, that is when the variable name followed by reference constitute character variable names (for example, followed by letters, numbers, and the underscore), we need to use a special notation to disambiguation, such as:
server {
listen 8080;
location /test {
set $first "hello ";
echo "${first}world";
}
}
Here, we refer to the variable $ first time, closely followed by the word world, so if the direct writing "$ firstworld" the Nginx "variable interpolation" computing engine will be recognized as a reference of a variable parameter values ​​echo configuration directives in . $ firstworld to solve this problem, Nginx string notation supports the use of braces after the $ surrounded the variable name, such as where the $ {first} the output of this example above is:
$ curl 'http://localhost:8080/test
hello world
set of instructions (as well as geo instructions mentioned earlier) is not only the assignment of function, it has created side effects Nginx variable, that is, when the object as a variable assignment does not already exist, it will be automatically created. For example, in the above example, if $ a variable that has not been created, the instruction set will automatically create the user $ a variable. If we do not directly use it to create value, it will error. E.g
server {
listen 8080;
location /bad {
echo $foo;
}
}
At this point Nginx server will refuse to load configuration:
[emerg] unknown "foo" variable
Yes, we can not even start the service! Interestingly, the creation and Nginx variable assignment takes place in an entirely different time periods. Nginx variables are created in the Nginx configuration can only occur when the load, or when Nginx start; and assignment will only happen when the actual processing of the request. This means do not create direct use of variables can cause failed to start, but also means we can not dynamically create new Nginx variables when processing the request. Nginx variable Once created, the visible range of the variable name is the entire Nginx configuration, even across different virtual server configuration block hosts. Let's look at an example:
server {
listen 8080;
location /foo {
echo "foo = [$foo]";
}
location /bar {
set $foo 32;
echo "foo = [$foo]";
}
}
Here we created in the location / bar with a set of instructions variable $ foo, then the entire profile is visible to this variable, so we can refer to this variable directly in the location / foo in without fear of Nginx error. Here are the results of these two access interfaces with the curl tool on the command line:
$ curl 'http://localhost:8080/foo'
foo = []

$ curl 'http://localhost:8080/bar'
foo = [32]

$ curl 'http://localhost:8080/foo'
foo = []
  From this example we can see, set command because it is used in the location / bar, the so assignment is performed only request access / bar in. The request / foo when the interface, we always get the value of $ foo empty, because the user is not assigned to a variable output, then get is an empty string. Another important feature of this example, we can glimpse is visible range Nginx variable names, although the entire configuration, but each has a separate request a copy of all the variables, or variables are used to store the values ​​of the independent container copy, do not interfere with each other. For example, after the foregoing we request / bar interfaces, $ foo variable is given the value 32, but it does not affect on the subsequent / foo request interface corresponding to the value of $ foo (which is still empty!), Since each request It has its own independent copy of the $ foo variable. Nginx For starters, one of the most common mistakes is to be understood as some kind of Nginx variables between requests globally shared something, or "global variables." In fact, the life of Nginx variable is impossible requests across the border. Article from: http: //blog.sina.com.cn/s/blog_6d579ff40100wi7p.html

Reproduced in: https: //my.oschina.net/766/blog/210899

Guess you like

Origin blog.csdn.net/weixin_34056162/article/details/91545986