---
title: "nginx源码阅读笔记"
author: "kenby"
date: "2011-09-10T00:16:43Z"
tags: ["nginx"]
categories: ["开源软件"]
slug: "nginx"
draft: false
lastmod: "2011-09-15T16:35:39Z"
id: 1169675
url: "https://kenby.iteye.com/blog/1169675"
---

最近做一个收集客户端数据的项目, 后台使用nginx, 通过实现nginx的模块来处理业务.  nginx的模块编写不难,

但写完后对nginx的内部机制还是云里雾里, 趁周末好好阅读一下nginx的源代码.  下面记录一些阅读过程中遇

到的数据结构. 关于nginx的内部实现, 等看懂了源码再写.

模块四要素:

1 模块实例, 2 模块上下文, 3 模块指令, 4 指令参数

模块定义

===================================================

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;

    void                 \*ctx;

    ngx\_command\_t        \*commands;

    ngx\_uint\_t            type;

    ngx\_int\_t           (\*init\_master)(ngx\_log\_t \*log);

    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);

    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;

};  (core/ngx\_conf\_file.h)

模块实例定义

===================================================

1 核心模块

ngx\_module\_t  ngx\_core\_module = {

    NGX\_MODULE\_V1,

    &ngx\_core\_module\_ctx,                  /* module context \*/

    ngx\_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

}; (core/nginx.c)

2 http模块

ngx\_module\_t  ngx\_http\_module = {

    NGX\_MODULE\_V1,

    &ngx\_http\_module\_ctx,                  /* module context \*/

    ngx\_http\_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

};

3 http核心模块

ngx\_module\_t  ngx\_http\_core\_module = {

    NGX\_MODULE\_V1,

    &ngx\_http\_core\_module\_ctx,             /* module context \*/

    ngx\_http\_core\_commands,                /* module directives \*/

    NGX\_HTTP\_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

}; (http/ngx\_http\_core\_module.c)

4 日志模块

ngx\_module\_t  ngx\_errlog\_module = {

    NGX\_MODULE\_V1,

    &ngx\_errlog\_module\_ctx,                /* module context \*/

    ngx\_errlog\_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

};

5 事件模块

ngx\_module\_t  ngx\_event\_core\_module = {

    NGX\_MODULE\_V1,

    &ngx\_event\_core\_module\_ctx,            /* module context \*/

    ngx\_event\_core\_commands,               /* module directives \*/

    NGX\_EVENT\_MODULE,                      /* module type \*/

    NULL,                                  /* init master \*/

    ngx\_event\_module\_init,                 /* init module \*/

    ngx\_event\_process\_init,                /* init process \*/

    NULL,                                  /* init thread \*/

    NULL,                                  /* exit thread \*/

    NULL,                                  /* exit process \*/

    NULL,                                  /* exit master \*/

    NGX\_MODULE\_V1\_PADDING

};

模块上下文定义 

===================================================

1 核心模块上下文定义

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;     （见src/core/ngx\_conf\_file.h）

2 事件模块上下文定义

typedef struct {

    ngx\_str\_t              \*name;

    void                 \*(\*create\_conf)(ngx\_cycle\_t \*cycle);

    char                 \*(\*init\_conf)(ngx\_cycle\_t \*cycle, void \*conf);

    ngx\_event\_actions\_t     actions;

} ngx\_event\_module\_t;（见src/event/ngx\_event.h）

3 http模块上下文定义

typedef struct {

    ngx\_int\_t   (\*preconfiguration)(ngx\_conf\_t \*cf);

    ngx\_int\_t   (\*postconfiguration)(ngx\_conf\_t \*cf);

    void       \*(\*create\_main\_conf)(ngx\_conf\_t \*cf);

    char       \*(\*init\_main\_conf)(ngx\_conf\_t \*cf, void \*conf);

    void       \*(\*create\_srv\_conf)(ngx\_conf\_t \*cf);

    char       \*(\*merge\_srv\_conf)(ngx\_conf\_t \*cf, void \*prev, void \*conf);

    void       \*(\*create\_loc\_conf)(ngx\_conf\_t \*cf);

    char       \*(\*merge\_loc\_conf)(ngx\_conf\_t \*cf, void \*prev, void \*conf);

} ngx\_http\_module\_t;  （见src/http/ngx\_http\_config.h）

