NGINX--初步变量详解

一、编译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]$

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值