The road to learning cloud computing - Nginx variables and echo modules

Nginx variables

Introduction to nginx variables:
1. All nginx variables need to be prefixed with $ when referenced in the nginx configuration file.
2. In the Nnginx configuration, variables can only store one type of value, and there is only one type, which is a string. type.

Definition and use of nginx variables:
Variables in nginx are divided into two types, custom variables and built-in predefined variables
1. Custom variables: You can use set and other commands to declare in modules such as server, http, location, etc. The syntax is: set $Variable name Variable value
It should be noted that all variables in nginx must start with $. All variables used in the nginx configuration file must be declared, otherwise nginx will be unable to start and print relevant exception logs. 2. Built-in
predefined variables are variables that can be used without declaration, usually including part of an http request or response. The value of the content. The following are some commonly used built-in predefined variables.
Insert image description here

nginx install echo module

1. Check the installed nginx version

[root@localhost ~]# nginx -V
Insert image description here

2. Download an nginx package of the same version

[root@localhost ~]#[root@localhost ~]# wget http://nginx.org/download/nginx-1.20.2.tar.gz
Insert image description here

3. Download the installation package of the echo module

[root@localhost ~]#[root@localhost ~]# wget https://github.com/openresty/echo-nginx-module/archive/v0.61.tar.gz
Insert image description here

4. Unzip the two installation packages

[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. Install the compilation environment

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

6. Add the echo module.
First enter the nginx installation directory and add the echo module to the original parameters.

[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
Insert image description here

6. Compile and install

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

7. Verification
Check the nginx version and find that the echo module has been added successfully.
Insert image description here

8. Use cases of echo module
Insert image description here

Insert image description here

Interpolate using braces

In the context of "variable interpolation", there is a special case, that is, when the referenced variable name is followed by the constituent characters of the variable name (such as followed by letters, numbers, and underscores), we need to use special notation. Eliminate ambiguity, for example:
Insert image description here
Insert image description here
here, when we refer to the variable first in the parameter value of the echo configuration directive, it is followed by the word world, so if it is written directly as "firstworld", the Nginx "variable interpolation" calculation engine will recognize it as The variable firstworld is referenced. To solve this problem, Nginx's string notation supports the use of curly braces to surround the variable name afterwards, such as ${first} here.

Built-in predefined variables
1, uri given request_uri

The built-in variable uri provided by the ngx_http_core module can be used to obtain the URI of the current request (without request parameters), while request_uri is used to obtain the original URI of the request (including request parameters).

Case:
Insert image description here
Verification:
Insert image description here

1、arg_xxx

Another particularly commonly used built-in variable is actually not a single variable, but a group of variables with infinite variations, that is, all variables whose names begin with arg_, which we call the arg_XXX variable group.
arg_name, the value of this variable is the value of the parameter named name in the current request, and it is also the value of the undecoded raw form.

Case:
Insert image description here
Verification:
Insert image description here
$arg_XXX is not case-sensitive.
In fact, $arg_name can not only match the name parameter, but also the NAME parameter, or Name. Nginx will automatically adjust the parameter name in the original request to all lowercase before matching the parameter name. form.
Insert image description here

Guess you like

Origin blog.csdn.net/weixin_44178770/article/details/124389811