4 事件模块上下文定义

ngx\_event\_module\_t  ngx\_event\_core\_module\_ctx = {

    &event\_core\_name,

    ngx\_event\_create\_conf,                 /* create configuration \*/

    ngx\_event\_init\_conf,                   /* init configuration \*/

    { NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL }

};

模块上下文实例

===============================================================

1 核心模块上下文

static ngx\_core\_module\_t  ngx\_core\_module\_ctx = {

    ngx\_string("core"),

    ngx\_core\_module\_create\_conf,

    ngx\_core\_module\_init\_conf

}; (core/nginx.c)

2 http核心模块上下文

static ngx\_http\_module\_t  ngx\_http\_core\_module\_ctx = {

    ngx\_http\_core\_preconfiguration,        /* preconfiguration \*/

    NULL,                                  /* postconfiguration \*/

    ngx\_http\_core\_create\_main\_conf,        /* create main configuration \*/

    ngx\_http\_core\_init\_main\_conf,          /* init main configuration \*/

    ngx\_http\_core\_create\_srv\_conf,         /* create server configuration \*/

    ngx\_http\_core\_merge\_srv\_conf,          /* merge server configuration \*/

    ngx\_http\_core\_create\_loc\_conf,         /* create location configuration \*/

    ngx\_http\_core\_merge\_loc\_conf           /* merge location configuration \*/

}; (http/ngx\_http\_core\_module.c)

存储指令参数的结构体

===============================================================

根指令的参数

typedef struct {

     ngx\_flag\_t               daemon;

     ngx\_flag\_t               master;

     ngx\_msec\_t               timer\_resolution;

     ngx\_int\_t                worker\_processes;

     ngx\_int\_t                debug\_points;

     ngx\_int\_t                rlimit\_nofile;

     ngx\_int\_t                rlimit\_sigpending;

     off\_t                    rlimit\_core;

     int                      priority;

     ngx\_uint\_t               cpu\_affinity\_n;

     u\_long                  \*cpu\_affinity;

     char                    \*username;

     ngx\_uid\_t                user;

     ngx\_gid\_t                group;

     ngx\_str\_t                working\_directory;

     ngx\_str\_t                lock\_file;

     ngx\_str\_t                pid;

     ngx\_str\_t                oldpid;

     ngx\_array\_t              env;

     char                   \*\*environment;

} ngx\_core\_conf\_t;

http指令的参数

typedef struct {

/* 保存所有server的配置信息 \*/

    ngx\_array\_t                servers;         /* ngx\_http\_core\_srv\_conf\_t \*/

    ngx\_http\_phase\_engine\_t    phase\_engine;

    ngx\_hash\_t                 headers\_in\_hash;

    ngx\_hash\_t                 variables\_hash;

    ngx\_array\_t                variables;       /* ngx\_http\_variable\_t \*/

    ngx\_uint\_t                 ncaptures;

    ngx\_uint\_t                 server\_names\_hash\_max\_size;

    ngx\_uint\_t                 server\_names\_hash\_bucket\_size;

    ngx\_uint\_t                 variables\_hash\_max\_size;

    ngx\_uint\_t                 variables\_hash\_bucket\_size;

    ngx\_hash\_keys\_arrays\_t    \*variables\_keys;

    ngx\_array\_t               \*ports;

    ngx\_uint\_t                 try\_files;       /* unsigned  try\_files:1 \*/

    ngx\_http\_phase\_t           phases\[NGX\_HTTP\_LOG\_PHASE + 1];

} ngx\_http\_core\_main\_conf\_t;

server指令的参数

typedef struct {

    /* array of the ngx\_http\_server\_name\_t, "server\_name" directive \*/

    ngx\_array\_t                 server\_names;

    /* server ctx,初始化http请求ngx\_http\_init\_request时用得到 \*/

    ngx\_http\_conf\_ctx\_t        \*ctx;

    ngx\_str\_t                   server\_name;

    size\_t                      connection\_pool\_size;

    size\_t                      request\_pool\_size;

    size\_t                      client\_header\_buffer\_size;

    ngx\_bufs\_t                  large\_client\_header\_buffers;

    ngx\_msec\_t                  client\_header\_timeout;

    ngx\_flag\_t                  ignore\_invalid\_headers;

    ngx\_flag\_t                  merge\_slashes;

    ngx\_flag\_t                  underscores\_in\_headers;

    unsigned                    listen:1;

    ngx\_http\_core\_loc\_conf\_t  \*\*named\_locations;

} ngx\_http\_core\_srv\_conf\_t;

