一、编译echo模块
1、echo模块下载以及解压
wget -c https://gitee.com/mirrors/echo-nginx-module/repository/archive/master.zip
unzip master.zip
ll echo-nginx-module-master
nginx下载安装的教程,请查看我之前发的nginx安装教程
2、编译nginx并添加echo模块
cd $HOME/nginx-1.22.1
./configure --prefix=$HOME/nginx \
--sbin-path=$HOME/nginx/sbin/nginx \
--user=yiifung \
--group=yiifung \
--with-stream \
--with-http_ssl_module \
--with-http_stub_status_module \
--with-http_realip_module \
--with-pcre=$HOME/pcre2-10.39 \
--add-module=$HOME/nginx-1.22.1/mod/nginx_upstream_check_module-master/ \
--add-module=$HOME/echo-nginx-module-master/
make && make install
二、使用echo模块详解各种变量
在nginx.conf文件中做如下配置
location = /test {
set $hh 'hello world' ;
echo $hh ;
}
输出结果如下
[yiifung@localhost conf]$ curl http://localhost:8080/test
hello world
[yiifung@localhost conf]$
nginx 通过set指令来设置自定义变量,语法如下
Syntax: set $variable value;
Default: —
Context: server, location, if
$remote_addr 客户端的地址
$remote_port 客户端端口号
$remote_user 客户端认证用户名
location = /test {
set $hh 'hello world' ;
echo 'hostname $hostname';
echo 'remote_addr $remote_addr';
echo 'remote_port $remote_port';
}
[yiifung@localhost conf]$ curl http://localhost:8080/test
hostname localhost.localdomain
remote_addr 127.0.0.1
remote_port 48610
[yiifung@localhost conf]$