nginx 动态添加模块

前言

1.9.11 版本前的 nginx 增加一个第三方模块,需要重新编译源代码,所有的模块都是用静态链接的形式组织起来的。而 Tengine 可以实现运行时动态加载模块,而不用每次都要重新编译Tengine。
Nginx 官方发布的 Nginx-1.9.11 版本,增加了此功能
openresty 的 1.9.15.1 版本增加了此功能

今天我将代码也更新到了 1.9.15.1 版本,试着动态加载自己编写的第三方模块

参考官方文档:

Converting Static Modules to Dynamic Modules

Old Config Shell File

New Config Shell File

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

要考虑nginx版本,也要考虑是否为动态模块

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

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

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

note

  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.

  2. 新旧模块的兼容

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

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

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

     #if defined(nginx_version) && nginx_version >= 1009011
         modules = cf->cycle->modules;
     #else
         modules = ngx_modules;
     #endif
    
  • 深入研究 OpenWAF 之 nginx 配置

    名称此文档将详细描述 OpenWAF 的 nginx 配置文件 /etc/ngx_openwaf.conf 中每一项配置 以及接入规则(access_rule)与 nginx 配置的关联 Table of Contents twaf_...

    深入研究 OpenWAF 之 nginx 配置
  • test nginx lua

    安装test nginx git clone https://github.com/openresty/test-nginx.git cd test-nginx perl Makefile.PL make && ma...

    test nginx lua
  • ngx.re.match 正则表达式长度限制

    captures, err = ngx.re.match(subject, regex, options?, ctx?, res_table?) 今天用 ngx.re.match,结果返回nil,查看err发现,regex正则表达式过...

    ngx.re.match 正则表达式长度限制
  • nginx http2.0

    nginx 有关 http2.0 的配置 背景今天在 openresty 测试 http2.0 编译参数首先需要编译时指定 v2 模块,--with-http_v2_module 配置 listen 指定 http2 ssl_pro...

    nginx http2.0
  • utrace 安装

    debian 上安装 utrace 背景今天安装 nginx-systemtap-toolkit 时需要安装 utrace (内核版本为 debian3.2,无内嵌的 utrace) 安装下载 https://github.com/u...

    utrace 安装
  • openresty 各组件依赖

    openresty 各组件(modsecurity、zlib、openssl、pcre、lua、gd、tools等)安装。 openresty目前最新版本 1.9.15.1 支持动态加载模块 nginx 依赖openssl – ssl...

    openresty 各组件依赖
  • lua 的 string 函数引发的血案

    背景用 lua 的 string.gmatch 封装了一个字符串分割函数 string_split(str, sep)文件上传时,解析请求体,有时解析正常,有时卡住十几秒,并不报错,但解析失败 分析原因代码如下: function s...

    lua 的 string 函数引发的血案
  • elastalert

    前言最近想在 ELK 基础上,临时搭一个告警系统,这里介绍有关 elastalert 的安装及使用。 安装参考官方安装说明: git clone https://github.com/bitsensor/elastalert.git;...

    elastalert
  • ipset

    ipset 安装参考文档(待整理)ipset 官方文档ipset 7.1版本链接使用参考文档 简单的流程可以用这几条命令概括使用 ipset 和 iptables 进行 IP 封禁的流程 ipset create blacklist ...

    ipset
  • 藏独组织“野鸡”的挑衅

    前言2019 年 1 月 12 日,有人以“匿名者”名义通过 YouTube 发布视频,呼吁将于2月13日针对中国部分政府网站进行网络攻击,并于 1 月 16 日在 PastBin 发布 100 个被攻击单位网站列表。 追踪视频明确...

    藏独组织“野鸡”的挑衅