Fork me on GitHub

nginx--第三方模块echo

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

开源的echo模块,实现输出变量等信息:
https://github.com/openresty/echo-nginx-module

  默认添加echo命令会提示错误

location /test {
        echo "hello word";
}
[15:00:57 root@localhost conf.d]#nginx -t
nginx: [emerg] "location" directive is not allowed here in /apps/nginx/conf.d/test.conf:2
nginx: configuration file /apps/nginx/conf/nginx.conf test failed

  一、通过git命令下载源码.

    1、安装git命令

yum install git -y

    2、

cd /usr/local/src

    3、通过clone 将github上的站点抓取下来

git clone https://github.com/openresty/echo-nginx-module.git

我这里抓取失败,在网上找到了其他办法,将https//xxxx改为git//xxxx

git clone git://github.com/openresty/echo-nginx-module.git

    4、下载nginx源码包

wget http://nginx.org/download/nginx-1.20.1.tar.gz

    5、创建nginx用户

useradd -r -s /sbin/nologin nginx

    6、安装依赖包

yum install gcc pcre-devel openssl-devel zlib-devel perl-ExtUtils-Embed -y

    7、解压缩nginx源码

tar xf nginx-1.20.1.tar.gz

    8、进入nginx目录中

cd nginx-1.20.1

    9、编译安装

./configure \
--prefix=/apps/nginx \
--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
make && make install

    10、修改nginx主配置文件

vim /apps/nginx/conf/nginx.conf
location /echo {
                default_type text/html;
                echo hello;
                echo $cookie_sessionid;
        }

在server字段添加一行,echo hello 是用来测试的

    11、启动nginx服务

sbin/nginx

    12、测试访问本机地址下的echo,是否成功,返回hello则成功

1 curl 192.168.1.5/echo
2 hello

    13、捕获cookie

1 curl -b sessionid=123456 192.168.1.5/echo
2 hello
3 123456

-b, --cookie STRING/FILE String or file to read cookies from (H)=读取cookie的字符串或文件(H)

 

posted @ 2021-06-10 10:59  Alex-Lzy  阅读(275)  评论(0编辑  收藏  举报