云计算学习之路——Nginx变量和echo模块

Nginx变量

nginx变量简介:
1、所有的nginx变量在nginx配置文件中引用时都需要带上$前缀
2、在Nnginx配置中,变量只能存放一种类型的值,有且只有一种类型,为字符串类型。

nginx变量的定义和使用:
nginx中的变量分为两种,自定义变量与内置预定义变量
1、自定义变量:可以在server、http、location等模块中使用set等命令声明,语法为:set $变量名 变量值
需要注意的是:nginx中的变量都必须以 $开头。nginx 的配置文件中所有使用的变量都必须是声明过的,否则 nginx 会无法启动并打印相关异常日志
2、内置预定义变量即无需声明就可以使用的变量,通常包括一个http请求或响应中一部分内容的值,以下为一些常用的内置预定义变量
在这里插入图片描述

nginx安装echo模块

1、查看已经安装的nginx版本

[root@localhost ~]# nginx -V
在这里插入图片描述

2、下载一个相同版本的nginx包

[root@localhost ~]#[root@localhost ~]# wget http://nginx.org/download/nginx-1.20.2.tar.gz
在这里插入图片描述

3、下载echo模块的安装包

[root@localhost ~]#[root@localhost ~]# wget https://github.com/openresty/echo-nginx-module/archive/v0.61.tar.gz
在这里插入图片描述

4、解压两个安装包

[root@localhost ~]# tar -xvzf nginx-1.20.2.tar.gz -C /usr/local/
[root@localhost ~]# tar -xvzf echo-nginx-module-0.61.tar.gz -C /usr/local/

5、安装编译环境

[root@localhost ~]# yum -y install pcre pcre-devel openssl openssl-devel gcc gcc-c++ zlib zlib-devel

6、添加echo模块
首先进入nginx的安装目录在原有的参数上新加入echo模块

[root@localhost ~]# cd /usr/localnginx-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-nginx-module-0.61
在这里插入图片描述

6、进行编译安装

[root@localhost nginx-1.20.2]# make && make install

7、验证
查看nginx版本,发现echo模块已添加成功
在这里插入图片描述

8、echo模块的使用案例
在这里插入图片描述

在这里插入图片描述

使用大括号插值

在“变量插值”的上下文中,还有一种特殊情况,即当引用的变量名之后紧跟着变量名的构成字符时(比如后跟字母、数字以及下划线),我们就需要使用特别的记法来消除歧义,例如:
在这里插入图片描述
在这里插入图片描述
这里,我们在 echo 配置指令的参数值中引用变量 first 的时候,后面紧跟着 world 这个单词,所以如果直接写作 “firstworld” 则 Nginx “变量插值”计算引擎会将之识别为引用了变量 firstworld. 为了解决这个难题,Nginx 的字符串记法支持使用花括号在 之后把变量名围起来,比如这里的 ${first}。

内置预定义变量
1、uri 与 request_uri

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

案例:
在这里插入图片描述
验证:
在这里插入图片描述

1、arg_xxx

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

案例:
在这里插入图片描述
验证:
在这里插入图片描述
$arg_XXX 不区分大小写
其实 $arg_name 不仅可以匹配 name 参数,也可以匹配 NAME 参数,抑或是 Name,Nginx 会在匹配参数名之前,自动把原始请求中的参数名调整为全部小写的形式。
在这里插入图片描述

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
THE END
分享
二维码
< <上一篇
)">
下一篇>>