nginx第三方模块echo和变量

第三方模块:echo

  第三模块是对nginx 的功能扩展,第三方模块需要在编译安装nginx 的时候使用参数--
add-module=PATH指定路径添加,有的模块是由公司的开发人员针对业务需求定制
开发的,有的模块是开源爱好者开发好之后上传到github进行开源的模块,nginx支持
第三方模块,需要重新编译源码才能支持
  开源的echo模块,实现输出变量等信息:https://github.com/openresty/echo-nginx-module

编译安装版本:nginx1.14

 

1、准备:

[root@centos7.6 ~]# yum install git –y
[root@centos7.6 src]# cd /usr/local/src
获取echo包:
[root@centos7.6 src]# git clone https://github.com/openresty/echo-nginx-module.git
创建nginx用户:
[root@centos7.6 src]# useradd –r –s /sbin/nologin nginx
安装依赖:
[root@centos7.6 src]# yum install gcc pcre-devel openssl-devel zlib-devel perl-ExtUtils-Embed

2、编译安装:

[root@centos7.6 src]# cd nginx-1.14.0/
./configure \
--prefix=/apps/nginx14 \
--user=nginx --group=nginx \
--with-http_ssl_module \
--with-http_v2_module \
--with-http_realip_module \
--with-http_stub_status_module \
--with-http_gzip_static_module \
--with-http_perl_module \
--with-pcre \
--with-stream \
--with-stream_ssl_module \
--with-stream_realip_module \
--add-module=/usr/local/src/echo-nginx-module-master        #指定echo包路径

3、测试:

3.1、echo输出字符串:

[root@centos7.6 conf.d]# vi /apps/nginx4/conf/conf.d/test.conf
server {
        server_name www.magedu.org;
        root /data/site14/;

        location /echo {                  #echo目录不用创建
        echo -n "hello,";
        echo "world";
        default_type text/html;
        }
}
web:http://www.magedu.org/echo

3.2、自定义变量echo输出: 

[root@centos7.6 conf.d]# vi /apps/nginx4/conf/conf.d/test.conf
server {
        server_name www.magedu.org;
        root /data/site14/;

        location /echo {
        set $opt "HELLO,";
        echo -n  $opt;
        echo "world";
        default_type text/html;
        }
}

3.3、echo输出内置变量:

  nginx的变量可以在配置文件中引用,作为功能判断或者日志等场景使用,
变量可以分为内置变量和自定义变量,内置变量是由nginx模块自带,通过
变量可以获取到众多的与客户端访问相关的值 

常见内置变量:

$http_cookie; #客户端的cookie信息
$cookie_name; #表示key为 name 的cookie值
$limit_rate; #如果nginx服务器使用limit_rate配置了显示网络速率,则会显示,如果没有设置,则显示0 

$request_body_file; #做反向代理时发给后端服务器的本地资源的名称
$request_method; #请求资源的方式,GET/PUT/DELETE等


$request_uri; #包含请求参数的原始URI,不包含主机名如:main/index.do?id=090&partner=search。 
$document_uri; #保存了当前请求中不包含指令的URI,注意是不包含请求的指令,如http://www.magedu.net/main/index.do?id=090&partner=search
会被定义为/main/index.do
$args; #变量中存放了URL中的指令http://www.magedu.net/main/index.do?id=090&partner=search以上:id=090&partner=search 即为 $args

$document_root; #保存了针对当前资源的请求的系统根目录,如/apps/nginx/html
$request_filename; #当前请求的资源文件的路径名称,由root或alias指令与URI请求生成的文件绝对路径,如/apps/nginx/html/main/index.html

$scheme; #请求的协议,如ftp,https,http等 
$server_protocol; #请求资源的协议版本,如HTTP/.0,HTTP/.,HTTP/.0等 


$server_name; #请求的服务器的主机名 
$server_port; #请求的服务器的端口
$server_addr; #保存了服务器的IP地址
$remote_port; #客户端请求Nginx服务器时客户端随机打开的端口
$remote_user; #已经经过Auth Basic Module验证的用户名
$remote_addr; #存放了客户端的地址,注意是客户端的公网IP

$host; #存放了请求的host名称
$http_user_agent; #客户端浏览器的详细信息

[root@centos7.6 conf.d]# vi /apps/nginx4/conf/conf.d/test.conf
server {
        server_name www.magedu.org;
        root /data/site14/;

        location /echo {
        #set $opt "hello,";
        echo hello;
        echo world;

        echo 'host:' $host;
        echo 'remote_addr:' $remote_addr
        echo 'args:' $args;
        echo 'document_root' $document_root;
        echo 'http_user_agent' $http_user_agent;
        echo ;
        default_type text/html;
        }
}

 

posted @ 2024-08-29 09:04  起点·漫步前行  阅读(234)  评论(0)    收藏  举报