nginx中的ngx_command_t结构中的set方法在何时调用

/*********************************************************************
 * Author  : Samson
 * Date    : 05/29/2015
 * Test platform:
 *              gcc (Ubuntu 4.8.2-19ubuntu1) 4.8.2
 *              GNU bash, 4.3.11(1)-release (x86_64-pc-linux-gnu) 
 * *******************************************************************/

在nginx模块中,作为标准的模块结构体ngx_module_t,此结构主要包括了ngx_command_t,ngx_http_module_t等模块的属性,配置字段及对应的处理方法,其中ngx_command_t的结构为:

struct ngx_command_s {

ngx_str_t name;

ngx_uint_t type;

char *(*set)(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);

ngx_uint_t conf;

ngx_uint_t offset;

void *post;

};

其中name表示配置字段的名称;

set是一个函数指针,用于指定一个参数转化函数,这个函数一般是将配置文件中相关指令的参数转化成需要的格式并存入配置结构体,即是配置字段对应的配置方法。


那么模块中的commands_t中的set在什么时候进行调用?

以一个proxy模块的proxy_pass字段为例,当nginx的配置文件中有proxy_pass选项启动时的执行堆栈为:

main---> ngx_init_cycle ---> ngx_conf_parse --->ngx_http_block --->ngx_conf_handler --->ngx_http_core_server --->ngx_conf_handler --->ngx_http_core_location ---> ngx_conf_handler---> ngx_http_proxy_pass


以上执行堆栈中的ngx_conf_parse为解析配置文件或配置块(如:http {} server {} location {});

ngx_http_block方法是在当配置文件中出现了http{}块配置时进行调用;

ngx_conf_handler是某个块配置下的所有进行了配置了的字段对应的commands进行调用,也即是对配置项对应的set函数进行调用处理;

ngx_http_core_server方法是在当配置文件中出现了server{}块配置时进行调用;

ngx_http_core_location方法是在当配置文件中出现了location{}块配置时进行调用;

ngx_http_proxy_pass方法对应的是proxy_pass配置字段的set方法。


在以上的执行堆栈中,每当遇到一个块配置时就会调用一次ngx_conf_handler进行配置字段对应的模块中set方法进行循环调用。


ngx_conf_handler,在函数中进行遍历所有http模块的已经配置的配置关键字,再调用rv = cmd->set(cf, cmd, conf);进行关键字对应的set方法的调用。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值