location指令的参数

struct ngx\_http\_core\_loc\_conf\_s {

    ngx\_str\_t     name;          /* location name \*/

    unsigned      noname:1;   /* "if () {}" block or limit\_except \*/

    unsigned      lmt\_excpt:1;

    unsigned      named:1;

    unsigned      exact\_match:1;

    unsigned      noregex:1;

    unsigned      auto\_redirect:1;

    /* pointer to the modules' loc\_conf \*/

    void        \*\*loc\_conf;

    uint32\_t      limit\_except;

    void        \*\*limit\_except\_loc\_conf;

    ngx\_http\_handler\_pt  handler;

    /* location name length for inclusive location with inherited alias \*/

    size\_t        alias;

    ngx\_str\_t     root;                    /* root, alias \*/

    ngx\_str\_t     post\_action;

    ngx\_array\_t  \*root\_lengths;

    ngx\_array\_t  \*root\_values;

    ngx\_array\_t  \*types;

    ngx\_hash\_t    types\_hash;

    ngx\_str\_t     default\_type;

    off\_t         client\_max\_body\_size;    /* client\_max\_body\_size \*/

    off\_t         directio;                /* directio \*/

    off\_t         directio\_alignment;      /* directio\_alignment \*/

    size\_t        client\_body\_buffer\_size; /* client\_body\_buffer\_size \*/

    size\_t        send\_lowat;              /* send\_lowat \*/

    size\_t        postpone\_output;         /* postpone\_output \*/

    size\_t        limit\_rate;              /* limit\_rate \*/

    size\_t        limit\_rate\_after;        /* limit\_rate\_after \*/

    size\_t        sendfile\_max\_chunk;      /* sendfile\_max\_chunk \*/

    size\_t        read\_ahead;              /* read\_ahead \*/

    ngx\_msec\_t    client\_body\_timeout;     /* client\_body\_timeout \*/

    ngx\_msec\_t    send\_timeout;            /* send\_timeout \*/

    ngx\_msec\_t    keepalive\_timeout;       /* keepalive\_timeout \*/

    ngx\_msec\_t    lingering\_time;          /* lingering\_time \*/

    ngx\_msec\_t    lingering\_timeout;       /* lingering\_timeout \*/

    ngx\_msec\_t    resolver\_timeout;        /* resolver\_timeout \*/

    ngx\_resolver\_t  \*resolver;             /* resolver \*/

    time\_t        keepalive\_header;        /* keepalive\_timeout \*/

};

指令定义

===========================================================

struct ngx\_command\_s {

    ngx\_str\_t             name;

    ngx\_uint\_t            type;初始化http请求: ngx\_http\_init\_request    

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

    ngx\_uint\_t            conf;

    ngx\_uint\_t            offset;

    void                 \*post;

};

cycle定义

===========================================================

struct ngx\_cycle\_s {

    void                  \*\*\*\*conf\_ctx;

    ngx\_pool\_t               \*pool;

    ngx\_log\_t                \*log;

    ngx\_log\_t                 new\_log;

    ngx\_connection\_t        \*\*files;

    ngx\_connection\_t         \*free\_connections;

    ngx\_uint\_t                free\_connection\_n;

    ngx\_queue\_t               reusable\_connections\_queue;

    ngx\_array\_t               listening;

    ngx\_array\_t               pathes;

    ngx\_list\_t                open\_files;

    ngx\_list\_t                shared\_memory;

    ngx\_uint\_t                connection\_n;

    ngx\_uint\_t                files\_n;

    ngx\_connection\_t         \*connections;

    ngx\_event\_t              \*read\_events;

    ngx\_event\_t              \*write\_events;

    ngx\_cycle\_t              \*old\_cycle;

    ngx\_str\_t                 conf\_file;

    ngx\_str\_t                 conf\_param;

    ngx\_str\_t                 conf\_prefix;

    ngx\_str\_t                 prefix;

    ngx\_str\_t                 lock\_file;

    ngx\_str\_t                 hostname;

};

http三大配置

===========================================================

