ngx_http_index_set_index

news/2025/3/31 5:25:57/

定义在 src\http\modules\ngx_http_index_module.c 

static char *
ngx_http_index_set_index(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
{ngx_http_index_loc_conf_t *ilcf = conf;ngx_str_t                  *value;ngx_uint_t                  i, n;ngx_http_index_t           *index;ngx_http_script_compile_t   sc;if (ilcf->indices == NULL) {ilcf->indices = ngx_array_create(cf->pool, 2, sizeof(ngx_http_index_t));if (ilcf->indices == NULL) {return NGX_CONF_ERROR;}}value = cf->args->elts;for (i = 1; i < cf->args->nelts; i++) {if (value[i].data[0] == '/' && i != cf->args->nelts - 1) {ngx_conf_log_error(NGX_LOG_WARN, cf, 0,"only the last index in \"index\" directive ""should be absolute");}if (value[i].len == 0) {ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,"index \"%V\" in \"index\" directive is invalid",&value[1]);return NGX_CONF_ERROR;}index = ngx_array_push(ilcf->indices);if (index == NULL) {return NGX_CONF_ERROR;}index->name.len = value[i].len;index->name.data = value[i].data;index->lengths = NULL;index->values = NULL;n = ngx_http_script_variables_count(&value[i]);if (n == 0) {if (ilcf->max_index_len < index->name.len) {ilcf->max_index_len = index->name.len;}if (index->name.data[0] == '/') {continue;}/* include the terminating '\0' to the length to use ngx_memcpy() */index->name.len++;continue;}ngx_memzero(&sc, sizeof(ngx_http_script_compile_t));sc.cf = cf;sc.source = &value[i];sc.lengths = &index->lengths;sc.values = &index->values;sc.variables = n;sc.complete_lengths = 1;sc.complete_values = 1;if (ngx_http_script_compile(&sc) != NGX_OK) {return NGX_CONF_ERROR;}}return NGX_CONF_OK;
}

ngx_http_index_set_index 函数是 Nginx 中处理 index 配置指令的核心逻辑


    if (ilcf->indices == NULL) {ilcf->indices = ngx_array_create(cf->pool, 2, sizeof(ngx_http_index_t));if (ilcf->indices == NULL) {return NGX_CONF_ERROR;}}

通过 ilcf->indices == NULL 判断当前配置块的 indices 数组是否未被初始化

ilcf->indices 是 Nginx 中用于 存储 index 指令配置的索引文件列表 的核心数据结构

