定義在 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?
?
?