typedef struct {

    void        \*\*main\_conf;

    void        \*\*srv\_conf;

    void        \*\*loc\_conf;

} ngx\_http\_conf\_ctx\_t;

解析配置文件的时候, 用结构体ngx\_conf\_s来暂时存放指令的参数

===========================================================

struct ngx\_conf\_s {

    char                 \*name;

    ngx\_array\_t          \*args;

    ngx\_cycle\_t          \*cycle;

    ngx\_pool\_t           \*pool;

    ngx\_pool\_t           \*temp\_pool;

    ngx\_conf\_file\_t      \*conf\_file;

    ngx\_log\_t            \*log;

    void                 \*ctx;

    ngx\_uint\_t            module\_type;

    ngx\_uint\_t            cmd\_type;

    ngx\_conf\_handler\_pt   handler;

    char                 \*handler\_conf;

};

在ngx\_init\_cycle中声明一个这样的变量

ngx\_conf\_t conf;

然后开始解析配置文件, 这个结构体可以反复使用, 每次遇到一个指令, 就会改变conf的内容

阅读痕迹

=====================================================================

\* ngx\_init\_cycle:

    * ngx\_conf\_t.ctx = cycle-&gt;conf\_ctx;

解析配置文件

\* 从ngx\_init\_cycle调用ngx\_conf\_param开始解析配置文件

\* ngx\_conf\_param再调用ngx\_conf\_parse解析配置文件

\* ngx\_conf\_parse:

    * 打开配置文件

    * for循环:

        * 读取文件, ngx\_conf\_read\_token, 把指令名和参数存到ngx\_conf\_t结构体,

          每次都一条指令便返回

        * 调用ngx\_conf\_handler来处理这条指令:

            * 寻找指令所在的模块

            * 检验指令在配置文件中的位置是否正确

            * 检验指令的参数个数是否合法

            * 取得指令参数要存储的地方(比如ngx\_core\_conf\_t,用conf指针指向),这个结构体由

              模块的create\_conf来创建, 然后把它安插在 ngx\_conf\_t.ctx 中

            * 调用指令的 set 函数, 把ngx\_conf\_t中的参数值转存到conf指向的结构体

解析http block

如果ngx\_conf\_read\_token返回"http {", 则调用ngx\_http\_block解析http block下面的配置

\* ngx\_http\_block:

    * 创建ngx\_http\_conf\_ctx\_t结构体, 这时conf指针不再指向ngx\_core\_conf\_t, 

      而是指向ngx\_http\_conf\_ctx\_t

    * 数一下有多少个http模块, 并设置每个模块的index

    * 调用所有http模块的create\_main\_conf钩子,把返回的结构体安插在ngx\_http\_conf\_ctx\_t.main\_conf表中

      调用所有http模块的create\_srv\_conf钩子,把返回的结构体安插在ngx\_http\_conf\_ctx\_t.srv\_conf表中

      调用所有http模块的create\_loc\_conf钩子,把返回的结构体安插在ngx\_http\_conf\_ctx\_t.loc\_conf表中

    * ngx\_conf\_t.ctx不再指向cycle-&gt;conf\_ctx, 而是指向ngx\_http\_conf\_ctx\_t 

    * 调用所有http模块的preconfiguration钩子

    * 再次调用ngx\_conf\_parse来解析http block里面的指令, 至此http block, 

      server block, location block都解析完毕

    * 调用所有http模块的init\_main\_conf钩子 

    * 待续...

解析server block

解析htto block的时候, 如果遇到"server {", 则调用

server command的set方法:ngx\_http\_core\_server, 开始解析server block

ngx\_http\_core\_server:

    * 再创建一个ngx\_http\_conf\_ctx\_t结构体, 这时ngx\_conf\_t.ctx不再指向

      http block的ngx\_http\_conf\_ctx\_t, 而是指向这个新的ngx\_http\_conf\_ctx\_t

      ctx = new ngx\_http\_conf\_ctx\_t

      http\_ctx = cf-&gt;ctx

      cf-&gt;ctx = ctx

    * server ctx的main\_conf从http ctx继承过来

      ctx-&gt;main\_conf = http\_ctx-&gt;main\_conf;

    * 调用所有http模块的create\_srv\_conf钩子,把返回的结构体安插在ctx.srv\_conf表中

      调用所有http模块的create\_loc\_conf钩子,把返回的结构体安插在ctx.loc\_conf表中

    

