Nginx 增加一個(gè)第三方模塊,需要重新編譯源代碼,所有的模塊都是用靜態(tài)鏈接的形式組織起來的。而 Tengine 可以實(shí)現(xiàn)運(yùn)行時(shí)動(dòng)態(tài)加載模塊,而不用每次都要重新編譯Tengine。
Nginx 官方發(fā)布的 Nginx-1.9.11 版本,也增加了該功能。?詳看 http://nginx.org/en/CHANGES?
openresty 的 1.9.15.1 版本也增加了該功能。詳看 http://openresty.org/cn/changelog-1009015.html?
今天將代碼也更新到了1.9.15.1版本,試著動(dòng)態(tài)加載自己編寫的第三方模塊?
參考官方文檔:
Converting Static Modules to Dynamic Modules
1.Converting a config file?
?old-config:
ngx_addon_name=ngx_http_xxx_filter_module
HTTP_AUX_FILTER_MODULES="$HTTP_AUX_FILTER_MODULES\
ngx_http_xxx_filter_module"
NGX_ADDON_SRCS="$NGX_ADDON_SRCS \
$ngx_addon_dir/ngx_http_xxx_filter_module.c"
?new-config:
?ngx_addon_name=ngx_http_xxx_filter_module
#nginx version >= 1.9.11
if test -n "$ngx_module_link"; then
if [ $ngx_module_link = ADDON ] ; then
ngx_module_type=HTTP_AUX_FILTER
fi
ngx_module_name=ngx_http_xxx_filter_module
? ngx_module_srcs="$ngx_addon_dir/ngx_http_xxx_filter_module.c"
. auto/module
#nginx version < 1.9.11
else
HTTP_AUX_FILTER_MODULES="$HTTP_AUX_FILTER_MODULES \
ngx_http_xxx_filter_module"
NGX_ADDON_SRCS="$NGX_ADDON_SRCS \
$ngx_addon_dir/ngx_http_xxx_filter_module.c"
fi
?ps: 要考慮nginx版本,也要考慮是否為動(dòng)態(tài)模塊
2. Compiling a Dynamic Module?
A new configure option has been created to add a module as a dynamic module. Instead of using--add-module you use --add-dynamic-module. For example:
./configure --add-dynamic-module=/opt/source/ngx_http_xxx_filter_module/?
Modules are compiled along with NGINX by running the make command. Alternatively you can ask NGINX to just build the modules by doing:
$ make -f objs/Makefile modules
With NGINX 1.9.13 the following is another way of doing this:
$ make modules
?3. Loading a Dynamic Module
Modules can be loaded into NGINX with the new load_module directive.
For example:(main級(jí)別)
load_module modules/ngx_http_xxx_filter_module.so;
ps: 雖說是main級(jí)別的,但是要其他塊之上,如:
user root root;
worker_processes 2;
pid /var/run/nginx.pid;
load_module modules/ngx_http_xxx_filter_module.so;
events {
...?
}?
http {
...?
}?
若load_module用在events與http之間,則會(huì)?提示:
"load_module" directive is specified too late?
4. note
4.1 There is a hard limit of 128 dynamic modules that can be loaded at one time, as defined by NGX_MAX_DYNAMIC_MODULES in the NGINX source. This hard limit can be increased by editing this constant.
4.2 新舊模塊的兼容
上面已經(jīng)提到了新舊模塊編譯腳本的兼容
同時(shí)也要考慮c語(yǔ)言層面的兼容,要關(guān)注動(dòng)態(tài)模塊對(duì)于 Nginx 框架的調(diào)整
例如之前凡是用到全局變量 ngx_modules 的地方,要修改為 cycle->modules?
#if defined(nginx_version) && nginx_version >= 1009011
modules = cf->cycle->modules;
#else
modules = ngx_modules;
#endif??
更多建議: