ngx_http_cmp_conf_addrs
1 定义ngx_http_cmp_conf_addrs 函数 定义在 ./nginx-1.24.0/src/http/ngx_http.cstaticngx_int_tngx_http_cmp_conf_addrs(constvoid*one,constvoid*two){ngx_http_conf_addr_t*first,*second;first(ngx_http_conf_addr_t*)one;second(ngx_http_conf_addr_t*)two;if(first-opt.wildcard){/* a wildcard address must be the last resort, shift it to the end */return1;}if(second-opt.wildcard){/* a wildcard address must be the last resort, shift it to the end */return-1;}if(first-opt.bind!second-opt.bind){/* shift explicit bind()ed addresses to the start */return-1;}if(!first-opt.bindsecond-opt.bind){/* shift explicit bind()ed addresses to the start */return1;}/* do not sort by default */return0;}ngx_http_cmp_conf_addrs 是 Nginx 用于 对 HTTP 监听地址listen 配置解析结果进行优先级排序的比较回调函数。 它按照 显式 bind 的地址 → 普通明确 IP 地址 → 通配符地址如 0.0.0.0/:: 的规则重排地址数组。 核心作用 确保精确 IP 优先绑定与匹配避免底层端口冲突 同时将通配符地址强制置末作为兜底default_server 从而提升新连接的路由匹配效率并保障虚拟主机配置语义的正确性。2 详解1 函数签名staticngx_int_tngx_http_cmp_conf_addrs(constvoid*one,constvoid*two)返回值 负值如 -1表示 one 小于 two 零表示 one 和 two 相等 正值如 1表示 one 大于 two参数 const void *one, const void *two 当前正在比较的两个元素2 逻辑流程1 局部变量 2 比较1 局部变量{ngx_http_conf_addr_t*first,*second;first(ngx_http_conf_addr_t*)one;second(ngx_http_conf_addr_t*)two;强制类型转换 转换为具体类型才能够访问其成员字段2 比较if(first-opt.wildcard){/* a wildcard address must be the last resort, shift it to the end */return1;}if(second-opt.wildcard){/* a wildcard address must be the last resort, shift it to the end */return-1;}if(first-opt.bind!second-opt.bind){/* shift explicit bind()ed addresses to the start */return-1;}if(!first-opt.bindsecond-opt.bind){/* shift explicit bind()ed addresses to the start */return1;}/* do not sort by default */return0;}#1 所有非通配符地址排在通配符地址之前 #2 在非通配符地址内部有 bind 标志的地址优先于无 bind 标志的地址 #3 其他情况视为相等不交换顺序
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2490492.html
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!