http block下面三大conf的偏移量

\-----------------------------------------------------------

#define NGX\_HTTP\_MAIN\_CONF\_OFFSET  offsetof(ngx\_http\_conf\_ctx\_t, main\_conf)

#define NGX\_HTTP\_SRV\_CONF\_OFFSET   offsetof(ngx\_http\_conf\_ctx\_t, srv\_conf)

#define NGX\_HTTP\_LOC\_CONF\_OFFSET   offsetof(ngx\_http\_conf\_ctx\_t, loc\_conf)

根据ngx\_http\_request\_t得到三大conf

\-----------------------------------------------------------

#define ngx\_http\_get\_module\_main\_conf(r, module)                             \\

    (r)-&gt;main\_conf\[module.ctx\_index]

#define ngx\_http\_get\_module\_srv\_conf(r, module)  (r)-&gt;srv\_conf\[module.ctx\_index]

#define ngx\_http\_get\_module\_loc\_conf(r, module)  (r)-&gt;loc\_conf\[module.ctx\_index]

根据ngx\_conf\_t得到三大conf

\-----------------------------------------------------------

#define ngx\_http\_conf\_get\_module\_main\_conf(cf, module)                        \\

    ((ngx\_http\_conf\_ctx\_t \*) cf-&gt;ctx)-&gt;main\_conf\[module.ctx\_index]

#define ngx\_http\_conf\_get\_module\_srv\_conf(cf, module)                         \\

    ((ngx\_http\_conf\_ctx\_t \*) cf-&gt;ctx)-&gt;srv\_conf\[module.ctx\_index]

#define ngx\_http\_conf\_get\_module\_loc\_conf(cf, module)                         \\

    ((ngx\_http\_conf\_ctx\_t \*) cf-&gt;ctx)-&gt;loc\_conf\[module.ctx\_index]

根据cycyle的conf\_ctx得到main conf

\-----------------------------------------------------------

#define ngx\_http\_cycle\_get\_module\_main\_conf(cycle, module)                    \\

    (cycle-&gt;conf\_ctx\[ngx\_http\_module.index] ?                                 \\

        ((ngx\_http\_conf\_ctx\_t \*) cycle-&gt;conf\_ctx\[ngx\_http\_module.index])      \\

            -&gt;main\_conf\[module.ctx\_index]:                                    \\

        NULL)

==================== ngx\_http\_log\_module实例 ==================

ngx\_http\_log\_create\_main\_conf返回的结构体

\-----------------------------------------------------------

typedef struct {

    ngx\_array\_t                 formats;    /* array of ngx\_http\_log\_fmt\_t \*/

    ngx\_uint\_t                  combined\_used; /* unsigned  combined\_used:1 \*/

} ngx\_http\_log\_main\_conf\_t;

ngx\_http\_log\_create\_loc\_conf返回的结构体

\-----------------------------------------------------------

typedef struct {

    ngx\_array\_t                \*logs;       /* array of ngx\_http\_log\_t \*/

    ngx\_open\_file\_cache\_t      \*open\_file\_cache;

    time\_t                      open\_file\_cache\_valid;

    ngx\_uint\_t                  open\_file\_cache\_min\_uses;

    ngx\_uint\_t                  off;        /* unsigned  off:1 \*/

} ngx\_http\_log\_loc\_conf\_t;

指令

\-----------------------------------------------------------

static ngx\_command\_t  ngx\_http\_log\_commands\[] = {

    { ngx\_string("log\_format"),

      NGX\_HTTP\_MAIN\_CONF|NGX\_HTTP\_SRV\_CONF|NGX\_HTTP\_LOC\_CONF|NGX\_CONF\_2MORE,

      ngx\_http\_log\_set\_format,

      NGX\_HTTP\_MAIN\_CONF\_OFFSET,

      0,

      NULL },

    { ngx\_string("access\_log"),

      NGX\_HTTP\_MAIN\_CONF|NGX\_HTTP\_SRV\_CONF|NGX\_HTTP\_LOC\_CONF|NGX\_HTTP\_LIF\_CONF

                        |NGX\_HTTP\_LMT\_CONF|NGX\_CONF\_TAKE123,

      ngx\_http\_log\_set\_log,    

      NGX\_HTTP\_LOC\_CONF\_OFFSET,

      0,   /* 如果使用自定义的set方法, 而且无须知道参数在结构体中的offset,那么offset就可以设为0 \*/

      NULL },

    { ngx\_string("open\_log\_file\_cache"),

      NGX\_HTTP\_MAIN\_CONF|NGX\_HTTP\_SRV\_CONF|NGX\_HTTP\_LOC\_CONF|NGX\_CONF\_TAKE1234,

      ngx\_http\_log\_open\_file\_cache,

      NGX\_HTTP\_LOC\_CONF\_OFFSET,

      0,

      NULL },

      ngx\_null\_command

};

