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

ngx_http_cmp_locations

1 定义

ngx_http_cmp_locations 函数 定义在 ./nginx-1.24.0/src/http/ngx_http.c
staticngx_int_tngx_http_cmp_locations(constngx_queue_t*one,constngx_queue_t*two){ngx_int_trc;ngx_http_core_loc_conf_t*first,*second;ngx_http_location_queue_t*lq1,*lq2;lq1=(ngx_http_location_queue_t*)one;lq2=(ngx_http_location_queue_t*)two;first=lq1->exact?lq1->exact:lq1->inclusive;second=lq2->exact?lq2->exact:lq2->inclusive;if(first->noname&&!second->noname){/* shift no named locations to the end */return1;}if(!first->noname&&second->noname){/* shift no named locations to the end */return-1;}if(first->noname||second->noname){/* do not sort no named locations */return0;}if(first->named&&!second->named){/* shift named locations to the end */return1;}if(!first->named&&second->named){/* shift named locations to the end */return-1;}if(first->named&&second->named){returnngx_strcmp(first->name.data,second->name.data);}#if(NGX_PCRE)if(first->regex&&!second->regex){/* shift the regex matches to the end */return1;}if(!first->regex&&second->regex){/* shift the regex matches to the end */return-1;}if(first->regex||second->regex){/* do not sort the regex matches */return0;}#endifrc=ngx_filename_cmp(first->name.data,second->name.data,ngx_min(first->name.len,second->name.len)+1);if(rc==0&&!first->exact_match&&second->exact_match){/* an exact match must be before the same inclusive one */return1;}returnrc;}
`ngx_http_cmp_locations` 是 nginx 中用于对配置文件中所有 `location` 进行排序的比较函数。 其作用是将不同类型的 `location` 按照 匹配优先级 排列成固定顺序: 1. 普通前缀匹配(精确匹配在前) 2. 正则匹配 location 3. 命名 location 4. 无名(noname)location 排序后的列表用于构建后续高效查找的位置树,确保运行时能按正确顺序匹配请求。

2 详解

1 函数签名

staticngx_int_tngx_http_cmp_locations(constngx_queue_t*one,constngx_queue_t*two)
返回值 < 0 表示 one 大于 two 第一个元素应该排在第二个之前。 == 0 表示 one 等于 two 两个元素排序位置相同(相对顺序不变)。 > 0 表示 one 小于 two 第一个元素应该排在第二个之后。
参数 const ngx_queue_t *one, const ngx_queue_t *two 需要比较排序的 2 个链表节点

2 逻辑流程

1 局部变量 2 比较 2-1 noname 排最后 2-2 named 排倒数第二 2-3 正则 排倒数第三 2-4 普通前缀匹配

1 局部变量
{ngx_int_trc;ngx_http_core_loc_conf_t*first,*second;ngx_http_location_queue_t*lq1,*lq2;lq1=(ngx_http_location_queue_t*)one;lq2=(ngx_http_location_queue_t*)two;first=lq1->exact?lq1->exact:lq1->inclusive;second=lq2->exact?lq2->exact:lq2->inclusive;

2 比较
2-1 noname 排最后
if(first->noname&&!second->noname){/* shift no named locations to the end */return1;}if(!first->noname&&second->noname){/* shift no named locations to the end */return-1;}if(first->noname||second->noname){/* do not sort no named locations */return0;}
确保有明确 URI 名称的 location 排在没有名称的 location 之前

2-2 named 排倒数第二
if(first->named&&!second->named){/* shift named locations to the end */return1;}if(!first->named&&second->named){/* shift named locations to the end */return-1;}if(first->named&&second->named){returnngx_strcmp(first->name.data,second->name.data);}

2-3 正则 排倒数第三
#if(NGX_PCRE)if(first->regex&&!second->regex){/* shift the regex matches to the end */return1;}if(!first->regex&&second->regex){/* shift the regex matches to the end */return-1;}if(first->regex||second->regex){/* do not sort the regex matches */return0;}#endif

2-4 普通前缀匹配
rc=ngx_filename_cmp(first->name.data,second->name.data,ngx_min(first->name.len,second->name.len)+1);if(rc==0&&!first->exact_match&&second->exact_match){/* an exact match must be before the same inclusive one */return1;}returnrc;}
精确匹配在前
http://www.jsqmd.com/news/555918/

相关文章:

  • 腾讯混元翻译模型HY-MT1.5-1.8B部署避坑指南,新手必看
  • 从Windows转战麒麟系统?这份Kylin-Desktop-V10-SP1外设配置指南帮你无缝衔接
  • Janus-Pro-7B案例集:10类真实图片输入下的高质量多轮响应
  • 告别JSP!用Mustache.java轻松构建轻量级Web页面(Spring Boot集成指南)
  • 告别环境配置焦虑:手把手教你用CMake和VS2019编译ProtoBuf C++开发库(附完整项目配置)
  • 吊打OpenClaw!国产AI助理MindX开源:Token消耗砍至10%,还能养出专属数字分身
  • Linux g++编译与GDB调试完整流程(文末附图)
  • 2024年图片识别新方案:FastAPI+Streamlit+LangChain实战解析
  • Alibaba DASD-4B Thinking 对话工具应用:自动化软件测试用例生成与评审
  • Java操作SFTP实现文件传输的安全方案
  • R语言实战:在boxplot中巧妙添加连线展示时序变化
  • 零基础部署计算机视觉标注工具CVAT:从环境配置到团队协作全指南
  • 攻克5090多卡部署:Docker化vLLM推理服务的实战避坑指南
  • 猫抓cat-catch:浏览器媒体资源捕获的全栈技术指南
  • 主治通关,选对课程少走弯路 - 医考机构品牌测评专家
  • 大厂面试真题汇总:涵盖Java中22个主流技术栈!
  • HY-Motion 1.0在网络安全教学中的虚拟演练应用
  • 如何释放键盘潜能?探索修饰键自定义的效率革命
  • STM32F103启动文件全解析:从官方库到Keil工程配置(含常见错误解决)
  • AI结对编程:利用快马平台智能助手深度理解和优化PyTorch代码
  • Pi0具身智能LaTeX文档生成:科研论文自动化排版
  • 卧式柱塞泵泵体夹具设计【说明书+CAD图纸+外文翻译+过程卡】
  • OpenClaw环境隔离方案:百川2-13B专用Python虚拟环境配置
  • 主治备考,谁是你通关的“引路人”? - 医考机构品牌测评专家
  • Qwen3-TTS语音合成零基础教程:5分钟部署,10种语言自由切换
  • 如何用MicroSIP实现远程团队高效沟通?这些高级配置技巧你知道吗?
  • 医疗护理员/医疗护理技术培训,卫健委相关机构认证,守嘉正规教学 - 品牌排行榜单
  • DeerFlow深度研究框架:5分钟快速上手指南与完整功能解析
  • 数据架构现代化:AI应用落地的关键突破口
  • MiroFish群体智能引擎:突破多智能体通信边界的创新之路