NGX_HTTP_MODULE 配置解析
一 源起
ngx_http_module 是 HTTP 类型模块在核心模块的代理,ngx_http_module 的指令 http 的解析(ngx_http_block 函数)为 HTTP 类型模块解析的入口。
二 ngx_http_block 分析
1 | static char * |
三 server 指令解析函数 ngx_http_core_server
1 |
|
在 server 指令解析过程中会设置当前 server {} 块的 ngx_http_conf_ctx_t 的 main_conf 为 http {} 的 main_conf,对于 srv_conf 和 loc_conf 会依次调用所有 HTTP 模块的 create_srv_conf 和 create_loc_conf 回调创建相应的配置结构,然后进入 server {} 块的解析。在 server {} 解析完毕后,会将当前 server {} 的配置 ngx_http_conf_ctx_t 加入 ngx_http_core_module 的 servers 数组中。
四 location 指令解析函数 ngx_http_core_location
1 | static char * |
在解析 server {} 内的配置项时会使用如下代码:
1 | pcf = *cf; |
此时设置 cf 为当前 server {} 的配置。
1 | ngx_int_t |
五 总结
http {}、server {}、location {} 的配置项是通过 ngx_http_conf_ctx_t 结构组织的,其定义如下:
1 | typedef struct { |
http {} 级别的配置项由 ngx_http_module 模块管理,ngx_http_module 会持有 ngx_http_core_module,进而管理 server {}、location {} 级别配置。
server {}、location {} 两种级别的配置项都是通过 ngx_http_core_module 模块管理。
main_conf是所有模块的http{}级配置项,由于只能有一个http{}存在他的组织是比较简单的,每个模块只需要有一个main_conf,srv_conf和loc_conf只需要指向对应模块的main_conf即可。- 对于
srv_conf的组织NGINX使用ngx_http_core_module模块作为管理者,所有的server{}块配置组织到动态数组中,数组的每个元素为一个server{}配置项,其中又包含各个模块的ngx_http_conf_ctx_t配置。 - 而
loc_conf的配置NGINX使用ngx_http_core_module模块来管理,将location {}放入父配置项(ngx_http_conf_ctx_t)的loc_conf的location队列中。