上下文

\-----------------------------------------------------------

static ngx\_http\_module\_t  ngx\_http\_log\_module\_ctx = {

    NULL,                                  /* preconfiguration \*/

    ngx\_http\_log\_init,                     /* postconfiguration \*/

    ngx\_http\_log\_create\_main\_conf,         /* create main configuration \*/

    NULL,                                  /* init main configuration \*/

    NULL,                                  /* create server configuration \*/

    NULL,                                  /* merge server configuration \*/

    ngx\_http\_log\_create\_loc\_conf,          /* create location configration \*/

    ngx\_http\_log\_merge\_loc\_conf            /* merge location configration \*/

};

模块实例

\-----------------------------------------------------------

ngx\_module\_t  ngx\_http\_log\_module = {

    NGX\_MODULE\_V1,

    &ngx\_http\_log\_module\_ctx,              /* module context \*/

    ngx\_http\_log\_commands,                 /* module directives \*/

    NGX\_HTTP\_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

};

==================== end of ngx\_http\_log\_module实例 ==================

注意ngx\_request\_s保存有http block里面的三大conf

\--------------------------------------------------------------------

struct ngx\_http\_request\_s {

    uint32\_t                          signature;         /* "HTTP" \*/

    ngx\_connection\_t                 \*connection;

    void                            \*\*ctx;

    void                            \*\*main\_conf;

    void                            \*\*srv\_conf;

    void                            \*\*loc\_conf;

}

总共创建多少个ngx\_http\_conf\_ctx\_t结构体

\----------------------------------------------------

http block下创建ngx\_http\_conf\_ctx\_t

在server block下创建ngx\_http\_conf\_ctx\_t

在location block下创建ngx\_http\_conf\_ctx\_t

在location block下创建ngx\_http\_conf\_ctx\_t

在location block下创建ngx\_http\_conf\_ctx\_t

结论:　http block只创建一个, 每个server都会创建一个结构体, 每个location都会创建一个结构体

疑问:server block创建的ngx\_http\_conf\_ctx\_t会保存在ngx\_http\_core\_srv\_conf\_t中

    那么location block创建的ngx\_http\_conf\_ctx\_t保存在哪里呢?

调用 ngx\_conf\_parse 的时候会先保存ngx\_conf\_t

\------------------------------------------------

ngx\_conf\_t save;

    save = \*cf;

    cf-&gt;ctx = ctx;

    cf-&gt;cmd\_type = NGX\_HTTP\_LOC\_CONF;

    rv = ngx\_conf\_parse(cf, NULL);

    

    \*cf = save;  

初始化http请求: ngx\_http\_init\_request    

-----------------------------------------------    

    ngx\_http\_core\_srv\_conf\_t \*cscf = addr\_conf-&gt;default\_server;

    r-&gt;main\_conf = cscf-&gt;ctx-&gt;main\_conf;

    r-&gt;srv\_conf = cscf-&gt;ctx-&gt;srv\_conf;

    r-&gt;loc\_conf = cscf-&gt;ctx-&gt;loc\_conf;

 

自动收割子进程

-----------------------------------------------    

在master process中, 函数 ngx\_signal\_handler 用来处理信号, 若

接收到 SIGCHLD 信号, 表示有一个子进程退出了, 然后把变量 ngx\_reap 置为 1

然后在 ngx\_master\_process\_cycle 函数中, 若检查到 ngx\_reap 等于 1,则创建

一个子进程:

```
if (ngx_reap) {            
    ngx_reap = 0;               
    ngx_log_debug0(NGX_LOG_DEBUG_EVENT, cycle->log, 0, "reap children");            
    live = ngx_reap_children(cycle);        
}
```