Dynamic modules – NGINX 1.11.5 was a milestone moment in the development of NGINX. It introduced
the new --with-compat option which allows any module to be compiled and dynamically loaded into a
running NGINX instance of the same version (and an NGINX Plus release based on that version).
There are over 120 modules for NGINX contributed by the open source community, and now you can load
them into our NGINX builds, or those of an OS vendor, without having to compile NGINX from source.
For more information on compiling dynamic modules, see Compiling Dynamic Modules for NGINX Plus.
下面,以动态加载模块 lua-nginx-module 为例,展示具体用法。
下载 Nginx 安装包
wget http://nginx.org/download/nginx-1.11.5.tar.gz
tar -xzvf nginx-1.11.5.tar.gz
nginx-1.9.11 开始,支持动态加载源码模块或第三方模块,需要先在编译 Nginx (./configure)时指定:
./configure --with-mail=dynamic ...
./configure --add-dynamic-module=...
模块对应的
.so
文件会被存放在/path/nginx/modules/
下面,当需要使用模块时,在nginx.conf
最顶端(main)配置 load_module,指定模块路径。但是,这时,添加模块仍然需要在编译阶段声明,即需要重新编译 Nginx 程序,很多时候,在生产环境是不能随便去更新替换二进制程序的。
因此,在 nginx-1.11.5 编译命令增加了
--with-compat
,可以单独编译需要新增的模块,直接动态加载到原有的 nginx 二进制程序,而不用重新编译 nginx! 官方阐述:下面,以动态加载模块
lua-nginx-module
为例,展示具体用法。安装必要组件
include
和lib
路径,编译的时候要用到。编译 Nginx ,记得带上
--with-compat
开启兼容模式安装 Nginx
下载
lua-nginx-module
源码包,及依赖ngx_devel_kit动态加载
.so
文件./configure --with-compat --with-cc-opt='-O0 -I /usr/local/include/luajit-2.0' \ --with-ld-opt='-Wl,-rpath,/usr/local/lib -lluajit-5.1' \ --add-dynamic-module=../lua-nginx-module-0.10.13 --add-dynamic-module=../ngx_devel_kit-0.3.0
make modules
cp objs/ndk_http_module.so objs/ngx_http_lua_module.so ../nginx-dy/nginx/modules/
load_module modules/ndk_http_module.so; load_module modules/ngx_http_lua_module.so;