---
title: "写一个核心模块"
author: "deyimsf"
date: "2019-05-23T17:15:03Z"
tags: []
categories: ["开源软件"]
slug: "untitled"
draft: false
lastmod: "2019-05-23T17:15:03Z"
id: 2441219
url: "https://deyimsf.iteye.com/blog/2441219"
---

在[上一篇](https://mp.weixin.qq.com/s?__biz=MzAwOTcyNjE1Mg%3D%3D&mid=2247483842&idx=1&sn=95cfd4fc82ce729087d0106eb26beb02&chksm=9b5a7065ac2df97377aa9dc32a611c9b6a7c8c07acbb62bb73ade473728500ab677b4cbdfcfc&token=1833750538&lang=zh_CN&scene=21#wechat_redirect)中有提到目前ngx共有六种模块，分别是：

> NGX\_CORE\_MODULE
> 
> NGX\_CONF\_MODULE
> 
> NGX\_HTTP\_MODULE
> 
> NGX\_MAIL\_MODULE
> 
> NGX\_EVENT\_MODULE
> 
> NGX\_STREAM\_MODULE

如果按照一种更抽象的方式来划分，除了NGX\_CONF\_MODULE外，我们基本上可以把ngx模块类型划分为两种，一种是核心类型(NGX\_CORE\_MODULE)，另一种是非核心类型。

 

核心类型模块主要用来搭建ngx基础功能和为非核心类型模块起支撑作用，比如核心模块src/http/ngx\_http.c的主要作用就是支撑(或管理)所有http模块可以正常工作。非核心类型模块主要完成各自的业务功能，比如ngx\_http\_proxy\_module主要完成反向代理工作，而ngx\_http\_gzip\_module模块则完成数据压缩。

 

不管是核心模块还是非核心模块，他们在整体的架构实现上差别其实并不大，最大的差别基本上有两个方面，一个是指令集的实现(各个模块都有自己的指令集)，另一个是上下文的实现(ngx\_module\_t-&gt;ctx字段)。基本上每种模块类型都会有一个特定的上下定义，上下文一般限定了该类型模块在文件解析过程中应该做的事(或能够做的事)；指令集则描述了某个具体模块都有哪些功能，比如ngx\_http\_rewrite\_module模块中的return指令可以直接返回数据。

 

正常情况下，在我们的实际业务场景中，编写一个非核心模块，要比编写一个核心模块的几率大的多得多，不过为了让读者对模块编写有一个更清晰的认识，我们循序渐进，这篇先实现一个核心自定义模块，等下篇再实现一个非核心自定义模块。

 

**1**开始之前

 

[上篇](https://mp.weixin.qq.com/s?__biz=MzAwOTcyNjE1Mg%3D%3D&mid=2247483842&idx=1&sn=95cfd4fc82ce729087d0106eb26beb02&chksm=9b5a7065ac2df97377aa9dc32a611c9b6a7c8c07acbb62bb73ade473728500ab677b4cbdfcfc&token=1833750538&lang=zh_CN&scene=21#wechat_redirect)有说过，要扩展一个模块，一般需要约定三种数据，一种是ngx\_modue\_t.ctx字段，另一种是ngx\_modue\_t.type字段，还有一个是该模块提供的指令集commands\[](元素类型为ngx\_command\_t)。

 

对于以上三种数据，因为我们明确指出是要实现一个核心模块，所以type的值就是NGX\_CORE\_MODULE，其它两种则需要根据我们模块要完成的功能而定。

 

编写这个核心模块的主要目的是让读者熟悉模块的工作机制，所以并不会有任何特殊的业务功能，假设这个模块有如下两个指令：

> my\_core\_simple xxx …;
> 
> my\_core\_block { // anything }

第一个条指令可以接收任意参数，作用是指令解析时打印出指令后的参数；第二个指令的参数是一个参数块，在它里面可以写任意多个参数，比如这样:

> my\_core\_block {
> 
>     aaa 111 222 333;
> 
>     bbb 122 33;
> 
> }

它的作用是在指令解析时打印出内部参数。

 

**2**约定上下文

 

核心模块在ngx中的上下文定义是这样的:

> typedef struct {
> 
>     ngx\_str\_t            name;
> 
>     void               \*(\*create\_conf)(ngx\_cycle\_t \*cycle);
> 
>     char               \*(\*init\_conf)(ngx\_cycle\_t \*cycle, void \*conf);
> 
> } ngx\_core\_module\_t;

我们对它的实现如下：

> static ngx\_core\_module\_t  ngx\_my\_core\_module\_ctx = {
> 
>     ngx\_string("my\_core"),
> 
>     ngx\_my\_core\_create\_conf,
> 
>     ngx\_my\_core\_init\_conf
> 
> };

其中“my\_core”表示这个核心模块的名字，随后两个方法则是对\*(\*create\_conf)和\*(\*ini\_conf)方法的实现，他们的具体实现分别如下：

> static void*
> 
> ngx\_my\_core\_create\_conf(ngx\_cycle\_t \*cycle)
> 
> {
> 
>   // 向标准输出打印数据
> 
>   ngx\_log\_stderr(0, "start runing ngx\_my\_core\_create\_conf()");
> 
>  
> 
>   // ngx要求该方法不能返回空，否则启动失败
> 
>   //我们本例不需要这个数据，随便返回一个
> 
>   return cycle;
> 
> }

> static char *
> 
> ngx\_my\_core\_init\_conf(ngx\_cycle\_t \*cycle, void \*conf)
> 
> {
> 
>   // 向标准输出打印数据
> 
>   ngx\_log\_stderr(0, "start runing ngx\_my\_core\_init\_conf()");
> 
>   return NULL;
> 
> }

第一个方法在开始解析配置文件前执行，该方法一般用来创建一个自定义的结构体，你可以用这个结构体存放一些需要在运行时用到的信息(比如某个指令的配置信息)；第二个方法在配置文件解析完毕后执行，可以在这里做一些初始化之类的工作。我们这里实现的及其简单，就是打印了各自的方法名。

 

**3**约定指令集

 

在ngx中每条指令都会用一个结构体来表示，这个结构体的具体定义如下:

> typedef struct ngx\_command\_s ngx\_command\_t;
> 
> struct ngx\_command\_s {
> 
>   ngx\_str\_t   name;
> 
>   ngx\_uint\_t  type;
> 
>  char  \*(\*set)(ngx\_conf\_t \*cf, ngx\_command\_t \*cmd, void \*conf);
> 
>   ngx\_uint\_t  conf;
> 
>   ngx\_uint\_t  offset;
> 
>   void       \*post;
> 
> };

- name表示指令的名字。
- type用来指定该指令可以出现的位置，以及它可以携带介个参数等
- \*(\*set) 表示一个函数指针(用来存放函数)，当ngx解析到该指令后就会回调这个方法。
- 其它的我们这次用不到，等后续用到再说。

 

我们的模块共有两条指令，用数组来存放他们，具体如下：

> static ngx\_command\_t  ngx\_my\_core\_commands \[] = {
> 
>   { ngx\_string("my\_core\_simple"),
> 
>     NGX\_MAIN\_CONF| NGX\_CONF\_TAKE1,
> 
>     ngx\_my\_core\_simple,
> 
>     0,
> 
>     0,
> 
>     NULL },
> 
>  
> 
> { ngx\_string("my\_core\_block"),
> 
>    NGX\_MAIN\_CONF|NGX\_CONF\_BLOCK|NGX\_CONF\_NOARGS,
> 
>    ngx\_my\_core\_block,
> 
>    0,
> 
>    0,
> 
>    NULL },
> 
>  
> 
>    ngx\_null\_command
> 
> };

数组总共有三个元素，第一个表示my\_core\_simple指令的定义。

第二个是my\_core\_block指令的定义。

最后一个用来表示指令集结尾，是一个宏定义。

NGX\_MAIN\_CONF表示指令只能出现在主配置这一级别(不能在某个xxx{}块内)。

NGX\_CONF\_TAKE1表示指令只能带一个参数。

NGX\_CONF\_BLOCK表示该指令是一个块指令(比如xxx {})。

NGX\_CONF\_NOARGS表示该指令不能携带参数。

 

**4**实现指令的回调函数

 

每个指令对应的回调函数必须是如下形式：

> char  * xxx(ngx\_conf\_t \*cf, ngx\_command\_t \*cmd, void \*conf);

其中入参cf存放了一些当前解析数据的上下文，具体内容我们后续用到的时候再说。

cmd表示当前解析到的指令定义。

conf表示ngx\_my\_core\_create\_conf方法(在ngx\_my\_core\_module\_ctx中设置的)的返回值。

 

第一个指令对应的函数实现如下:

> static char * 
> 
> ngx\_my\_core\_simple(ngx\_conf\_t \*cf, ngx\_command\_t \*cmd, void \*conf)
> 
> {
> 
>   ngx\_str\_t  \*name;
> 
>  
> 
>   // 取出指令名字
> 
>   name = cf-&gt;args-&gt;elts;
> 
>   
> 
>   // 向标准输出打印指令名字和指令值
> 
>  ngx\_log\_stderr(0, "find a directive name\[%s] value\[%s] method\[ngx\_my\_core\_simple]", name-&gt;data, name\[1].data);
> 
>  
> 
>   return NGX\_CONF\_OK;
> 
> }

其中cf-&gt;args-&gt;elts表示解析到的指令串值，是一个数组，比如解析到的指令串是：

> my\_core\_simple 111;

那么该数组的第一个值就是“my\_core\_simple”，第二个则是“111”。

 

第二个指令因为是一个块指令，实现上稍微复杂一点，具体如下:

> static char * 
> 
> ngx\_my\_core\_block(ngx\_conf\_t \*cf, ngx\_command\_t \*cmd, void \*conf)
> 
> {
> 
>   char        \*rv;
> 
>   ngx\_conf\_t   save;
> 
>   ngx\_str\_t  \*name;
> 
>  
> 
>   // 取出指令名字
> 
>   name = cf-&gt;args-&gt;elts;
> 
>  
> 
>   // 向标准输出打印指令名字和指令值
> 
> ngx\_log\_stderr(0, "find a directive block name\[%s] method\[ngx\_my\_core\_block]", name-&gt;data);
> 
>  
> 
>  // 开始解析my\_core\_block{}块内的数据
> 
>  save = \*cf;
> 
>  cf-&gt;handler = ngx\_my\_core\_block\_handler;
> 
>  cf-&gt;handler\_conf = conf;
> 
>    
> 
>  // 块内数据由该方法负责解析(ngx自带的方法)
> 
>  rv = ngx\_conf\_parse(cf, NULL);
> 
>    
> 
>  \*cf = save;
> 
>  
> 
>  return rv;
> 
> }

ngx在解析配置文件的时候，默认情况下，每当解析出一个“指令串”(ngx内部叫token)时，会把该串的第一个“单词”当成指令，然后用去模块中匹配指令定义，匹配成功后就会回调对应的指令方法。

 

指令串的定义是这样的，以“；”或“{”结尾的串，比如:

> aa 112 … ;
> 
> ss {
> 
> bb xx {

以上三种形式中的任何一种都算一个“指令串”，其中第一个“单词”(按空格分隔)就是指令名字。

 

不过这次我们没有使用ngx的默认行为来解析我们的自定义块指令，因为我们并没有打算把块内的数据当成指令，仅仅把它当成普通字符串，所以我们有了如下两行代码:

> cf-&gt;handler = ngx\_my\_core\_block\_handler;
> 
> cf-&gt;handler\_conf = conf;

第一行代替ngx的默认行为，这个方法的形式跟指令对应的回调函数一样，每当解析完我们自定义指令块内的一条“指令串”时就会调用这个方法。

第二个设置的值会传入ngx\_my\_core\_block\_handler方法中。

 

我们写一下ngx\_my\_core\_block\_handler方法的实现：

> static char * 
> 
> ngx\_my\_core\_block\_handler (ngx\_conf\_t \*cf, ngx\_command\_t \*cmd, void \*conf)
> 
> {
> 
>   ngx\_uint\_t  num;
> 
>   ngx\_str\_t   \*words;
> 
>  
> 
>   // 解析到的“指令串”中“单词”的个数
> 
>   num = cf-&gt;args-&gt;nelts;
> 
>  
> 
>   // 解析到的“指令串”
> 
>   words = cf-&gt;args-&gt;elts;
> 
>  
> 
>   // 向标准输出打印解析到的信息
> 
>   ngx\_log\_stderr(0, "find a directive string, num of words\[%ui] first word\[%s] end word\[%s]", num, words\[0].data, words\[num-1].data);
> 
>  
> 
>    return NGX\_CONF\_OK;
> 
> }

偷了个懒，只打印了每个指令串的单词个数和第一个单词跟最后一个单词的名字。

 

另外因为我们在解析块内数据时修改了ngx的默认行为，所以在调用ngx\_conf\_parse()方法之前要先“保留现场”

> save = \*cf;

等解析完我们自定义的快内数据后再“还原现场”

> \*cf = save;

因为在解析其它ngx块指令的时候是不需要设置cf-&gt;handler的(比如解析http{}时用的就是默认行为)。

 

**5**组合三种约定好的数据

 

[上篇](https://mp.weixin.qq.com/s?__biz=MzAwOTcyNjE1Mg%3D%3D&mid=2247483842&idx=1&sn=95cfd4fc82ce729087d0106eb26beb02&chksm=9b5a7065ac2df97377aa9dc32a611c9b6a7c8c07acbb62bb73ade473728500ab677b4cbdfcfc&token=1833750538&lang=zh_CN&scene=21#wechat_redirect)文章曾经说过，不管哪种类型模块，都是用ngx\_module\_t表示的，约定数据都准备好后就需要用这个结构体来真正声明一个模块，具体如下:

> ngx\_module\_t  ngx\_my\_core\_module = {
> 
>     NGX\_MODULE\_V1,
> 
>     &ngx\_my\_core\_module\_ctx,          /* module context \*/
> 
>     ngx\_my\_core\_commands,            /* module directives \*/
> 
>     NGX\_CORE\_MODULE,               /* module type \*/
> 
>     NULL,                            /* init master \*/
> 
>     NULL,                            /* init module \*/
> 
>     NULL,                            /* init process \*/
> 
>     NULL,                            /* init thread \*/
> 
>     NULL,                            /* exit thread \*/
> 
>     NULL,                            /* exit process \*/
> 
>     NULL,                            /* exit master \*/
> 
>     NGX\_MODULE\_V1\_PADDING
> 
> };

组合好之后我们的自定义核心模块就算写好了，剩下的就是如何把他安装到ngx并运行了。

 

**6**注册并运行模块

 

模块写好之后就要向ngx中注册了，注册之前我们需要先把模块代码组织成如下结构：

> ngx\_my\_core\_module
> 
>    config
> 
>    ngx\_my\_core\_module.c

其中ngx\_my\_core\_module是一个目录；ngx\_my\_core\_module.c就是写好的源代码；config是一个文件，里面的数据用来描述要注册的模块信息。

 

我们的模块config文件有如下信息

> ngx\_addon\_name=ngx\_my\_core\_module 
> 
> CORE\_MODULES="$CORE\_MODULES ngx\_my\_core\_module" 
> 
> NGX\_ADDON\_SRCS="$NGX\_ADDON\_SRCS $ngx\_addon\_dir/ngx\_my\_core\_module.c"

第一行的信息在执行configure的时候会输出到控制台，可以随便写，但不推荐。

第二行是ngx内部要用的模块变量名，这个名字会放到/objs/ngx\_modules.c文件中，所以必须是模块声明的变量名(ngx\_my\_core\_module)。

第三行用来指定模块源码路径。

 

目录建好之后就可以进到ngx源码根目录中进行注册了，假设我们的模块和nginx源码都在/my目录下，则注册步骤如下：

> cd /my/nginx-1.9.4

执行configure命令:

> ./configure –prefix=/my/nginx --add-module=/my/ngx\_my\_core\_module

编译并安装

> make & make install

注册好了就可以“运行”模块了，不过在此之前我们先把自定义的指令都配置好，把如下指令配置nginx.conf中：

> my\_core\_simple 111;
> 
> my\_core\_block {
> 
>     aaa 222;
> 
>     bbb 111 222 333 444 555;
> 
> }

启动ngx后你就会在控制台看到如下信息:

> nginx: start runing ngx\_my\_core\_create\_conf()
> 
> nginx: find a directive name\[my\_core\_simple] value\[111] method\[ngx\_my\_core\_simple]
> 
> nginx: find a directive block name\[my\_core\_block] method\[ngx\_my\_core\_block]
> 
> nginx: find a directive string, num of words\[2] first word\[aaa] end word\[222]
> 
> nginx: find a directive string, num of words\[6] first word\[bbb] end word\[555]
> 
> nginx: start runing ngx\_my\_core\_init\_conf()

 

模块注册好了，并且已经可以“运行”了，只不过这个模块的“运行”就是在启动的时候打印一些信息，实际工作中基本没什么用，不过用来学习模块的编写基本够用了，有兴趣的读者可以试着模仿一个更实用的例子。

 

**7**附件

 

为了便于读者学习，这里贴出完整代码

> #include &lt;ngx\_config.h&gt;  
> 
> #include &lt;ngx\_core.h&gt;
> 
>  
> 
>  
> 
> static void * ngx\_my\_core\_create\_conf(ngx\_cycle\_t \*cycle);
> 
> static char * ngx\_my\_core\_init\_conf(ngx\_cycle\_t \*cycle, void \*conf);
> 
> static char * ngx\_my\_core\_simple(ngx\_conf\_t \*cf, ngx\_command\_t \*cmd, void \*conf);
> 
> static char * ngx\_my\_core\_block(ngx\_conf\_t \*cf, ngx\_command\_t \*cmd, void \*conf);
> 
> static char * ngx\_my\_core\_block\_handler (ngx\_conf\_t \*cf, ngx\_command\_t \*cmd, void \*conf);
> 
>  
> 
>  
> 
> static ngx\_command\_t  ngx\_my\_core\_commands\[] = {
> 
>    
> 
>     { ngx\_string("my\_core\_simple"),
> 
>       NGX\_MAIN\_CONF| NGX\_CONF\_TAKE1,
> 
>       ngx\_my\_core\_simple,
> 
>       0,
> 
>       0,
> 
>       NULL },
> 
>  
> 
>     { ngx\_string("my\_core\_block"),
> 
>       NGX\_MAIN\_CONF|NGX\_CONF\_BLOCK|NGX\_CONF\_NOARGS,
> 
>       ngx\_my\_core\_block,
> 
>       0,
> 
>       0,
> 
>       NULL },
> 
>  
> 
>     ngx\_null\_command
> 
> };
> 
>  
> 
>  
> 
> static ngx\_core\_module\_t  ngx\_my\_core\_module\_ctx = {
> 
>     ngx\_string("my\_core"),
> 
>     ngx\_my\_core\_create\_conf,
> 
>     ngx\_my\_core\_init\_conf
> 
> };
> 
>  
> 
>  
> 
> ngx\_module\_t  ngx\_my\_core\_module = {
> 
>     NGX\_MODULE\_V1,
> 
>     &ngx\_my\_core\_module\_ctx,         /* module context \*/
> 
>     ngx\_my\_core\_commands,            /* module directives \*/
> 
>     NGX\_CORE\_MODULE,                 /* module type \*/
> 
>     NULL,                            /* init master \*/
> 
>     NULL,                            /* init module \*/
> 
>     NULL,                            /* init process \*/
> 
>     NULL,                            /* init thread \*/
> 
>     NULL,                            /* exit thread \*/
> 
>     NULL,                            /* exit process \*/
> 
>     NULL,                            /* exit master \*/
> 
>     NGX\_MODULE\_V1\_PADDING
> 
> };
> 
>  
> 
>  
> 
> static void *
> 
> ngx\_my\_core\_create\_conf(ngx\_cycle\_t \*cycle)
> 
> {
> 
>    ngx\_log\_stderr(0, "start runing ngx\_my\_core\_create\_conf()");
> 
>  
> 
>    return cycle;
> 
> }
> 
>  
> 
>   
> 
> static char *
> 
> ngx\_my\_core\_init\_conf(ngx\_cycle\_t \*cycle, void \*conf)
> 
> {
> 
>    ngx\_log\_stderr(0, "start runing ngx\_my\_core\_init\_conf()");
> 
>    return NULL;
> 
> }
> 
>  
> 
>  
> 
> static char * 
> 
> ngx\_my\_core\_simple(ngx\_conf\_t \*cf, ngx\_command\_t \*cmd, void \*conf)
> 
> {
> 
>    ngx\_str\_t  \*name;
> 
>  
> 
>    name = cf-&gt;args-&gt;elts;
> 
> ngx\_log\_stderr(0, "find a directive name\[%s] value\[%s] method\[ngx\_my\_core\_simple]",name-&gt;data, name\[1].data);
> 
>  
> 
>    return NGX\_CONF\_OK;
> 
> }
> 
>  
> 
>  
> 
> static char * 
> 
> ngx\_my\_core\_block(ngx\_conf\_t \*cf, ngx\_command\_t \*cmd, void \*conf)
> 
> {
> 
>    char        \*rv;
> 
>    ngx\_conf\_t   save;
> 
>    ngx\_str\_t  \*name;
> 
>  
> 
>    name = cf-&gt;args-&gt;elts;
> 
>  
> 
>    ngx\_log\_stderr(0, "find a directive block name\[%s] method\[ngx\_my\_core\_block]",
> 
>         name-&gt;data);
> 
>  
> 
>    save = \*cf;
> 
>    cf-&gt;handler = ngx\_my\_core\_block\_handler;
> 
>    cf-&gt;handler\_conf = conf;
> 
>    
> 
>    rv = ngx\_conf\_parse(cf, NULL);
> 
>    
> 
>    \*cf = save;
> 
>  
> 
>    return rv;
> 
> }
> 
>  
> 
>  
> 
> static char * 
> 
> ngx\_my\_core\_block\_handler (ngx\_conf\_t \*cf, ngx\_command\_t \*cmd, void \*conf)
> 
> {
> 
>    ngx\_uint\_t  num;
> 
>    ngx\_str\_t   \*words;
> 
>  
> 
>    num = cf-&gt;args-&gt;nelts;
> 
>  
> 
>    words = cf-&gt;args-&gt;elts;
> 
>  
> 
>    ngx\_log\_stderr(0, "find a directive string, num of words\[%ui] first word\[%s] end word\[%s]",num, words\[0].data, words\[num-1].data);
> 
>  
> 
>    return NGX\_CONF\_OK;
> 
> }