ngx_tcp_core_location(ngx_conf_t*cf, ngx_command_t *cmd, void *dummy)
clcf= ctx->loc_conf[ngx_tcp_core_module.ctx_index];
clcf->loc_conf= ctx->loc_conf;
同样的逻辑,看着很迷惑。
使用clcf,给loc_conf赋值
ngx_tcp_add_location(cf,&pclcf->locations, clcf)
这个函数主要把pclcf,就是以前的ctx中的loc_conf中的locations赋值,添加clcf。
相当于location串起来了。
lcf->upstream.upstream= ngx_tcp_upstream_add(cf, &u, 0);
增加上游配置,配置到了当前的loc_conf。
lcf->upstream.upstream里面保存的是upstream的loc_conf。
rv= module->init_main_conf(cf, ctx->main_conf[mi]);
初始化main_conf,因为http的全局只有一份main_conf,所以不是merge。而是init。
ngx_tcp_merge_servers这个函数主要合并每个module的srv_conf。
每个module调用她,然后,每个server合并module。
rv= module->merge_srv_conf(cf, saved.srv_conf[ctx_index],
cscfp[s]->ctx->srv_conf[ctx_index]);
从这儿可以看到,每个server的ctx中的srv_conf数组中的每个module有一个srv_conf。
rv= module->merge_loc_conf(cf, saved.loc_conf[ctx_index],
cscfp[s]->ctx->loc_conf[ctx_index]);
同样,每个server中的loc_conf对应的模块,都有自己的loc_conf。
cscfp[s]->ctx->loc_conf[ngx_tcp_core_module.ctx_index]->locations--------------q->loc_conf[ctx_index] child
cscfp[s]->ctx->loc_conf[ctx_index] parent
合并后,便每一个server就有一个ngx_tcp_core_module.ctx_index->locations
也就是说,我可以找到cscfp[s]->ctx->loc_conf[ngx_tcp_core_module.ctx_index]->locations中的location,并且,然后,找到相应的location,根据当前模块,找到当前模块的loc_conf。也就是说每个location中放的每个module的指令值不相同。
就是说,如果一个指令可以放在main_conf,srv_conf,loc_conf中,则指令的偏移值必须取道loc_conf中。这样子,在parse的适合,就会把指令放入最低级之中。即loc_conf中,然后,通过merge进行合并,最后,放到相应的地方。例如srv_conf,则放入每个server的srv_conf数组中间,根据模块的ctx_index取出来。
ngx_queue_middle(ngx_queue_t*queue)
这个函数很有意思,两个指针,一个走一步,一个走二步,判断是否到中间了。
structngx_tcp_location_tree_node_s {
ngx_tcp_location_tree_node_t *left;
ngx_tcp_location_tree_node_t *right;
ngx_tcp_location_tree_node_t *tree;
ngx_tcp_core_loc_conf_t *exact;
ngx_tcp_core_loc_conf_t *inclusive;
u_char auto_redirect;
u_char len;
u_char name[1]; // 非常奇怪,为什么是1个空间呢?
};
node= ngx_palloc(cf->pool, offsetof(ngx_tcp_location_tree_node_t,name) + len);
这里,有好像有分配node->name的空间
ngx_memcpy(node->name,&lq->name->data[prefix], len);
这里,好像把name都拷贝进去了
locationtree
参考:
http://blog.chinaunix.net/uid-27767798-id-3759557.html
nginxqueue:
参考:
http://blog.youkuaiyun.com/livelylittlefish/article/details/6607324
这段代码出自:ngx_tcp_create_locations_list
for(x = ngx_queue_next(q);
x!= ngx_queue_sentinel(locations);
x= ngx_queue_next(x))
{
lx= (ngx_tcp_location_queue_t *) x;
if(len > lx->name->len
||(ngx_strncmp(name, lx->name->data, len) != 0))
{
break;
}
}
突然看懂,这段代码的意思是如果当前的name不包含于现在的那么,则跳出。
然后,撕裂这段代码,这样包含的就在q->list中了,不包含的就在location中。
ngx_tcp_optimize_servers(ngx_conf_t*cf, ngx_tcp_core_main_conf_t *cmcf,
ngx_array_t*ports)
这个函数的作用
ngx_sprintf(buf,"lws://%V:%ui", &s->connection->addr_text, port)
nginx自定义,直接跟踪即可