当前位置: 首页 > news >正文

ngx_http_add_location

声明在 src\http\ngx_http_core_module.c 

ngx_int_t ngx_http_add_location(ngx_conf_t *cf, ngx_queue_t **locations,
    ngx_http_core_loc_conf_t *clcf);

定义在 src\http\ngx_http.c 

ngx_int_t
ngx_http_add_location(ngx_conf_t *cf, ngx_queue_t **locations,
    ngx_http_core_loc_conf_t *clcf)
{
    ngx_http_location_queue_t  *lq;

    if (*locations == NULL) {
        *locations = ngx_palloc(cf->temp_pool,
                                sizeof(ngx_http_location_queue_t));
        if (*locations == NULL) {
            return NGX_ERROR;
        }

        ngx_queue_init(*locations);
    }

    lq = ngx_palloc(cf->temp_pool, sizeof(ngx_http_location_queue_t));
    if (lq == NULL) {
        return NGX_ERROR;
    }

    if (clcf->exact_match
#if (NGX_PCRE)
        || clcf->regex
#endif
        || clcf->named || clcf->noname)
    {
        lq->exact = clcf;
        lq->inclusive = NULL;

    } else {
        lq->exact = NULL;
        lq->inclusive = clcf;
    }

    lq->name = &clcf->name;
    lq->file_name = cf->conf_file->file.name.data;
    lq->line = cf->conf_file->line;

    ngx_queue_init(&lq->list);

    ngx_queue_insert_tail(*locations, &lq->queue);

    if (ngx_http_escape_location_name(cf, clcf) != NGX_OK) {
        return NGX_ERROR;
    }

    return NGX_OK;
}

ngx_http_add_location 函数的作用是将一个新的 location 配置项添加到 Nginx 的 location 队列中,以便后续处理 HTTP 请求时进行匹配


ngx_int_t
ngx_http_add_location(ngx_conf_t *cf, ngx_queue_t **locations,
    ngx_http_core_loc_conf_t *clcf)
{
    ngx_http_location_queue_t  *lq;

    if (*locations == NULL) {
        *locations = ngx_palloc(cf->temp_pool,
                                sizeof(ngx_http_location_queue_t));
        if (*locations == NULL) {
            return NGX_ERROR;
        }

        ngx_queue_init(*locations);
    }

声明一个指向 ngx_http_location_queue_t 类型的指针 lq,用于后续存储新的 location 配置节点

ngx_http_location_queue_t-CSDN博客

判断传入的 locations 队列是否为空(未初始化)

如果队列未初始化(*locations == NULL),需要为其分配内存并初始化队列结构

此时 *locations=NULL

    lq = ngx_palloc(cf->temp_pool, sizeof(ngx_http_location_queue_t));
    if (lq == NULL) {
        return NGX_ERROR;
    }

 为新的 location 队列节点分配内存

    if (clcf->exact_match
#if (NGX_PCRE)
        || clcf->regex
#endif
        || clcf->named || clcf->noname)
    {
        lq->exact = clcf;
        lq->inclusive = NULL;

    } else {
        lq->exact = NULL;
        lq->inclusive = clcf;
    }
  • clcf->exact_match
    表示当前 location 是精确匹配 (如 location = /exact/ {})。
  • clcf->regex
    表示当前 location 使用正则表达式 (如 location ~ ^/regex/ {})。
    • 该条件受 NGX_PCRE 宏控制,仅在编译时启用 PCRE 库时生效。
  • clcf->named
    表示当前 location 是命名 location (如 location @name {}),通常用于内部跳转。
  • clcf->noname
    表示当前 location 是未命名的特殊 location (如 location / {} 的默认配置)

条件判断

如果当前 location 是以下类型之一:

exact_match= 精确匹配)

