nginx的結構體 ngx_conf_t

ngx_conf_t

該結構體用於Nginx在解析配置文件時描述每一個指令的屬性。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,存放當前解析到的指令。ide

*args,存放該指令包含的全部參數。函數

*cycle,參見"ngx_cycle_s結構體"。post

*pool,參見"ngx_pool_s結構體"。ui

*temp_pool,用於解析配置文件的臨時內存池,解析完成後釋放。其結構體類型的細節參見11.4節"ngx_pool_s結構體"。atom

*conf_file,存放Nginx配置文件的相關信息。ngx_conf_file_t結構體的定義:spa

typedef struct {
    ngx_file_t            file;
    ngx_buf_t            *buffer;
    ngx_uint_t            line;
} ngx_conf_file_t;

*log,描述日誌文件的相關屬性。ngx_log_t結構體的定義:debug

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 <ngx_set_user>, 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}
相關文章
相關標籤/搜索