ngx_http_core_find_location 1 定义ngx_http_core_find_location 函数 定义在 src/http/ngx_http_core_module.cstaticngx_int_tngx_http_core_find_location(ngx_http_request_t*r){ngx_int_trc;ngx_http_core_loc_conf_t*pclcf;#if(NGX_PCRE)ngx_int_tn;ngx_uint_tnoregex;ngx_http_core_loc_conf_t*clcf,**clcfp;noregex0;#endifpclcfngx_http_get_module_loc_conf(r,ngx_http_core_module);rcngx_http_core_find_static_location(r,pclcf-static_locations);if(rcNGX_AGAIN){#if(NGX_PCRE)clcfngx_http_get_module_loc_conf(r,ngx_http_core_module);noregexclcf-noregex;#endif/* look up nested locations */rcngx_http_core_find_location(r);}if(rcNGX_OK||rcNGX_DONE){returnrc;}/* rc NGX_DECLINED or rc NGX_AGAIN in nested location */#if(NGX_PCRE)if(noregex0pclcf-regex_locations){for(clcfppclcf-regex_locations;*clcfp;clcfp){ngx_log_debug1(NGX_LOG_DEBUG_HTTP,r-connection-log,0,test location: ~ \%V\,(*clcfp)-name);nngx_http_regex_exec(r,(*clcfp)-regex,r-uri);if(nNGX_OK){r-loc_conf(*clcfp)-loc_conf;/* look up nested locations */rcngx_http_core_find_location(r);return(rcNGX_ERROR)?rc:NGX_OK;}if(nNGX_DECLINED){continue;}returnNGX_ERROR;}}#endifreturnrc;}ngx_http_core_find_location 函数的作用是 递归地为 HTTP 请求匹配最佳的 location 块。 它按照 Nginx 的优先级规则 依次执行静态前缀/精确匹配、嵌套 location 递归以及正则匹配 最终确定请求应使用的 location 配置 并返回匹配结果成功、需重定向、未匹配或错误。2 详解1 函数签名staticngx_int_tngx_http_core_find_location(ngx_http_request_t*r)返回值 返回函数执行结果的状态码 告知调用者执行情况参数 ngx_http_request_t *r 指向当前 HTTP 请求上下文结构体2 逻辑流程1 局部变量 2 静态 location 匹配 3 处理嵌套 location 递归 4 返回静态匹配的结果 5 正则匹配 6 最终返回1 局部变量{ngx_int_trc;ngx_http_core_loc_conf_t*pclcf;#if(NGX_PCRE)ngx_int_tn;ngx_uint_tnoregex;ngx_http_core_loc_conf_t*clcf,**clcfp;noregex0;#endif2 静态 location 匹配pclcfngx_http_get_module_loc_conf(r,ngx_http_core_module);获取当前级别的 location 配置rcngx_http_core_find_static_location(r,pclcf-static_locations);在静态 location 树中查找匹配当前请求 URI 的 location3 处理嵌套 location 递归if(rcNGX_AGAIN){#if(NGX_PCRE)clcfngx_http_get_module_loc_conf(r,ngx_http_core_module);noregexclcf-noregex;#endif/* look up nested locations */rcngx_http_core_find_location(r);}静态匹配发现当前匹配的 location 是一个嵌套 location 即带有子 location 定义的 location 块 必须继续向其内部查找。 作用 获取当前已匹配 location 的 noregex 属性 并赋给局部变量 noregex。 逻辑 嵌套 location 可能设置 noregex 指令 禁止对内部请求执行正则 location 匹配。 在递归前将该标志取出传递给更深层的匹配流程。 意义保证 noregex 能被子级 location 继承实现正则匹配的控制。 递归调用自身 以当前已匹配的 location 为新的上下文 r-loc_conf 已经指向该 location 的配置数组 继续在内部的子 location 中查找。 逻辑 递归结束后rc 会获得最终的匹配结果 意义通过递归实现了 location 的无限嵌套能力同时保持了代码的简洁。4 返回静态匹配的结果if(rcNGX_OK||rcNGX_DONE){returnrc;}如果静态匹配及其嵌套递归已经得到了明确的结果—— 成功匹配到最终 locationNGX_OK 或需要重定向NGX_DONE 则立即返回该结果不再进行正则匹配。 意义确保 location 匹配优先级5 正则匹配#if(NGX_PCRE)if(noregex0pclcf-regex_locations){for(clcfppclcf-regex_locations;*clcfp;clcfp){ngx_log_debug1(NGX_LOG_DEBUG_HTTP,r-connection-log,0,test location: ~ \%V\,(*clcfp)-name);nngx_http_regex_exec(r,(*clcfp)-regex,r-uri);if(nNGX_OK){r-loc_conf(*clcfp)-loc_conf;/* look up nested locations */rcngx_http_core_find_location(r);return(rcNGX_ERROR)?rc:NGX_OK;}if(nNGX_DECLINED){continue;}returnNGX_ERROR;}}#endif正则匹配阶段 仅在 PCRE 库可用时编译 保证在没有 PCRE 的环境下该函数仍能工作。6 最终返回returnrc;}