首页 > 网站 > WEB服务 > 正文

Nginx 配置文件解析

2020-05-27 13:45:56
字体:
来源:转载
供稿:网友

  cycle = ngx_init_cycle(&init_cycle):

  1、在内存池中分配一个ngx_cycle_t变量,并初始化它的各个成员。

  2、调用core类型的create_conf,实际只有一个ngx_core_module_create_conf函数----初始化ngx_core_conf_t结构(存放core_module支持的指令),保存在ngx_cycle->conf_ctx数组中。可以说,此时,ngx_cycle->conf_ctx数组中只有一个ngx_core_conf_t结构。

  3、初始化ngx_conf_t结构。

  4、ngx_conf_parse 解析配置文件,把结果保存在模块对应的ngx_conf里面。

  5、调用core类型的init_conf,实际只有一个ngx_core_module_init_conf函数(初始化对应的ngx_core_conf_t函数)。为什么要init,都已经解析配置文件了,应该在这之前初始化呀--如果值为-1,表明没有设置,初始化默认值!

  6、ngx_open_listening_sockets:遍历listening数组并打开所有侦听sockets(socket()->setsockopt()->bind()->listen())。

  7、调用所有模块的init_module(实际上只有ngx_event_core_module模块定义了该callback,即只有ngx_event_module_init()被调用)。

  ngx_conf_parse(ngx_conf_t *cf, ngx_str_t *filename):

  函数的作用就是循环不停的从配置文件中读取指令,然后进行指令处理,直到结束

  1、先分析ngx_core_module的指令及其对应的set函数。

  { ngx_string("daemon"),

  NGX_MAIN_CONF|NGX_DIRECT_CONF|NGX_CONF_FLAG,

  ngx_conf_set_flag_slot,

  0,

  offsetof(ngx_core_conf_t, daemon),

  //计算daemon成员在ngx_core_conf_t结构体里面的偏移

  NULL },

  ngx_conf_set_flag_slot(ngx_conf_t *cf, ngx_command_t *cmd, void *conf):里面代码很清楚,根据配置指令,设置模块conf结构的成员变量。

  2、分析ngx_events_modules的指令及其对应的set函数。只有一条指令:

  { ngx_string("events"),

  NGX_MAIN_CONF|NGX_CONF_BLOCK|NGX_CONF_NOARGS,

  ngx_events_block,

  0,

  0,

  NULL },

  1、ngx_events_module编号为3,于是ngx_cycle->conf_ctx指向的void*数组第3号槽位指向一个void*指针,这个指针又指向一个void*数组(个数==事件类型模块的个数,Linux平台编译之后,只有两个ngx_epoll_module事件模型ngx_event_core_module和ngx_epoll_module)。

  2、调用event类型模块的上下文ngx_event_module_t 的create_conf钩子,为void*数组指定槽位创建相应的conf结构。

  3、更改当前cf环境(NGX_EVENT_MODULE,NGX_EVENT_CONF)解析events{ 块里面的指令。里面的set函数都是根据配置文件设置ngx_event_conf_t结构体里面的成员变量。

  3、分析ngx_http_module的指令及其对应的set函数。只有一条指令,如下:

  { ngx_string("http"),

  NGX_MAIN_CONF|NGX_CONF_BLOCK|NGX_CONF_NOARGS,

  ngx_http_block,

  0,

  0,

  NULL },

  ngx_init_cycle创建了core module的config,那么http module相关的config在那里创建呢?http module相关的config是在ngx_http_block中创建(ngx_http_conf_ctx_t)的,在ngx_http_block中会创建,初始化,合并config(未完全看懂),以及整个http handler phase的初始化(还未看)等等。

 
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表