nginx 配置之变量

重点:在 Nginx 配置中,变量只能存放一种类型的值,那就是字符串

1. 定义变量语法:

set $argument  test

server {
    listen 8080;
    server_name  localhost;
    
    location /test {
            set $foo hello;
            echo "foo: $foo";
    }
}

  输出:

[root@localhost html]# nginx -s reload
[root@localhost html]# curl localhost/test
foo:  hello

1.2 使用大括号插值  

 在“变量插值”的上下文中,还有一种特殊情况,即当引用的变量名之后紧跟着变量名的构成字符时(比如后跟字母、数字以及下   划线),我们就需要使用特别的记法来消除歧义,例如:

server {
    ...
    location /test-brace {
        set $first "hello ";
        echo "${first}world";
    }
}

 输出:

[root@localhost html]# nginx -s reload
[root@localhost html]# curl localhost/test-brace
hello world

1.3 变量作用域

set 指令(以及geo 指令)不仅有赋值的功能,它还有创建 Nginx 变量的副作用,即当作为赋值对象的变量尚不存在时,它会自动创建该变量。Nginx 变量的创建和赋值操作发生在全然不同的时间阶段,Nginx 变量的创建只能发生在 Nginx 配置加载的时候,或者说 Nginx 启动的时候,而赋值操作则只会发生在请求实际处理的时候。这意味着不创建而直接使用变量会导致启动失败,同时也意味着我们无法在请求处理时动态地创建新的 Nginx 变量。Nginx 变量一旦创建,其变量名的可见范围就是整个 Nginx 配置,甚至可以跨越不同虚拟主机的 server 配置块

2. 系统内置变量

Nginx 内建变量最常见的用途就是获取关于请求或响应的各种信息。

由 ngx_http_core 模块提供的内建变量 $uri,可以用来获取当前请求的 URI(经过解码,并且不含请求参数),
而 $request_uri 则用来获取请求最原始的 URI (未经解码,并且包含请求参数)。

location /test-uri {
    echo "uri = $uri";
    echo "request_uri = $request_uri";
}

输出

[root@localhost html]# nginx -s reload
[root@localhost html]# curl localhost/test-uri
uri = /test-uri
request_uri = /test-uri

[root@localhost html]# curl "localhost/test-uri?a=3&b=4"
uri = /test-uri
request_uri = /test-uri?a=3&b=4

[root@localhost html]# curl "localhost/test-uri/hello%20world?a=3&b=4"
uri = /test-uri/hello world
request_uri = /test-uri/hello%20world?a=3&b=4

全局变量:

arg_PARAMETER    #这个变量包含GET请求中,如果有变量PARAMETER时的值。
args                          #这个变量等于请求行中(GET请求)的参数,如:foo=123&bar=blahblah;
binary_remote_addr #二进制的客户地址。
body_bytes_sent    #响应时送出的body字节数数量。即使连接中断,这个数据也是精确的。
content_length    #请求头中的Content-length字段。
content_type      #请求头中的Content-Type字段。
cookie_COOKIE    #cookie COOKIE变量的值
document_root    #当前请求在root指令中指定的值。
document_uri      #与uri相同。
host                #请求主机头字段,否则为服务器名称。
hostname          #Set to themachine’s hostname as returned by gethostname
http_HEADER
is_args              #如果有args参数,这个变量等于”?”,否则等于”",空值。
http_user_agent    #客户端agent信息
http_cookie          #客户端cookie信息
limit_rate            #这个变量可以限制连接速率。
query_string          #与args相同。
request_body_file  #客户端请求主体信息的临时文件名。
request_method    #客户端请求的动作,通常为GET或POST。
remote_addr          #客户端的IP地址。
remote_port          #客户端的端口。
remote_user          #已经经过Auth Basic Module验证的用户名。
request_completion #如果请求结束,设置为OK. 当请求未结束或如果该请求不是请求链串的最后一个时,为空(Empty)。
request_method    #GET或POST
request_filename  #当前请求的文件路径,由root或alias指令与URI请求生成。
request_uri          #包含请求参数的原始URI,不包含主机名,如:”/foo/bar.php?arg=baz”。不能修改。
scheme                #HTTP方法(如http,https)。
server_protocol      #请求使用的协议,通常是HTTP/1.0或HTTP/1.1。
server_addr          #服务器地址,在完成一次系统调用后可以确定这个值。
server_name        #服务器名称。
server_port          #请求到达服务器的端口号。

原文地址:https://www.jianshu.com/p/44680c081ea0

3. nginx location正则

官方文档:http://wiki.nginx.org/NginxHttpCoreModule#location

   1) “~” for case sensitive matching : 区分大小

   2) “~*” for case insensitive matching:不区分大小

发布了55 篇原创文章 · 获赞 17 · 访问量 8万+

猜你喜欢

转载自blog.csdn.net/XuHang666/article/details/85050829
今日推荐