APISIX 路由优先级
文章目录
- APISIX 路由优先级
- 匹配维度
- radixtree_uri 和 radixtree_host_uri
- 如何判断你当前实例用的是哪种主索引
- 路由 RadixTree
APISIX 路由优先级
在 APISIX 的路由匹配机制中,优先级的判定犹如一场精密的竞赛裁决,直接决定着哪个路由规则能够在众多候选者中脱颖而出,成功匹配请求。深入理解优先级的判定依据,对于优化路由配置、确保系统行为的精准性至关重要。
匹配维度
APISIX 的 HTTP 路由典型可用的匹配维度包括:
- hosts/host:域名或域名列表
- uri/uris:路径(可包含通配符或更复杂匹配方式)
- methods:HTTP 方法列表
- remote_addr/remote_addrs:客户端 IP
- vars:基于 NGINX 变量的表达式(如 Header、Query、Cookie 等条件)
- priority:优先级,决定命中顺序
radixtree_uri 和 radixtree_host_uri
-
radixtree_uri
主索引只有 uri。匹配排序遵循:uri 全等 > 长前缀 > 短前缀 > priority > 配置顺序
host、headers、query、cookie、method、IP 等都只是“过滤条件”,不参与排序加权结果:同一条请求,如果两条路由 uri 层级并列,才会比 priority;host 精确与通配不影响排序
-
radixtree_host_uri
主索引是 host + uri。host 也参与“越具体越优先”的排序
先比较 host 的具体程度(精确优于通配),再比较 uri(全等 > 长前缀 > 短前缀),其后才是 priority > 配置顺序结果:对同一路径,host 精确匹配能“压住”通配 host,即便通配 host 的路由 priority 更高
如何判断你当前实例用的是哪种主索引
看配置文件(最直接)
APISIX 3.x(及较新版本)通常在 conf/config.yaml:
apisix:router:http: radixtree_uri # 或 radixtree_host_uri
看 Helm/容器化参数
使用官方 Helm Chart 时,values.yaml 一般有 apisix.router.http 配置项
apisix:router:# -- Defines how apisix handles routing:# - radixtree_uri: match route by uri(base on radixtree)# - radixtree_host_uri: match route by host + uri(base on radixtree)# - radixtree_uri_with_parameter: match route by uri with parametershttp: radixtree_host_uri
路由 RadixTree
官网:https://apisix.apache.org/zh/docs/apisix/router-radixtree/