解剖Nginx·模块开发篇(2)ngx_http_hello_world_module 模块基本结构定义

本文详细介绍了Nginx模块开发中的ngx_http_hello_world_module的基本结构,包括模块定义、命令定义及其相关参数,如NGX_MODULE_V1、模块类型和命令回调函数等。同时,提到了HTTP模块的上下文定义及其对应的配置处理函数。
摘要由CSDN通过智能技术生成

解剖Nginx·模块开发篇(2)ngx_http_hello_world_module 基本结构定义

  • 作者:柳大·Poechant(钟超)
  • 邮箱:zhongchao.ustc#gmail.com(# -> @)
  • 博客:Blog.CSDN.net/Poechant
  • 日期:June 2nd, 2012

HelloWorld 是一个典型的 location 模块。什么是 location 模块?在 Nginx 中,根据作用域,有 main 模块、server 模块、location 模块。

1 模块定义

在 HelloWorld 模块中有一个 ngx_http_hello_world_module 变量,用于定义模块。它是 ngx_module_t 类型。ngx_module_t 是 ngx_module_s 的别名,其定义如下:

struct ngx_module_s {
    ngx_uint_t            ctx_index; 
    ngx_uint_t            index; 

    ngx_uint_t            spare0;
    ngx_uint_t            spare1;
    ngx_uint_t            spare2;
    ngx_uint_t            spare3;

    ngx_uint_t            version; // Nginx模块版本

    void                 *ctx; // 上下文定义的地址
    ngx_command_t        *commands; // 命令定义地址
    ngx_uint_t            type; // 模块类型

    ngx_int_t           (*init_master)(ngx_log_t *log); // 初始化 master 时执行

    ngx_int_t           (*init_module)(ngx_cycle_t *cycle); // 初始化模块时执行

    ngx_int_t           (*init_process)(ngx_cycle_t *cycle); // 初始化进程时执行
    ngx_int_t           (*init_thread)(ngx_cycle_t *cycle); // 初始化线程时执行
    void                (*exit_thread)(ngx_cycle_t *cycle); // 退出线程时执行
    void                (*exit_process)(ngx_cycle_t *cycle); // 退出进程时执行

    void                (*exit_master)(ngx_cycle_t *cycle); // 退出 master 时执行

    uintptr_t             spare_hook0;
    uintptr_t             spare_hook1;
    uintptr_t             spare_hook2;
    uintptr_t             spare_hook3;
    uintptr_t             spare_hook4;
    uintptr_t             spare_hook5;
    uintptr_t             spare_hook6;
    uintptr_t             spare_hook7;
};

在 HelloWorld 例子中:

// Structure for the HelloWorld module, the most important thing
ngx_module_t ngx_http_hello_world_module = {
    NGX_MODULE_V1,
    &ngx_http_hello_world_module_ctx,
    ngx_http_hello_world_commands,
    NGX_HTTP_MODULE,
    NULL,
    NULL,
    NULL,
    NULL,
    NULL,
    NULL,
    NULL,
    NGX_MODULE_V1_PADDING
};

1.1 NGX_MODULE_V1

看它的定义你就知道,它是用来填充前 7 个 fields 的。

NGX_MODULE_V1          0, 0, 0, 0, 0, 0, 1

1.2 模块类型

我们的模块是 HTTP 模块,还可以开发 CORE 模块,或者 CONF 模块等等。

  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

钟超

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值