ngx_conf_t
该结构体用于Nginx在解析配置文件时描述每个指令的属性。
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;};typedef struct ngx_conf_s ngx_conf_t;
指令描述
*name,存放当前解析到的指令。
*args,存放该指令包含的所有参数。
*cycle,参见"ngx_cycle_s结构体"。
*pool,参见"ngx_pool_s结构体"。
*temp_pool,用于解析配置文件的临时内存池,解析完成后释放。其结构体类型的细节参见11.4节"ngx_pool_s结构体"。
*conf_file,存放Nginx配置文件的相关信息。ngx_conf_file_t结构体的定义:
typedef struct { ngx_file_t file; ngx_buf_t *buffer; ngx_uint_t line;} ngx_conf_file_t;
*log,描述日志文件的相关属性。ngx_log_t结构体的定义:
struct ngx_log_s { ngx_uint_t log_level; ngx_open_file_t *file; ngx_atomic_uint_t connection; ngx_log_handler_pt handler; void *data; /* * we declare "action" as "char *" because the actions are usually * the static strings and in the "u_char *" case we have to override * their types all the time */ char *action; ngx_log_t *next;};typedef struct ngx_log_s ngx_log_t;
*ctx,描述指令的上下文。
module_type,支持该指令的模块的类型,core、http、event和mail中的一种。
cmd_type,指令的类型。
handler,指令自定义的处理函数。
*handler_conf,自定义处理函数需要的相关配置。
示例:
配置:
user root; #use root mode
打印ngx_conf_t结构体
p *cf$68 = {name = 0x0, args = 0x2098388, cycle = 0x20970b0, pool = 0x2097060, temp_pool = 0x209b070, conf_file = 0x7fffffffde80, log = 0xa37c40, ctx = 0x2097eb8, module_type = 1163022147, cmd_type = 16777216, handler = 0, handler_conf = 0x0}
args内容
(gdb) p (ngx_str_t *) cf->args->elts$70 = (ngx_str_t *) 0x20983b0(gdb) p ((ngx_str_t *) cf->args->elts)[0]$71 = {len = 4, data = 0x2098450 "user"}(gdb) p ((ngx_str_t *) cf->args->elts)[1]$72 = {len = 4, data = 0x2098456 "root"}(gdb) p cf->args$73 = (ngx_array_t *) 0x2098388(gdb) p *cf->args$74 = {elts = 0x20983b0, nelts = 2, size = 16, nalloc = 10, pool = 0x2097060}
conf_file内容
(gdb) p *cf->conf_file$69 = {file = {fd = 8, name = {len = 37, data = 0x20972b3 "/usr/local/sms/conf/nginx_andrew.conf"}, info = {st_dev = 2050, st_ino = 958962, st_nlink = 1, st_mode = 33188, st_uid = 0, st_gid = 0, __pad0 = 0, st_rdev = 0, st_size = 8325, st_blksize = 4096, st_blocks = 24, st_atim = {tv_sec = 1502187399, tv_nsec = 449991648}, st_mtim = {tv_sec = 1501494423, tv_nsec = 911991609}, st_ctim = {tv_sec = 1501494423, tv_nsec = 913991638}, __unused = {0, 0, 0}}, offset = 4096, sys_offset = 140737488348464, log = 0xa37c40, valid_info = 0, directio = 0}, buffer = 0x7fffffffdf60, line = 1}
log内容
(gdb) p cf->log$75 = (ngx_log_t *) 0xa37c40(gdb) p *cf->log$76 = {log_level = 6, file = 0xa37c80, connection = 0, handler = 0, data = 0x0, action = 0x0, next = 0x0}
配置上下文*ctx
ngx_cycle_t * ngx_init_cycle(ngx_cycle_t *old_cycle)函数中内容:
for (i = 0; ngx_modules[i]; i++) { if (ngx_modules[i]->type != NGX_CORE_MODULE) { continue; } module = ngx_modules[i]->ctx; if (module->create_conf) { rv = module->create_conf(cycle); if (rv == NULL) { ngx_destroy_pool(pool); return NULL; } cycle->conf_ctx[ngx_modules[i]->index] = rv; } } conf.ctx = cycle->conf_ctx; conf.cycle = cycle; conf.pool = pool; conf.log = log; conf.module_type = NGX_CORE_MODULE; conf.cmd_type = NGX_MAIN_CONF;#if 0 log->log_level = NGX_LOG_DEBUG_ALL;#endif if (ngx_conf_param(&conf) != NGX_CONF_OK) { environ = senv; ngx_destroy_cycle_pools(&conf); return NULL; } if (ngx_conf_parse(&conf, &cycle->conf_file) != NGX_CONF_OK) { environ = senv; ngx_destroy_cycle_pools(&conf); return NULL; }
static ngx_int_t ngx_conf_handler(ngx_conf_t *cf, ngx_int_t last) 函数中内容:
if (cmd->type & NGX_DIRECT_CONF) { conf = ((void **) cf->ctx)[ngx_modules[i]->index]; } else if (cmd->type & NGX_MAIN_CONF) { conf = &(((void **) cf->ctx)[ngx_modules[i]->index]); } else if (cf->ctx) { confp = *(void **) ((char *) cf->ctx + cmd->conf); if (confp) { conf = confp[ngx_modules[i]->ctx_index]; } } rv = cmd->set(cf, cmd, conf);
打印结果
(gdb) p conf$40 = (void *) 0x20981a8(gdb) p cmd$41 = (ngx_command_t *) 0xa06d58(gdb) p *cmd$42 = {name = {len = 4, data = 0x736609 "user"}, type = 16842758, set = 0x44b706, conf = 0, offset = 0, post = 0x0}(gdb) p ccf$65 = (ngx_core_conf_t *) 0x20981a8(gdb) p *ccf$66 = {daemon = -1, master = -1, timer_resolution = 18446744073709551615, worker_processes = -1, debug_points = -1, rlimit_nofile = -1, rlimit_sigpending = -1, rlimit_core = -1, priority = 0, cpu_high = 18446744073709551615, cpu_affinity_n = 0, cpu_affinity = 0x0, username = 0x2098456 "root", user = 0, group = 4294967295, working_directory = {len = 0, data = 0x0}, lock_file = {len = 0, data = 0x0}, pid = {len = 0, data = 0x0}, oldpid = {len = 0, data = 0x0}, env = {elts = 0x2098290, nelts = 0, size = 16, nalloc = 1, pool = 0x2097060}, environment = 0x0, resolver_hostfile = -1}
其中cmd表示ngx_command_t结构体对应的指针,conf表示ngx_core_module_t结构体元素create_conf对应的结构体指针。
(gdb) p cf->ctx$77 = (void *) 0x2097eb8(gdb) p ((void **) cf->ctx)[0]$79 = (void *) 0x20981a8(gdb) p (ngx_core_conf_t *)((void **) cf->ctx)[0]$80 = (ngx_core_conf_t *) 0x20981a8(gdb) p *(ngx_core_conf_t *)((void **) cf->ctx)[0]$81 = {daemon = -1, master = -1, timer_resolution = 18446744073709551615, worker_processes = -1, debug_points = -1, rlimit_nofile = -1, rlimit_sigpending = -1, rlimit_core = -1, priority = 0, cpu_high = 18446744073709551615, cpu_affinity_n = 0, cpu_affinity = 0x0, username = 0x2098456 "root", user = 0, group = 0, working_directory = {len = 0, data = 0x0}, lock_file = {len = 0, data = 0x0}, pid = {len = 0, data = 0x0}, oldpid = {len = 0, data = 0x0}, env = {elts = 0x2098290, nelts = 0, size = 16, nalloc = 1, pool = 0x2097060}, environment = 0x0, resolver_hostfile = -1}