它是一个动态数组(ngx_array_t 类型),每个元素是 ngx_http_index_t 结构体,表示一个索引文件(如 index.html

ngx_http_index_loc_conf_t-CSDN博客

ngx_http_index_t-CSDN博客

此时

ilcf->indices= NULL

进入这个条件

    value = cf->args->elts;for (i = 1; i < cf->args->nelts; i++) {if (value[i].data[0] == '/' && i != cf->args->nelts - 1) {ngx_conf_log_error(NGX_LOG_WARN, cf, 0,"only the last index in \"index\" directive ""should be absolute");}

校验 index 指令参数的合法性 ,确保绝对路径的索引文件(以 / 开头)只能作为最后一个参数

  • Nginx 按顺序检查索引文件,一旦找到匹配的文件即停止搜索。
  • 绝对路径通常指向固定位置,作为“最终回退选项”更合理。
  • 如果绝对路径出现在中间参数,可能导致后续参数被忽略,引发配置逻辑错误
        if (value[i].len == 0) {ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,"index \"%V\" in \"index\" directive is invalid",&value[1]);return NGX_CONF_ERROR;}

 校验 index 指令中每个索引文件名的合法性 ,确保文件名非空

        index = ngx_array_push(ilcf->indices);if (index == NULL) {return NGX_CONF_ERROR;}

 ilcf->indices 数组末尾分配一个新元素

        index->name.len = value[i].len;index->name.data = value[i].data;index->lengths = NULL;index->values = NULL;

设置新元素的各个字段

 

index->name.len  = value[i].len;       // (1) 复制文件名长度
index->name.data = value[i].data;   // (2) 复制文件名数据指针
index->lengths   = NULL;                // (3) 标记无需动态长度计算
index->values    = NULL;                // (4) 标记无需动态值生成

n = ngx_http_script_variables_count(&value[i]);

统计 index 指令参数中变量的数量

此时 n=0 

        if (n == 0) {if (ilcf->max_index_len < index->name.len) {ilcf->max_index_len = index->name.len;}if (index->name.data[0] == '/') {continue;}/* include the terminating '\0' to the length to use ngx_memcpy() */index->name.len++;continue;}

 

if (n == 0) {  // (1) 确认为静态文件名(无变量)
    // (2) 更新最大文件名长度
    if (ilcf->max_index_len < index->name.len) {
        ilcf->max_index_len = index->name.len;
    }

    // (3) 跳过绝对路径的特殊处理
    if (index->name.data[0] == '/') {
        continue;
    }

    // (4) 包含终止符 '\0' 的长度调整
    index->name.len++;

    continue;  // (5) 跳出循环,避免动态编译逻辑
}

2次 continue 然后循环结束

return NGX_CONF_OK;

返回 NGX_CONF_OK 

 


 


http://www.ppmy.cn/news/1583740.html

相关文章

测谎仪策略思路

来源:【东吴金工 金工专题】“高频价量相关性拥抱CTA”系列研究&#xff08;四&#xff09;&#xff1a;CPV因子期货版3.0—CPV测谎机 原创 高子剑 量化邻距离 2024年09月20日 14:37 该报告主要介绍了“高频价量相关性拥抱CTA”系列研究中CPV因子期货版的相关内容&#xff0c;…

新能源动力电池测试设备深度解析:充放电设备与电池模拟器的差异及技术趋势

一、技术原理对比与核心技术创新 充放电设备 核心原理与硬件架构 充放电设备的核心功能是通过电力电子技术精确控制电池的充放电过程&#xff0c;其硬件架构包括高精度电源模块、双向DC/DC变换器、数据采集系统和温控单元。例如&#xff0c;在放电阶段&#xff0c;设备通过双向…

MySQL - 数据库基础操作

SQL语句 结构化查询语言(Structured Query Language)&#xff0c;在关系型数据库上执行数据操作、数据检索以及数据维护的标准语言。 分类 DDL 数据定义语言(Data Definition Language)&#xff0c;定义对数据库对象(库、表、列、索引)的操作。 DML 数据操作语言(Data Manip…

python每日十题(12)

根据字典的索引方式可知&#xff0c;d.get( egg ,no this food)索引的是字典第一层&#xff0c;但是第一层只有键food&#xff0c;没有键egg&#xff0c;故索引不出值&#xff0c;输出的是“no this food ”。 外层for循环是将a[0][1,2,3],a[1][4,5,6],a[2][7,8,9]依次赋给变量…

多版本PHP开发环境配置教程:WAMPServer下MySQL/Apache/MariaDB版本安装与切换

提示&#xff1a;文章写完后&#xff0c;目录可以自动生成&#xff0c;如何生成可参考右边的帮助文档 文章目录 前言一、版本切换指南总结 前言 由于有几个项目分别使用到PHP7.0 和7.4以及8.0版本&#xff0c;设置mysql也会根据PHP版本使用不同的版本&#xff0c;于是开始研究…

【Charles的重定向】

重定向接口请求 注意&#xff1a;不用的时候记得关掉&#xff01; 在测试和开发过程中&#xff0c;有时候需要修改接口的返回状态&#xff0c;或是返回值。在Charles中可以通过远程映射&#xff0c;将B接口的响应返回给A接口&#xff0c;从而达到修改接口响应的目的。这个功能…

在 Linux(Ubuntu / CentOS 7)上快速搭建我的世界 MineCraft 服务器,并实现远程联机,详细教程

Linux 部署 MineCraft 服务器 详细教程&#xff08;丐版&#xff0c;无需云服务器&#xff09; 一、虚拟机 Ubuntu 部署二、下载 Minecraft 服务端三、安装 JRE 21四、安装 MCS manager 面板五、搭建服务器六、本地测试连接七、下载樱花&#xff0c;实现内网穿透&#xff0c;邀…

Scheme语言的网络安全

Scheme语言在网络安全中的应用 引言 在当今信息技术迅速发展的时代&#xff0c;网络安全成为了各个组织和个人关注的焦点。随着互联网的普及&#xff0c;网络攻击、数据泄露和信息安全事件层出不穷&#xff0c;如何有效保障网络安全&#xff0c;成为了亟待解决的问题。语言的…