Home  >  Article  >  Backend Development  >  nginx variables

nginx variables

WBOY
WBOYOriginal
2016-08-08 09:26:501149browse

See the detailed original text here: http://blog.sina.com.cn/s/blog_6d579ff40100wk2j.html

For another article summarizing the original text, see here: http://blog.csdn.net/yankai0219/ article/details/8070790

Variable interpolation:

All Nginx variables must be brought with $ when referenced in the Nginx configuration file prefix, use $ symbol + variable name to construct a new variable. This method is called variable interpolation. For example:

location test{

set $a hello;

set $b "$a ,world";

echo "b: $b";

}

Visit: curl http://localhost/test/Output: b: hello worldExplanation: The above set is HttpRewriteModule instructions , and echo is a HttpEchoModule instruction. It can be seen that both modules support variable interpolation. But not all modules support variable interpolation. In fact, whether the instruction parameter allows "Variable interpolation" depends on the implementation module of the instruction.

Points to note when using variables:

When a variable is directly followed by other strings, you need to use

{} to enclose the variable name, otherwise the variable name will be concatenated with the following string Together as a variable name For example:

location /test {

                                                                                                                               set $variable "hello";

Variables must be created first and then used, otherwise an error will be reported For example:

server {

Listen 80

location /test

{

foo;

}

}

Visit: curl

http://localhost/test/Error:

[emerg] unknown "foo" variable

Instructions: nginxnginxThe creation and assignment of variables occur at completely different stages of time. The creation of Nginx variables can only occur when Nginx configuration is loaded, or when Nginx is started; while the assignment operation can only occur when the request is actually processed. This means that using variables directly without creating them will cause startup failure, and it also means that we cannot dynamically create new Nginx variablesVisibility of variables: Once a Nginx

variable is created, the visible scope of its variable name is the entire

Nginx configuration, and can even span server

configuration blocks of different virtual hosts. For example: server { listen 80;

location /foo {

echo "foo = [$foo]";

}

location /bar {

set $foo 32;

echo "foo =[$foo]";

}

}

Description: Here we use the set

command to create the variable $foo

in

location /bar, so this variable is visible in the entire configuration file, so we can set it in location /foo You can directly reference this variable in without worrying about Nginx reporting an error. The results of accessing these two interfaces:

$ curl 'http://localhost/foo'

foo = []

$ curl 'http://localhost/bar'

foo = [32]

$ curl 'http://localhost/foo'

foo = []

Another variable feature derived from this example:
Nginx
Variable name Although the visible scope is the entire configuration, each request has an independent copy of all variables, or an independent copy of the container used by each variable to store values, without interfering with each other. For example, after we requested the /bar interface earlier, the $foo variable was assigned the value 32, but it will not affect subsequent requests to the /foo interface at all. The corresponding $foo value (it is still empty!), because each request has its own independent copy of the $foo variable This is most common for Nginx newbies One of the mistakes is to understand Nginx variables as something that is shared globally between requests, or "global variables ". In fact, the lifetime of Nginxvariables cannot cross request boundaries

Built-in variables

Common built-in variables:
$uri:
is used to get the current Requested URI (decoded and does not contain request parameters)
$request_uri:
is used to obtain the original URI (undecoded and does not contain request parameters)

location /test {

echo “uri = $uri”;

$ curl 'http://localhost:8080/test'

uri = /test

request_uri = /test

$ curl 'http://localhost:8080/test ?a=3&b=4'

uri = /test

request_uri = /test?a=3&b=4

$ curl 'http://localhost:8080/test/ hello%20world?a=3&b=4'

uri = /test/hello world

request_uri = /test/hello%20world?a=3&b=4

$arg_xxx

variable group : Used to get the value of the URI

parameter named xxx

in the current request

location /test { echo "name:$arg_name"; echo "class :$arg_class";}

Request and response:

$ curl 'http://localhost:8080/test'

name:

class:

$ curl 'http://localhost:8080/test?name=Tom&class=3'

name: Tom

class: 3

$ curl 'http://localhost:8080/test?name=hello%20world&class=9'

name: hello%20world

class: 9

Actually

$arg_name Not only can it match the

name parameter, it can also match the

NAME parameter, or

Name

, etc., Nginx will automatically match the parameters in the original request before matching the parameter name Adjust the name to all lowercase: $ curl 'http://localhost:8080/test?NAME=Marry' name: Marryclass:

$ curl 'http://localhost:8080/test?Name=Jimmy'

name: Jimmy

class:

$cookie_xxxVariable group: used to get the cookie value
$http_xxx
Variable group: used to get the value of the request header
$send_http_xxx
Variable group: used to get the response header The value of
$args:
returns the URL parameter string of the current request (that is, the part after the question mark in the request URL)

The above has introduced nginx variables, including aspects of it. I hope it will be helpful to friends who are interested in PHP tutorials.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn