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

下面我们就以一个简单配置(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,




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值