nginx配置文件解析

以启动过程中设置worker_processes的值为例,分析nginx加载配置的过程,代码版本1.1.15。

1. 首先创建所有模块配置的上下文,代码调用栈如下:

ngx_init_cycle (old_cycle=0xbffff5b0) at src/core/ngx_cycle.c:223

调用对应模块的create_conf指针函数创建默认的配置项。

由于worker_processes属于核心模块的配置,自然是调用模块ngx_core_module的ngx_core_module_ctx的

ngx_core_module_create_conf函数创建默认的配置环境,代码在:

ngx_core_module_create_conf (cycle=0x80fc5a8) at src/core/nginx.c:930


2. 然后调用ngx_core_module_init_conf()函数初始化core模块的配置环境。


3. 由于worker_processes是core的指令,所以在ngx_core_commands数组中定义了如何设置worker_process的值。


4. 读取配置文件,按照2中的方法设置worker_processes的值。

函数调用堆栈:

#0  ngx_conf_set_num_slot (cf=0xbffff408, cmd=0x80e632c, conf=0x80fce28) at src/core/ngx_conf_file.c:1193
#1  0x0805eedc in ngx_conf_handler (cf=0xbffff408, last=0) at src/core/ngx_conf_file.c:394
#2  0x0805eb32 in ngx_conf_parse (cf=0xbffff408, filename=0x80fc658) at src/core/ngx_conf_file.c:244
#3  0x0805bca2 in ngx_init_cycle (old_cycle=0xbffff5b0) at src/core/ngx_cycle.c:268
#4  0x0804a53c in main (argc=3, argv=0xbffff744) at src/core/nginx.c:331

调用ngx_conf_set_num_slot时, 第三个参数conf就是在1中创建的该模块的配置环境,worker_process在conf中的偏移量就是cmd->offset:12,


图中,cmd->offset就是offsetof(ngx_core_conf_t, worker_processes)的值,worker_processes是ngx_core_conf_t的成员变量。





你可能感兴趣的:(Linux编程,nginx,module,cmd,c)