Nginx The configuration file of is a micro programming language , A lot of real world Nginx The configuration file is actually a small program . Of course , Is it right? “ Turing is complete ” For the time being , At least according to my observation , It's designed by Perl and Bourne Shell These two languages have a great influence . At this point , comparison Apache and Lighttpd Others, such as Web Server configuration notation , I can't help saying it's Nginx It's a big feature of . Since it's a programming language , In general, it is necessary to “ Variable ” This kind of thing ( Of course ,Haskell Except for such strange functional languages ).
be familiar with Perl、Bourne Shell、C/C++ And so on imperative programming language's friend certainly knew , Variables are simply stored “ value ” The container of . And so called “ value ”, In many programming languages , It can be either 3.14
Something like that , It can also be hello world
Such a string , It can even be like arrays 、 Hash table is a complex data structure . However , stay Nginx Configuration in progress , Variables can hold only one type of value , Because there is only one type of value , That's the string .
Like our nginx.conf
The file has the following line configuration :
set $a "hello world";
We used standards ngx_rewrite Modular set Configuration instruction pair variables $a
The assignment operation is carried out . Specially , We put the string hello world
Given it .
We see ,Nginx The variable name is preceded by a $
Symbol , This is the requirement of notation . be-all Nginx Variable in Nginx All references in the configuration file must be accompanied with $
Prefix . This representation is similar to Perl、PHP These languages are similar .
although $
Such variable prefixes make orthodox Java
and C#
Programmers are not comfortable , But the benefits of this representation are obvious , That is, you can directly embed variables into string constants to construct new strings :
Here we go through what we have Nginx Variable $a
Value , To construct variables $b
Value , So after these two instructions are executed in sequence ,$a
The value of is hello
, and $b
The value of is hello, hello
. This technology Perl The world is called “ Variable interpolation ”(variable interpolation), It makes specialized string splicing operators less necessary . We might as well use this term here .
Let's look at a more complete configuration example :
This example omits nginx.conf
The outermost part of the configuration file http
Configuration block and events
Configuration block . Use curl
This HTTP The client requests this on the command line /test
Interface , We can get
$ curl 'http://localhost:8080/test'
foo: hello
Here we use a third party ngx_echo Modular echo Configuration instructions will $foo
The value of the variable is output as the response body of the current request .
We see ,echo The parameters of the configuration command also support “ Variable interpolation ”. however , It should be noted that , Not all configuration instructions support “ Variable interpolation ”. in fact , Whether the instruction parameter allows “ Variable interpolation ”, Depending on the implementation module of the instruction .
If we want to pass echo Instruction direct output contains “ Dollar symbol ”($
) String , So is there any way to put the special $
Characters are escaped ? The answer is No ( At least up to date Nginx Stable version 1.0.10
). But fortunately , We can get around this limitation , For example, by not supporting “ Variable interpolation ” The module configuration instruction of is specially constructed with the value of $
Of Nginx Variable , And then in echo Use this variable . Here's an example :
geo $dollar {
default "$";
}
server {
listen 8080;
location /test {
echo "This is a dollar sign: $dollar";
}
}
The test results are as follows :
$ curl 'http://localhost:8080/test'
This is a dollar sign: $
Standard modules are used here ngx_geo Configuration instructions provided geo To be a variable $dollar
Give the string "$"
, So we need to use the dollar sign below , Just quote our $dollar
Variables are OK . Actually ngx_geo The most common use of modules is based on the client's IP The address pair specifies Nginx Variable assignment , This is just borrowing it to “ Unconditionally ” To our $dollar
Variables give “ Dollar symbol ” This value .
stay “ Variable interpolation ” In the context of , There is another special case , That is, when the referenced variable name is followed by the constituent character of the variable name ( For example, followed by letters 、 Numbers and underscores ), We need to use special notation to disambiguate , for example :
here , We are echo Variables are referenced in parameter values of configuration instructions $first
When , Followed closely by world
The word , So if you write directly "$firstworld"
be Nginx “ Variable interpolation ” The computing engine recognizes it as a reference to a variable $firstworld
. To solve this problem ,Nginx The string notation of supports the use of curly braces in $
Then enclose the variable name , Like here ${first}
. The output of the above example is :
$ curl 'http://localhost:8080/test
hello world
set Instructions ( And as mentioned earlier geo Instructions ) It not only has the function of assignment , It also creates Nginx Side effects of variables , That is, when the variable as the assignment object does not exist yet , It automatically creates the variable . For example, in the above example , If $a
This variable has not been created yet , be set
Instructions are created automatically $a
This user variable . If we don't create it, we just use its value , May be an error . for example
here Nginx The server will refuse to load the configuration :
[emerg] unknown "foo" variable
Yes , We can't even start the service !
Interestingly ,Nginx The creation and assignment of variables take place at very different times .Nginx Variable creation can only happen in Nginx When the configuration is loaded , Or say Nginx When it starts ; Assignment only happens when the request is actually processed . This means that using variables directly without creating them causes the startup to fail , It also means that we can't create new ones dynamically while the request is being processed Nginx Variable .
Nginx Once the variable is created , The visible range of its variable name is the entire Nginx To configure , Even across different virtual hosts server
Configuration block . Let's take an example :
server {
listen 8080;
location /foo {
echo "foo = [$foo]";
}
location /bar {
set $foo 32;
echo "foo = [$foo]";
}
}
Here we are location /bar
of use set
Instructions create variables $foo
, So this variable is visible throughout the configuration file , So we can location /foo
You can refer to this variable directly without worrying about it Nginx Will report a mistake .
Here's how to use curl
The result of the tool accessing these two interfaces :
$ 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 that ,set
Because the command is location /bar
Used in , So the assignment will only be done when accessing /bar
To execute . The request /foo
Interface , We always get empty $foo
value , Because the user variable is output without assignment , You get an empty string .
Another important feature we can see from this example is ,Nginx The visible range of variable names is the entire configuration , But each request has a separate copy of all variables , In other words, there are independent copies of the container in which each variable is used to store values , Don't interfere with each other . For example, we asked /bar
After the interface ,$foo
Variables are given values 32
, But it doesn't affect the follow-up to /foo
Interface request corresponding to $foo
value ( It's still empty !), Because each request has its own independent $foo
Copies of variables .
about Nginx beginners , One of the most common mistakes , Will be Nginx Variables are understood as something that is globally shared between requests , Or say “ Global variables ”. As a matter of fact ,Nginx The lifetime of a variable cannot cross the request boundary .
( To be continued )
from:http://blog.sina.com.cn/s/blog_6d579ff40100wi7p.html
Nginx Random talk about variables ( One ) More articles about
- Nginx Random talk about variables ( 8、 ... and )
And $arg_XXX similar , We are ( Two ) Built in variables mentioned in $cookie_XXX Variables will also be called XXX Of cookie Returns a special value when it does not exist “ Did not find ”: location /test ...
- Nginx Random talk about variables ( 7、 ... and )
stay ( One ) We mentioned in ,Nginx The value of a variable has only one type , That's the string , But it's also possible that variables don't have meaningful values at all . Variables without values have two special values : One is “ illegal ”(invalid), The other is “ Did not find ”( ...
- Nginx Random talk about variables ( 5、 ... and )
In the front ( Two ) We have learned that the lifetime of the variable value container is bound to the request , But I deliberately avoided “ request ” Formal definition of . You should always acquiesce in “ request ” They are initiated by the client HTTP request . Actually in Nginx There are... In the world ...
- Nginx Random talk about variables ( Four )
In setting up the “ Access handler ” Under the circumstances ,Nginx A variable can also choose to use its value container as a cache , In this way, when the variable is read many times , Just call “ Access handler ” Calculate once . Let's take a look at an example : map $args ...
- Nginx Random talk about variables ( 3、 ... and )
There are also some built-in variables that support rewriting , One example is $args. This variable returns the current requested URL Parameter string ( I.e. request URL The part after the question mark in , If any ), In the assignment, you can directly modify the parameter string . Let's take a look at one ...
- Nginx Random talk about variables ( Two )
About Nginx Another common misconception about variables is that the lifetime of a variable container , Is with the location Configuration block bound . It's not . Let's look at one that involves “ Jump inside ” Example : server { listen ...
- Nginx Random talk about variables
from :http://blog.sina.com.cn/openrestyNginx The configuration file of is a micro programming language , A lot of real world Nginx The configuration file is actually a small program . Of course , Is it right? ...
- Nginx Random talk about variables ( 6、 ... and )
Nginx Built in variables are used in “ Sub request ” In the context of , Its behavior will also become a little subtle . In the front ( 3、 ... and ) We already know , Many built-in variables are not simple “ A container for storing values ”, They usually register “ Access handler ” To be different , ...
- Nginx Variable implementation mechanism
Nginx There are two ways to define variables , One is to use... In the configuration file set Instructions ( from rewrite Modules provide support ), The other is to define variables within a module . Variable related structures : struct ngx_http_variable_s { ...
Random recommendation
- 【 Answer key 】【 Bit operation 】【Leetcode】Single Number II
Given an array of integers, every element appears three times except for one. Find that single one. ...
- Solr4.8.0 Source code analysis (22) And SolrCloud Of Recovery Strategy ( 3、 ... and )
Solr4.8.0 Source code analysis (22) And SolrCloud Of Recovery Strategy ( 3、 ... and ) This article is about SolrCloud Of Recovery The third article in the strategy series , The first two articles mainly introduce Recovery The overall process of , as well as P ...
- DSOframer Microsoft official API How to look up
Don't understand DSOframer Friend, , You can refer to the article first DSOframer Brief introduction and resource arrangement of . We are using DSOframer when , I often don't know where to look it up API file , The articles on the Internet are very scattered , It's hard to find self ...
- VAXVOIP SDK Licensekey
Insert the following key to the SDK with SetLicenseKey call: VAXVOIP.COM-191P238P55P253P97P229P51P76 ...
- MySQL Concurrent replication series 2 : Multithreaded replication 2016
Concurrent replication (Parallel Replication) Series two : Enhanced Multi-threaded Slaves author : Wok Technology MySQL Database Engineer Ma Pengfei First, sort out the tradition MySQL/M ...
- Android art ——Bitmap Efficient loading and caching (1)
adopt Bitmap We can design a ImageLoader, The function that the implementation should have is : Synchronous loading of pictures : Asynchronous loading of pictures : Image compression : Memory cache : Disk caching : Internet access : 1. load First of all, load :BitmapFact ...
- [ Codeforces Round #554 (Div. 2) C]
C. Neko does Maths time limit per test 1 second memory limit per test 256 megabytes input standard i ...
- Android programming resource file string The use of placeholders in
Here's a detailed introduction to the use of placeholders , Put it into practice , It can simplify the layout file , Reduce string resources . 1. Use in resource files . Open... In the resource file strings.xml file , Editing . As shown in the figure below : chart 1.0 2. ...
- caffe Network structure visualization online tool
http://ethereon.github.io/netscope/#/editor shift+enter
- Please make sure ASP.NET State Service (ASP.NET State Service ) Started Problem solving
When iis The deployed web site encountered the following error : Unable to issue session state request to session state server . Please make sure ASP.NET State Service (ASP.NET State Service ) Started , And the client port is the same as the server port . Such as ...