Linux 环境下已安装 nginx 添加扩展第三方动态模块
已经安装好的 nginx ,需要添加一个未被编译安装的模块,如何处理? nginx 的模块是需要重新编译 nginx 并引入该编译文件,而不是像 apache 一样配置文件引用 .so 。
这里以扩展第三方 echo
模块, echo
模块是在 nginx 程序上扩展了 echo 输出字符的功能, Nginx-echo 可以在 Nginx 中用来输出一些信息,是在测试排错过程中一个比较好的工具。它也可以做到把来自不同链接地址的信息进行一个汇总输出。可以说是学习 nginx 的必备神器。
echo
官方文档页:https://www.nginx.com/resources/wiki/modules/echo/
echo
官方项目地址释出页:https://github.com/openresty/echo-nginx-module/tags
- 下载
echo
扩展模块:
[centos@localhost ~]$ cd nginx-1.16.1/
[centos@localhost nginx-1.16.1]$ mkdir echo
[centos@localhost nginx-1.16.1]$ cd echo/
[centos@localhost echo]$ wget https://github.com/openresty/echo-nginx-module/archive/v0.62rc1.tar.gz
[centos@localhost echo]$ tar -zxvf v0.62rc1.tar.gz
扩展模块下载到任何位置都是可以的,但需要在 configure 的过程引用到解压地址。
- 查看 nginx 编译安装时安装的模块:
[centos@localhost echo]$ nginx -V
nginx version: nginx/1.16.1
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-39) (GCC)
built with OpenSSL 1.0.2k-fips 26 Jan 2017
TLS SNI support enabled
configure arguments: --prefix=/usr/local/nginx1.16.1 --sbin-path=/usr/sbin/nginx --conf-path=/etc/nginx/nginx.conf --user=nginx --group=nginx --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --with-select_module --with-poll_module --with-threads --with-file-aio --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_addition_module --with-http_xslt_module=dynamic --with-http_image_filter_module=dynamic --with-http_geoip_module=dynamic --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_auth_request_module --with-http_random_index_module --with-http_secure_link_module --with-http_degradation_module --with-http_slice_module --with-http_stub_status_module --with-mail=dynamic --with-mail_ssl_module --with-stream --with-stream_ssl_module --with-stream_realip_module --with-stream_geoip_module=dynamic --with-stream_ssl_preread_module --with-compat --with-debug
- 加入需要安装的模块,重新配置参数。
请注意
,需要源码。如果 nginx 源码已经缺失,请去官网重新下载相应版本的源码:
[centos@localhost echo]$ cd ..
[centos@localhost nginx-1.16.1]$ ./configure --add-dynamic-module=echo/echo-nginx-module-0.62rc1
请注意
,使用选项 --add-dynamic-module
动态编译已经在高版本中替代了静态编译 --add-module
,需要 nginx 版本大于 1.19.11 ,通过配置文件可以决定是否启用该模块 load_module=*.so
。
- 执行编译.
make
。如果要重新安装,执行安装make install
,否则,切记不要执行安装:
[centos@localhost nginx-1.16.1]$ make
- 替换 nginx 二进制文件:
[centos@localhost nginx-1.16.1]$ sudo cp /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx-bak
[centos@localhost nginx-1.16.1]$ sudo cp ./objs/nginx /usr/local/nginx/sbin/
注意
,如果是 --add-module
则不用修改文件直接重启即可,所有模块都会被加载。而如果是 --add-dynamic-module
则添加在 nginx.conf
中添加 load_module modules/ngx_http_echo_module.so
;表示启用该模块。如果不想启用,则不添加或加入注释符号 “#”。
- 如果 nginx 已经启动,请用 nginx -s stop 停止后,再启动或者别的重启 nginx 的方式,不要 nginx -s reload 重新加载配置文件,否则 echo 模块不生效。
- 检验:
[centos@localhost echo]$ nginx -V
会查看到 configure arguments: --add-dynamic-module=
参数的存在,其路径命中到 echo
文件路径。
echo 模块的基本使用:
hello world:
location /hello {
echo hello world;
echo "hello world";
echo $request_uri; ##输出请求路径uri
}
延迟后输出:
location /echo_with_sleep {
echo hello;
echo_sleep; 2.5; # in sec
echo world;
}
重复输出:
location /duphello {
echo_duplicate 1000; 'hello';
}
处理请求前后加入内容:
location = /api/proxy_before_after {
echo_before_body before;
proxy_pass http://127.0.0.1:8090;
echo_after_body after;
}
nginx 第三方扩展模块官方地址:https://www.nginx.com/resources/wiki/modules/
nginx 动态编译模块官方网站:Converting Static Modules to Dynamic Modules