codecamp

nginx add dynamic module

    Nginx 增加一个第三方模块,需要重新编译源代码,所有的模块都是用静态链接的形式组织起来的。而 Tengine 可以实现运行时动态加载模块,而不用每次都要重新编译Tengine。

    Nginx 官方发布的 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版本,试着动态加载自己编写的第三方模块​

    参考官方文档:

    Converting Static Modules to Dynamic Modules

    Old Config Shell File

    New Config Shell File


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版本,也要考虑是否为动态模块


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级别)

     load_module modules/ngx_http_xxx_filter_module.so;

     ps: 虽说是main级别的,但是要其他块之上,如:

    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之间,则会​提示:

        "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 新旧模块的兼容

        上面已经提到了新旧模块编译脚本的兼容

        同时也要考虑c语言层面的兼容,要关注动态模块对于 Nginx 框架的调整

        例如之前凡是用到全局变量 ngx_modules 的地方,要修改为 cycle->modules​

    #if defined(nginx_version) && nginx_version >= 1009011
        modules = cf->cycle->modules;
    #else
        modules = ngx_modules;
    #endif​​


压缩与解压缩
磁盘空间不足提示 No space left on device
温馨提示
下载编程狮App,免费阅读超1000+编程语言教程
取消
确定
目录

关闭

MIP.setData({ 'pageTheme' : getCookie('pageTheme') || {'day':true, 'night':false}, 'pageFontSize' : getCookie('pageFontSize') || 20 }); MIP.watch('pageTheme', function(newValue){ setCookie('pageTheme', JSON.stringify(newValue)) }); MIP.watch('pageFontSize', function(newValue){ setCookie('pageFontSize', newValue) }); function setCookie(name, value){ var days = 1; var exp = new Date(); exp.setTime(exp.getTime() + days*24*60*60*1000); document.cookie = name + '=' + value + ';expires=' + exp.toUTCString(); } function getCookie(name){ var reg = new RegExp('(^| )' + name + '=([^;]*)(;|$)'); return document.cookie.match(reg) ? JSON.parse(document.cookie.match(reg)[2]) : null; }