Nginx 变量

Nginx 同 Apache 等其他 Web 服务器的配置记法不太相同,Nginx的配置文件使用语法的就是一门微型的编程语言。可以类似写程序一般编写配置文件,可操作性很大。既然是编程语言,一般也就少不了“变量”这种概念。

一、nginx变量简介

  • 所有的 Nginx变量在 Nginx 配置文件中引用时都须带上 $ 前缀

  • 在 Nginx 配置中,变量只能存放一种类型的值,有且也只存在一种类型,那就是字符串类型

  • nginx可以使用变量简化配置与提高配置的灵活性,所有的变量值都可以通过这种方式引用:

$变量名

二、nginx 变量的定义和使用

nginx中的变量分为两种,自定义变量与内置预定义变量

1. 自定义变量

1.1 声明变量

可以在sever,http,location等标签中使用set命令(非唯一)声明变量,语法如下:

set $变量名 变量值

注意:

  • nginx 中的变量必须都以$开头
  • nginx 的配置文件中所有使用的变量都必须是声明过的,否则 nginx 会无法启动并打印相关异常日志
1.2 变量的可见性

在不同层级的标签中声明的变量性的可见性规则如下:

  • location标签中声明的变量中对这个location块可见
  • server标签中声明的变量对server块以及server块中的所有子块可见
  • http标签中声明的变量对http块以及http块中的所有子块可见
1.3 nginx安装echo模块

注意:我的 nginx 设置了软连接,没有设置的请以 nginx 绝对路径启动

# 下载 echo 模块的安装包
[root@localhost ~]# wget https://github.com/openresty/echo-nginx-module/archive/v0.61.tar.gz

# 解压到 nginx 相同的目录下
[root@localhost ~]# tar -xvzf v0.61.tar.gz -C /usr/local/

# 安装编译工具
[root@localhost ~]# yum -y install pcre pcre-devel openssl openssl-devel gcc gcc-c++ zlib zlib-devel

# -V 查看已有的模块
[root@localhost ~]# cd /usr/local/nginx-1.16.1/

[root@localhost nginx-1.16.1]# nginx -V
nginx version: nginx/1.16.1
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-39) (GCC) 
built with OpenSSL 1.0.2k-fips  26 Jan 2017
TLS SNI support enabled
configure arguments: --prefix=/usr/local/nginx --group=nginx --user=nginx --sbin-path=/usr/local/nginx/sbin/nginx --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --http-client-body-temp-path=/tmp/nginx/client_body --http-proxy-temp-path=/tmp/nginx/proxy --http-fastcgi-temp-path=/tmp/nginx/fastcgi --pid-path=/var/run/nginx.pid --lock-path=/var/lock/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_gzip_static_module --with-pcre --with-http_realip_module --with-stream

# 添加上原来已经有的参数和新添加的模块:# --add-module=/usr/local/echo-nginx-module-0.61
[root@localhost nginx-1.16.1]# ./configure --prefix=/usr/local/nginx --group=nginx --user=nginx --sbin-path=/usr/local/nginx/sbin/nginx --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --http-client-body-temp-path=/tmp/nginx/client_body --http-proxy-temp-path=/tmp/nginx/proxy --http-fastcgi-temp-path=/tmp/nginx/fastcgi --pid-path=/var/run/nginx.pid --lock-path=/var/lock/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_gzip_static_module --with-pcre --with-http_realip_module --with-stream --add-module=/usr/local/echo-nginx-module-0.61

#编译,不要make install 否则会覆盖原来的文件
[root@localhost nginx-1.16.1]# make   

# 将原来的nginx备份
[root@localhost nginx-1.16.1]# mv /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx.bk

# 拷贝新的nignx
[root@localhost nginx-1.16.1]# cp objs/nginx /usr/local/nginx/sbin/

[root@localhost nginx-1.16.1]# ls /usr/local/nginx/sbin/
nginx  nginx.bk

# 启动
[root@localhost nginx-1.16.1]# nginx

