Nginx:location快速查询

一、location解析

在这里插入图片描述

  • location共分为三种:
    1)exact_match:“=”类型,全匹配;
    2)noregx:“^~”类型,前缀匹配;
    3)regex:“”或“*”类型,正则匹配;
    4)named:“@”类型,内部跳转匹配。

  • 对于存在父子关系的location:
    1)父只能是noregex或regex类型;
    2)子不能使named类型;
    3)子的name不能包含父。

二、location加入server级的clcf->locations队列

在这里插入图片描述

三、快速查询树的建立

  • ngx_http_init_locations
    1)对server级的clcf->locations队列进行排序。
    exact_match/inclusive(name相同时exact在前)> regex(不排序) > named(排序) > noname
    2)对named(队列)部分,加入server级cscf->named_locations数组;
    对regex(队列)部分,加入server级clcf->regex_locations数组。
    此时server级clcf->locations只剩下exact_match和noregex类型。

  • ngx_http_join_exact_locations
    如果locations队列中存在名字相同的exact_match和inclusive类型,那么将这两个节点合并,即:lq->inclusive = lx->inclusive;这一步的目的实际是去重,为后面建立顺序树做准备。

  • ngx_http_create_locations_list
    递归每个location节点,得到当前节点的名字为其前缀的location列表,保存在队列节点的*list字段
    在这里插入图片描述

  • ngx_http_create_locations_tree
    建立三叉平衡树,根节点为locations的中间节点,其左子树为locations队列的左半部分建立的location tree,右子树为location队列的右半部分建立的tree,tree节点为该根节点的list队列建立的tree。
    在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/u013032097/article/details/91422084