您的位置:首页 > 运维架构 > Nginx

[nginx源码解析]配置解析(main作用域)

2015-05-07 06:58 357 查看
下面我们就以一个简单配置(nginx默认配置文件)来进行分析整个配置解析过程,同时会附图

配置文件如下(nginx默认生成配置文件)

worker_processes 1;
daemon off;
events {
worker_connections 1024 ;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 8880;
server_name localhost;
location / {
root html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}

配置文件中的一些注释,让我给去掉,方便说明,其中这些用户设置的值,都会保存在conf->ctx上,我们主要是根据用户配置的文件(nginx.conf)来一步步跟踪conf->ctx。

break三个断点

1 执行完ngx_conf_read_token后

2 函数ngx_conf_handler

3 函数ngx_conf_handler中如下407行(因为我的代码加注释了,所以可能行数与你的不同)

407             conf = NULL;
408
409             if (cmd->type & NGX_DIRECT_CONF) {//如果是直接存在即command的ctx存在create_conf的话,就会走这个流程
410                 conf = ((void **) cf->ctx)[ngx_modules[i]->index];
411
412             } else if (cmd->type & NGX_MAIN_CONF) {




找到为第一个模块(索引i==0)即ngx_core_module,所以conf指向的是ngx_core_module的结构映射到的命令为

{ ngx_string("worker_processes"),
NGX_MAIN_CONF|NGX_DIRECT_CONF|NGX_CONF_TAKE1,
ngx_conf_set_num_slot,
0,
offsetof(ngx_core_conf_t, worker_processes),
NULL },

从上面worker_processes可以看到该命令是只能用的NGX_MAIN_CONF,而且必须带一个参数,该命令set回调函数是ngx_conf_set_num_slot(cf,cmd, ngx_core_conf_t)

设置ngx_conf_conf_t->worker_processes=1



daemon和worker_processes一样,也是存储在ngx_core_module里面,相应的命令为

{ ngx_string("daemon"),
NGX_MAIN_CONF|NGX_DIRECT_CONF|NGX_CONF_FLAG,
ngx_conf_set_flag_slot,
0,
offsetof(ngx_core_conf_t, daemon),
NULL }

和worker_processes不同的是daemon是NGX_CONF_FLAG即使一个bool值,同时回调函数为ngx_conf_set_flag_slot,

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  nginx main 源码