# 查看模块是否添加成功
[root@localhost nginx-1.16.1]# nginx -V
nginx version: nginx/1.16.1
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-39) (GCC) 
built with OpenSSL 1.0.2k-fips  26 Jan 2017
TLS SNI support enabled
configure arguments: --prefix=/usr/local/nginx --group=nginx --user=nginx --sbin-path=/usr/local/nginx/sbin/nginx --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --http-client-body-temp-path=/tmp/nginx/client_body --http-proxy-temp-path=/tmp/nginx/proxy --http-fastcgi-temp-path=/tmp/nginx/fastcgi --pid-path=/var/run/nginx.pid --lock-path=/var/lock/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_gzip_static_module --with-pcre --with-http_realip_module --with-stream --add-module=/usr/local/echo-nginx-module-0.61
1.4 配置 $foo=hello
[root@localhost conf.d]# cat server.conf
server {
        listen 80;
        server_name     localhost;
        location /test {
            set $foo "hello";
            echo "foo: $foo";
        }
}

[root@localhost conf.d]# curl 192.168.181.129/test
foo: hello
1.5 使用大括号插值

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

[root@localhost conf.d]# cat server.conf
server {
        listen 80;
        server_name     localhost;
        location /test {
            set $foo "hello-";
            echo "${foo}world";
        }
}

[root@localhost conf.d]# curl 192.168.181.129/test
hello-world

这里,我们在 echo 配置指令的参数值中引用变量 foo 的时候,后面紧跟着 world 这个单词,所以如果直接写作 "$fooworld" 则 Nginx “变量插值”计算引擎会将之识别为引用了变量 firstworld. 为了解决这个难题,Nginx 的字符串记法支持使用花括号在之后把变量名围起来,比如这里的 ${foo}

1.6 变量作用域

set 指令不仅有赋值的功能,它还有创建 Nginx 变量的副作用,即当作为赋值对象的变量尚不存在时,它会自动创建该变量。比如在上面这个例子中,如果 $a 这个变量尚未创建,则 set 指令会自动创建 $a 这个用户变量。如果我们不创建就直接使用它的值,则会报错。 例如:

[root@localhost conf.d]# cat server.conf
server {
        listen 80;
        server_name     localhost;

        location /test {
            echo $foo;
        }
}

# 此时 Nginx 服务器会拒绝加载配置:
[root@localhost conf.d]# nginx -s reload
nginx: [emerg] unknown "foo" variable

Nginx 变量的创建和赋值操作发生在全然不同的时间阶段,Nginx 变量的创建只能发生在 Nginx 配置加载的时候,或者说 Nginx 启动的时候,而赋值操作则只会发生在请求实际处理的时候。
这意味着不创建而直接使用变量会导致启动失败,同时也意味着我们无法在请求处理时动态地创建新的 Nginx 变量。

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

[root@localhost conf.d]# cat server.conf
server {
        listen 80;
        server_name     localhost;

        location /test {
            echo "foo = [$foo]";
        }
        location /test1 {
            set $foo 123;
            echo "foo = [$foo]";
        }

}

[root@localhost ~]# curl 192.168.181.129/test
foo = []
[root@localhost ~]# curl 192.168.181.129/test1
foo = [123]

这里我们在 location /test1 中用 set 指令创建了变量 foo,于是在整个配置文件中这个变量都是可见的,因此我们可以在 location /test 中直接引用这个变量而不用担心 Nginx 会报错。
从这个例子我们可以看到,set 指令因为是在 location /test1 中使用的,所以赋值操作只会在访问 /test1 的请求中执行。而请求 /test 接口时,我们总是得到空的 foo 值,因为用户变量未赋值就输出的话,得到的便是空字符串。

从这个例子我们可以窥见的另一个重要特性是,Nginx 变量名的可见范围虽然是整个配置,但每个请求都有所有变量的独立副本,或者说都有各变量用来存放值的容器的独立副本,彼此互不干扰。比如前面我们请求了 /test1 接口后,foo 变量被赋予了值 123,但它丝毫不会影响后续对 /test 接口的请求所对应的 foo 值(它仍然是空的!),因为各个请求都有自己独立的 $foo 变量的副本。

2. 内置预定义变量

内置预定义变量即无需声明就可以使用的变量,通常包括一个http请求或响应中一部分内容的值,以下为一些常用的内置预定义变量:

变量名 定义
$arg_PARAMETER GET请求中变量名PARAMETER参数的值。
$args 这个变量等于GET请求中的参数。例如,foo=123&bar=blahblah;这个变量只可以被修改
$binary_remote_addr 二进制码形式的客户端地址。
$body_bytes_sent 传送页面的字节数
$content_length 请求头中的Content-length字段。
$content_type 请求头中的Content-Type字段。
$cookie_COOKIE cookie COOKIE的值。
$document_root 当前请求在root指令中指定的值。
$document_uri 与$uri相同。
$host 请求中的主机头(Host)字段,如果请求中的主机头不可用或者空,则为处理请求的server名称(处理请求的server的server_name指令的值)。值为小写,不包含端口。
$hostname 机器名使用 gethostname系统调用的值
$http_HEADER HTTP请求头中的内容,HEADER为HTTP请求中的内容转为小写,-变为_(破折号变为下划线),例如:$http_user_agent(Uaer-Agent的值);
$sent_http_HEADER HTTP响应头中的内容,HEADER为HTTP响应中的内容转为小写,-变为_(破折号变为下划线),例如:$sent_http_cache_control, $sent_http_content_type…;
$is_args 如果$args设置,值为"?",否则为""。
$limit_rate 这个变量可以限制连接速率。
$nginx_version 当前运行的nginx版本号。
$query_string 与$args相同。
$remote_addr 客户端的IP地址。
$remote_port 客户端的端口。
$remote_user 已经经过Auth Basic Module验证的用户名。
$request_filename 当前连接请求的文件路径,由root或alias指令与URI请求生成。
$request_body 这个变量(0.7.58+)包含请求的主要信息。在使用proxy_pass或fastcgi_pass指令的location中比较有意义。
$request_body_file 客户端请求主体信息的临时文件名。
$request_completion 如果请求成功,设为"OK";如果请求未完成或者不是一系列请求中最后一部分则设为空。
$request_method 这个变量是客户端请求的动作,通常为GET或POST。包括0.8.20及之前的版本中,这个变量总为main request中的动作,如果当前请求是一个子请求,并不使用这个当前请求的动作。
$request_uri 这个变量等于包含一些客户端请求参数的原始URI,它无法修改,请查看$uri更改或重写URI。
$scheme 所用的协议,比如http或者是https,比如rewrite ^(.+)$
$scheme://example.com$1 redirect;
$server_addr 服务器地址,在完成一次系统调用后可以确定这个值,如果要绕开系统调用,则必须在listen中指定地址并且使用bind参数。
$server_name 服务器名称。
$server_port 请求到达服务器的端口号。
$server_protocol 请求使用的协议,通常是HTTP/1.0或HTTP/1.1。
$uri 请求中的当前URI(不带请求参数,参数位于args,不同于浏览器传递的args),不同于浏览器传递的request_uri的值,它可以通过内部重定向,或者使用index指令进行修改。不包括协议和主机名,例如/foo/bar.html

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

2.1 uri vs request_ur

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

[root@localhost conf.d]# cat server.conf
server {
        listen 80;
        server_name     localhost;

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

# 输出
[root@localhost ~]# curl 192.168.181.129/test
uri = /test
request_uri = /test

[root@localhost ~]# curl "192.168.181.129/test?a=3$b=4"
uri = /test
request_uri = /test?a=3=4

[root@localhost ~]# curl "192.168.181.129/test/hello%20world?a=3$b=4"
uri = /test/hello world
request_uri = /test/hello%20world?a=3=4
2.2 $arg_XXX

另一个特别常用的内建变量其实并不是单独一个变量,而是有无限多变种的一群变量,即名字以 arg_ 开头的所有变量,我们估且称之为 arg_XXX 变量群
一个例子是 arg_name,这个变量的值是当前请求中名为 name 的参数的值,而且还是未解码的原始形式的值。

[root@localhost conf.d]# cat server.conf
server {
        listen 80;
        server_name     localhost;

        location /test {
            echo "name: $arg_name";
            echo "age: $arg_age";
        }

}

# 输出
[root@localhost ~]# curl 192.168.181.129/test
name: 
age: 

[root@localhost ~]# curl "192.168.181.129/test?name=chen&age=18"
name: chen
age: 18

[root@localhost ~]# curl "192.168.181.129/test?name=hello%20world&age=18"
name: hello%20world
age: 18
2.3 $arg_XXX 不区分大小写

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

[root@localhost ~]# curl "192.168.181.129/test?NAME=chen"
name: chen
age:

[root@localhost ~]# curl "192.168.181.129/test?NaMe=chen&AgE=18"
name: chen
age: 18

你可能感兴趣的:(Nginx 变量)