WEB开发网
开发学院服务器WEB服务器Nginx Nginx 配置文件解析 阅读

Nginx 配置文件解析

 2013-03-04 13:58:37 来源:WEB开发网   
核心提示:3、遍历所有的http类型的模块的上下文ngx_http_module_t, 调用 preconfiguration 钩子函数,Nginx 配置文件解析(2),4、解析http块里面的指令:ngx_conf_parse 未看,当前环境:ctx指向ngx_http_conf_ctx_t,ngx_http_core_loc
3、遍历所有的http类型的模块的上下文ngx_http_module_t, 调用 preconfiguration 钩子函数。
4、解析http块里面的指令:ngx_conf_parse 未看,当前环境:ctx指向ngx_http_conf_ctx_t,指令类型为NGX_HTTP_MAIN_CONF,模块类型为:NGX_HTTP_MODULE。
4.1、 遇到http里面的main指令:
if (cmd->type & NGX_DIRECT_CONF) {
conf = ((void **) cf->ctx)[ngx_modules[i]->index]; //ngx_core_conf_t
} else if (cmd->type & NGX_MAIN_CONF) {
//hp: 比如,遇到http/events指令,这里,conf指向的是cf->ctx的第idx个槽位
conf = &(((void **) cf->ctx)[ngx_modules[i]->index]);
} else if (cf->ctx) {
//否则进入二级模块处理,比如events/http {里面的指令,都是在这里

//http 里面的main指令,ctx指向于ngx_http_conf_ctx_t
confp = *(void **) ((char *) cf->ctx + cmd->conf); //这里,cmd->conf的作用体现出来了
if (confp) {
//http里面的main指令: conf指向的是对应的ngx_http_core_main_conf_t
conf = confp[ngx_modules[i]->ctx_index];
}
}
比如variables_hash_max_size 指令:
{ ngx_string("variables_hash_max_size"),
NGX_HTTP_MAIN_CONF|NGX_CONF_TAKE1,
ngx_conf_set_num_slot, //这里,刚好设置conf(ngx_http_core_main_conf_t) 的variables_hash_max_size的值。
NGX_HTTP_MAIN_CONF_OFFSET, //这里,conf+NGX_HTTP_MAIN_CONF_OFFSET就是需要设置的结构体==ngx_http_core_main_conf_t变量地址
offsetof(ngx_http_core_main_conf_t, variables_hash_max_size), //需要设置结构体中的哪个成员,成员在结构体中的偏移。
NULL },
总结出struct ngx_command_s { // 这个域主要是供nginx解析配置文件时使用,设置相关的数据结构
ngx_str_t name;
//命令名称
ngx_uint_t type;
//命令类型(有几个参数等)
char *(*set)(ngx_conf_t *cf, ngx_command_t *cmd, void *conf); //这个conf指向需要设置的结构体变量地址
ngx_uint_t conf;
//配置结构体在内存的位置,相对于conf的偏移量,在ngx_conf_handler中使用
ngx_uint_t offset;
//要设置结构体的哪个成员,相对于结构体的偏移量
void *post;
//一些读取配置时候的零碎变量,一般为null
};
4.2 遇到 server指令时:
生成一个新的ngx_http_conf_ctx_t,其中main_conf指向原来的,server_conf和loc_conf创建新的void*数组。把ngx_http_core_module的server块配置信息存入 main_conf 中的server 容器里面。更新当前环境。 开始解析server块里面的指令。
cf->ctx = ctx; //指向新的ngx_http_conf_ctx_t
cf->cmd_type = NGX_HTTP_SRV_CONF;
4.3 遇到server里面的指令:遇到listen 指令时,ngx_conf_handler中,confp指向的新的ngx_http_conf_ctx_t.srv_conf所指向的void*数组,于是 conf = confp[ngx_modules[i]->ctx_index];指向listen指令对应的模块(ngx_htt_core_module)的 ngx_http_core_srv_conf_t数据结构。
4.4 遇到location指令:生成一个新的ngx_http_conf_ctx_t,其中main_conf指向原来的,srv_conf指向上一层ngx_http_conf_ctx_t的srv_conf数组,loc_conf创建新的void*数组。
ngx_http_core_location 这个函数很麻烦,看起来主要是设置ngx_http_loc_conf_ctx_t里面的name变量。然后递归调用ngx_parse_conf解析location里面的命令。

Tags:Nginx 配置 文件

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接