Nginx--安装模块

一 安装系统自带模块

#进入安装目录
[root@localhost ~]# cd nginx-1.18.0/

#查看原来的编译选项 [root@localhost nginx-1.18.0]# nginx -V

#查看安装的模块 [root@localhost nginx
-1.18.0]# ./configure --help

#以http_v2_module为例 [root@localhost nginx
-1.18.0]# ./configure --with-http_v2_module +原来的编译选项

#编译 [root@localhost nginx
-1.18.0]# make

#拷贝原有的二进制文件 [root@localhost nginx
-1.18.0]# mv /usr/local/nginx/sbin/nginx{,.bak}

#将新生成的二进制文件拷贝至sbin目录下 [root@localhost nginx
-1.18.0]# mv objs/nginx /usr/local/nginx/sbin/

#重启服务 [root@localhost nginx-1.18.0]# nginx -s reopen

二 安装第三方模块

 以echo模块为例(echo模块可以用来输出一些信息,是在测试排错过程中一个比较好用的工具)

#下载模块
wget https://github.com/openresty/echo-nginx-module/archive/v0.61.tar.gz

#创建目录来专门存放第三方模块 mkdir /usr/local/nginx/modules

#将模块解压至目录 tar xvf v0.
61.tar.gz -C /usr/local/nginx/modules/ cd nginx-1.18.0/ nginx -V ./configure 原来的编译选项 --add-module=/usr/local/nginx/modules/echo-nginx-module-0.61/ make
mv /usr/local/nginx/sbin/nginx{,.bak}
mv objs/nginx /usr/local/nginx/sbin/
nginx -s reopen

三 echo模块用法

 1 echo   可输出字符或变量,自带换行符,支持转义字符

location = /test1 {
    echo "hello,world!";
}
location = /test2 {
    echo -n "hello,";
    echo "world!";
}
location = /test3 {
    echo "$remote_addr";
}

 2 echo_sleep 暂停,后面接数字,单位为秒

ocation = /timed_hello {
    echo_reset_timer;
    echo_sleep 2;
    echo "echo-sleep takes about $echo_timer_elapsed sec.";
}

 3 echo_after_body 页面前输出,页面后输出

location /echo {
    echo_before_body "begin";
    proxy_pass http://192.168.10.42/echo;
    echo_after_body "end";
}
# curl 127.0.0.1/echo/
begin
This is test page
end

 4 echo_duplicate 输出重复的字符,不带换行符,支持转义子符

location /dup {
    echo_duplicate 3 "--";
    echo_duplicate 1 " END ";
    echo_duplicate 3 "--";
    echo;
}
# curl 127.0.0.1/dup
------ END ------
原文地址:https://www.cnblogs.com/Xinenhui/p/14093860.html