regex(正则表达式匹配,如 ~~*

named(命名 location,如 location @name

noname(无名 location,如 location {}

则将其标记为 exact 类型(高优先级)。

否则 (进入 else 分支),标记为 inclusive 类型(普通前缀匹配)。

字段赋值

exact = NULL:显式清空高优先级匹配指针。

inclusive = clcf:将当前 location 的配置结构体指针赋给 inclusive,表示这是一个普通前缀匹配的 location

Nginx 在路由请求时

按优先级顺序匹配 (精确匹配 > 正则匹配 > 普通前缀匹配)

此时 exact_match=0    regex=NULL   named=0    noname=0

    lq->name = &clcf->name;
    lq->file_name = cf->conf_file->file.name.data;
    lq->line = cf->conf_file->line;
 lq->name = &clcf->name;
  • 作用 :将 lqname 指针指向当前 location 配置的名称(clcf->name)。
  • 意义
    • clcf->namengx_str_t 类型,存储了 location 的匹配规则(如 /api~ ^/regex)。
    • 通过 lq->name 可以直接访问该名称,用于请求匹配时的调试输出 日志记录 (例如记录命中了哪个 location)。
 lq->file_name = cf->conf_file->file.name.data;
  • 作用 :将 lqfile_name 指向当前解析的配置文件路径(如 /etc/nginx/conf.d/default.conf)。
  • 意义
    • cf->conf_file->file.name.data 是一个字符串,表示当前解析的配置文件路径。
    • 在出现配置错误(如重复 location 或语法错误)时,Nginx 可以通过 file_name 定位到具体的配置文件 ,方便用户快速排查问题。
 lq->line = cf->conf_file->line;
  • 作用 :将 lqline 设置为当前解析的配置文件行号。
  • 意义
    • cf->conf_file->line 是一个整数,表示当前解析到的配置文件行号。
    • 结合 file_name,可以在错误日志中精确标识配置的位置 (例如 nginx.conf:15),提升调试效率。

此时

lq->name->data=/
lq->file_name=/home/wsd/桌面/nginx/conf/nginx.conf
lq->line=43 

    ngx_queue_init(&lq->list);

 初始化 ngx_http_location_queue_t 节点内部的子队列

若配置中存在嵌套的 location(如 location /parent { location /child {} }),父 location 的 list 可用于链接子节点

    ngx_queue_insert_tail(*locations, &lq->queue);

 将新节点添加到队列的末尾

    if (ngx_http_escape_location_name(cf, clcf) != NGX_OK) {
        return NGX_ERROR;
    }

 

  • 调用转义函数
    ngx_http_escape_location_name(cf, clcf)clcf->name(location 的名称)进行 URI 转义。
  • 错误检查
    如果转义失败(返回值非 NGX_OK),立即终止当前配置解析,返回 NGX_ERROR

ngx_http_escape_location_name-CSDN博客

return NGX_OK;

 返回 NGX_OK

相关文章:

  • python速通小笔记-------3.Numpy库
  • 深入理解 Spring 框架中的 AOP 技术
  • Python----计算机视觉处理(Opencv:图像轮廓特征查找:外接轮廓)
  • 华为HCIE方向那么多应该如何选择?
  • Linux的SPI子系统的原理和结构详解【SPI控制器(spi_master)、SPI总线(device-driver-match匹配机制)、SPI设备、SPI万能驱动`spidev.c`】
  • 前端性能优化方案总结
  • ARCGIS PRO SDK 创建右键菜单
  • 基于腾讯云HAI-CPU部署DeepSeek:搭建图书馆知识库,赋能智慧图书馆建设
  • 从技术架构和生态考虑,不是单纯的配置优化,还有哪些方式可以提高spark的计算性能
  • TCP 三次握手与四次挥手过程
  • Nordic Semiconductor 芯片(如 nRF52/nRF53 系列)的 VSCode 开发环境的步骤
  • 大型语言模型(LLM)推理框架的全面分析与选型指南(2025年版)
  • LLM之RAG实战(五十二)| 如何使用混合搜索优化RAG 检索
  • 鸿蒙进行视频上传,使用 request.uploadFile方法
  • 深入理解C#中的享元模式(Flyweight Pattern)
  • 感应电机反电动势频率与电源频率相等以及转差率的测量机制
  • 26考研——图_图的遍历(6)
  • 【C++】vector的push_back和emplace_back
  • 电动自行车/电动工具锂电池PCM方案--SH367003、SH367004、SH79F329
  • C# SerialPort 类中 Handshake 属性的作用
  • 证监会副主席王建军被查
  • 2025年“投资新余•上海行”钢铁产业“双招双引”推介会成功举行
  • 五一小长假,带着小狗去上海音乐厅
  • 工行一季度净赚841亿元降3.99%,营收降3.22%
  • 辽宁省全力开展辽阳一饭店火灾事故救援处置工作
  • 电话费被私改成48元套餐长达数年,投诉后移动公司退补600元话费