Heim  >  Artikel  >  Backend-Entwicklung  >  nginx 变量

nginx 变量

WBOY
WBOYOriginal
2016-08-08 09:26:501105Durchsuche

详细原文查看这里:http://blog.sina.com.cn/s/blog_6d579ff40100wk2j.html

另一篇归纳原文的文章看这里:http://blog.csdn.net/yankai0219/article/details/8070790

变量插值:

所有的 Nginx变量在 Nginx配置文件中引用时都须带上 $前缀,用$符号+变量名来构造新的变量,这种方式叫做变量插值。例如:

location test{

    set $a hello;

    set $b "$a ,world";

    echo "b: $b";

}

访问:curl http://localhost/test/输出:b: hello world说明:上述setHttpRewriteModule的指令,而echoHttpEchoModule指令。可见这两个模块都支持变量插值。但是并不是所有模块都支持变量插值,事实上,指令参数是否允许变量插值,取决于该指令的实现模块。

量使用注意点:

当变量后面直接接其他字符串时,需要用{}将变量名括起来,否则会将变量名和后面的字符串连在一起当成一个变量名例如:

location /test {

    set $variable "hello";

    echo"${variable}world";

        }

变量必须先创建再使用,否则报错例如:

server {

    listen 80

    location /test

    {

        echo $foo;

     }

}

访问:curl http://localhost/test/报错:[emerg] unknown"foo" variable说明:nginxnginx变量的创建和赋值操作发生在全然不同的时间阶段。Nginx变量的创建只能发生在 Nginx配置加载的时候,或者说 Nginx启动的时候;而赋值操作则只会发生在请求实际处理的时候。这意味着不创建而直接使用变量会导致启动失败,同时也意味着我们无法在请求处理时动态地创建新的 Nginx变量

变量的可见性:

Nginx 变量一旦创建,其变量名的可见范围就是整个 Nginx配置,甚至可以跨越不同虚拟主机的 server配置块。例如:

server {

    listen 80;

 

    location /foo {

        echo "foo =[$foo]";

    }

 

    location /bar {

        set $foo 32;

        echo "foo =[$foo]";

    }

}

说明:这里我们在 location /bar中用 set指令创建了变量 $foo,于是在整个配置文件中这个变量都是可见的,因此我们可以在 location /foo中直接引用这个变量而不用担心 Nginx会报错。访问这两个接口的结果:

$ curl 'http://localhost/foo'

foo = []

 

$ curl 'http://localhost/bar'

foo = [32]

 

$ curl 'http://localhost/foo'

foo = []

从本例子得出另一个变量特性:
Nginx
变量名的可见范围虽然是整个配置,但每个请求都有所有变量的独立副本,或者说都有各变量用来存放值的容器的独立副本,彼此互不干扰。比如前面我们请求了 /bar接口后,$foo变量被赋予了值 32,但它丝毫不会影响后续对 /foo接口的请求所对应的 $foo值(它仍然是空的!),因为各个请求都有自己独立的 $foo变量的副本对于 Nginx 新手来说,最常见的错误之一,就是将 Nginx变量理解成某种在请求之间全局共享的东西,或者说全局变量。而事实上,Nginx变量的生命期是不可能跨越请求边界的

内建变量

常见的内建变量:
$uri:
用来获取当前请求的URI(经过解码,并且不包含请求参数)
$request_uri:
用来获取请求最原始的URI(未经解码,并且包含请求参数)

location /test {

    echo "uri = $uri";

    echo "request_uri =$request_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变量群:用来获取当前请求名为xxxURI参数的值

location /test {

    echo "name:$arg_name";

    echo "class:$arg_class";

}

请求和响应:

$ 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

其实 $arg_name不仅可以匹配 name参数,也可以匹配 NAME参数,抑或是 Name,等等,Nginx会在匹配参数名之前,自动把原始请求中的参数名调整为全部小写的形式:

$ curl 'http://localhost:8080/test?NAME=Marry'

 name: Marry

class:

 

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

name: Jimmy

class:

$cookie_xxx变量群:用来获取cookie
$http_xxx
变量群:用来获取请求头的值
$send_http_xxx
变量群:用来获取响应头的值
$args:
返回当前请求的URL参数串(即请求URL中问号后面的部分)

以上就介绍了nginx 变量,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn