nginx变量简介
- 所有的 Nginx变量在 Nginx 配置文件中引用时都须带上 $ 前缀
- 在 Nginx 配置中,变量只能存放一种类型的值,有且也只存在一种类型,那就是字符串类型
- ginx可以使用变量简化配置与提高配置的灵活性,所有的变量值都可以通过这种方式引用
nginx 变量的定义和使用
nginx中的变量分为两种,自定义变量与内置预定义变量
自定义变量
可以在sever,http,location等标签中使用set命令(非唯一)声明变量,语法如下:
set $变量名 变量值
注意:
nginx 中的变量必须都以$开头
nginx 的配置文件中所有使用的变量都必须是声明过的,否则 nginx 会无法启动并打印相关异常日志
nginx安装echo模块
安装部署
查看已经安装的nginx的版本
[root@localhost ~]# nginx -V
nginx version: nginx/1.20.2
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC)
built with OpenSSL 1.0.2k-fips 26 Jan 2017
TLS SNI support enabled
configure arguments: --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -fPIC' --with-ld-opt='-Wl,-z,relro -Wl,-z,now -pie'
上传或者下载一个相同版本的nginx包,下载echo模块的安装包
[root@localhost ~]# wget https://github.com/openresty/echo-nginx-module/archive/v0.61.tar.gz
[root@localhost ~]# ls
anaconda-ks.cfg echo-nginx-module-0.61.tar.gz nginx-1.20.2.tar.gz
[root@localhost ~]# tar zxf echo-nginx-module-0.61.tar.gz -C /usr/local
[root@localhost ~]# tar zxf nginx-1.20.2.tar.gz -C /usr/local
[root@localhost ~]# cd /usr/local
[root@localhost local]# mv echo-nginx-module echo
[root@localhost local]# yum -y install pcre pcre-devel openssl openssl-devel gcc gcc-c++ zlib zlib-devel
[root@localhost local]# cd nginx-1.20.2
[root@localhost nginx-1.20.2]# ./configure --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -fPIC' --with-ld-opt='-Wl,-z,relro -Wl,-z,now -pie' --add-module=/usr/local/echo
[root@localhost nginx-1.20.2]# make #编译,不要make install 否则会覆盖原来的文件
[root@localhost nginx-1.20.2]# mv /usr/sbin/nginx /usr/sbin/nginx_bak #将原来的nignx备份
[root@localhost nginx-1.20.2]# cp objs/nginx /usr/sbin/ 拷贝nignx
[root@localhost nginx-1.20.2]# systemctl restart nginx #重启
[root@localhost nginx-1.20.2]# nginx -V 查看模块是否添加成功
....
使用echo模块
[root@localhost ~]# vim /etc/nginx/conf.d/rewrite.conf
server {
listen 80;
server_name www.hjf777.com;
location / {
set $say hello;
echo "见面要说:$say";
}
}
[root@localhost ~]# nginx -s reload
用其他机器访问测试
[root@localhost ~]# curl 192.168.242.138
见面要说:hello
使用大括号插值
在“变量插值”的上下文中,还有一种特殊情况,即当引用的变量名之后紧跟着变量名的构成字符时(比如后跟字母、数字以及下划线),我们就需要使用特别的记法来消除歧义,例如:
[root@localhost ~]# vim /etc/nginx/conf.d/rewrite.conf
server {
listen 80;
server_name www.hjf777.com;
location / {
set $say hello;
echo "见面要说:${say}TOM";
}
}
[root@localhost ~]# nginx -s reload
用其他机器访问测试
[root@localhost ~]# curl 192.168.242.138
见面要说:helloTOM
这里,我们在 echo 配置指令的参数值中引用变量 first 的时候,后面紧跟着 world 这个单词,所以如果直接写作 "firstworld" 则 Nginx “变量插值”计算引擎会将之识别为引用了变量 firstworld. 为了解决这个难题,Nginx 的字符串记法支持使用花括号在 之后把变量名围起来,比如这里的 ${first}。
内置预定义变量
内置预定义变量即无需声明就可以使用的变量,通常包括一个http请求或响应中一部分内容的值,以下为一些常用的内置预定义变量
变量名 | 定义 |
$arg_PARAMETER | GET请求中变量名PARAMETER参数的值。 |
$args | 这个变量等于GET请求中的参数。例如,foo=123&bar=blahblah;这个变量只可以被修改 |
$binary_remote_addr | 二进制码形式的客户端地址。 |
$body_bytes_sen | 传送页面的字节数 |
$content_length | 请求头中的Content-length字段。 |
$content_type | 请求头中的Content-Type字段。 |
$cookie_COOKIE | cookie COOKIE的值。 |
$document_roo | 当前请求在root指令中指定的值。 |
$document_uri | 与$uri相同。 |
$host | 请求中的主机头(Host)字段,如果请求中的主机头不可用或者空,则为处理请求的server名称(处理请求的server的server_name指令的值)。值为小写,不包含端口。 |
$hostname | 机器名使用 gethostname系统调用的值 |
$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 | 请求到达服务器的端口号。 |
$server_protocol | 请求使用的协议,通常是HTTP/1.0或HTTP/1.1。 |
$uri | 请求中的当前URI(不带请求参数,参数位于args,不同于浏览器传递的args),不同于浏览器传递的request_uri的值,它可以通过内部重定向,或者使用index指令进行修改。不包括协议和主机名,例如/foo/bar.html |
uri与request_uri
由 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
$arg_xxx
另一个特别常用的内建变量其实并不是单独一个变量,而是有无限多变种的一群变量,即名字以 arg_ 开头的所有变量,我们估且称之为 $arg_XXX 变量群。
一个例子是 $arg_name,这个变量的值是当前请求中名为 name 的参数的值,而且还是未解码的原始形式的值。
location /test-arg {
echo "name: $arg_name";
echo "class: $arg_class";
}
输出
[root@localhost html]# nginx -s reload
[root@localhost html]# curl localhost/test-arg
name:
class:
[root@localhost html]# curl "localhost/test-arg?name=Tom&class=3"
name: Tom
class: 3
[root@localhost html]# curl "localhost/test-arg?name=hello%20world&class=9"
name: hello%20world
class: 9
注意: $arg_XXX 不区分大小写
$arg_name
不仅可以匹配name
参数,也可以匹配 NAME 参数,抑或是 Name,Nginx 会在匹配参数名之前,自动把原始请求中的参数名调整为全部小写的形式。(无论$arg_xxx在配置文件内还是在uri中的参数,都不区分大小写,比如在配置文件内是$arg_NAme,在uri中能够同样匹配到name参数)
[root@localhost html]# curl "localhost/test-arg?NAME=Marry"
name: Marry
class:
[root@localhost html]# curl "localhost/test-arg?Name=Jimmy"
name: Jimmy
class: