Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 42 additions & 2 deletions docs/en/latest/router-radixtree.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,8 @@ $ curl http://127.0.0.1:9180/apisix/admin/routes/2 -H "X-API-KEY: $admin_key" -X
Test:

```shell
curl http://127.0.0.1:1980/hello
1980
curl http://127.0.0.1:9080/hello
# 1980
```

All requests will only hit the route of port `1980` because it has a priority of 3 while the route with the port of `1981` has a priority of 2.
Expand Down Expand Up @@ -196,6 +196,46 @@ will match both `/blog/dog` and `/blog/cat`.

Named parameters match one path segment. A trailing wildcard parameter matches the remaining path segments.

When two parameterized routes match the same request, use `priority` to
choose which route handles it. Give `/languages/:tenant/all` a higher priority
than `/languages/:tenant/:language` to send requests ending in `/all` to the
route with the literal segment. The routes below use the `mocking` Plugin
to show which one matched.

```shell
curl http://127.0.0.1:9180/apisix/admin/routes/languages-one \
-H "X-API-KEY: $admin_key" -X PUT -d '
{
"uri": "/languages/:tenant/:language",
"methods": ["GET"],
"priority": 0,
"plugins": {
"mocking": {"content_type": "text/plain", "response_example": "one language"}
}
}'

curl http://127.0.0.1:9180/apisix/admin/routes/languages-all \
-H "X-API-KEY: $admin_key" -X PUT -d '
{
"uri": "/languages/:tenant/all",
"methods": ["GET"],
"priority": 10,
"plugins": {
"mocking": {"content_type": "text/plain", "response_example": "all languages"}
}
}'
```

Send both requests through APISIX.

```shell
curl http://127.0.0.1:9080/languages/acme/all
# all languages

curl http://127.0.0.1:9080/languages/acme/en
# one language
```

By default, an URL-encoded slash (`%2F`) inside a parameter is decoded by Nginx
into a real `/` before route matching, so a request like `/blog/cat%2Fdog` is
treated as `/blog/cat/dog` and does not match `/blog/:name`. To keep `%2F`
Expand Down
42 changes: 40 additions & 2 deletions docs/zh/latest/router-radixtree.md
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,8 @@ $ curl http://127.0.0.1:9180/apisix/admin/routes/2 -H "X-API-KEY: $admin_key" -X
测试:

```shell
curl http://127.0.0.1:1980/hello
1980
curl http://127.0.0.1:9080/hello
# 1980
```

所有请求只到达端口 `1980` 的路由。
Expand Down Expand Up @@ -210,6 +210,44 @@ apisix:

此时将匹配 `/blog/dog` 和 `/blog/cat`。

两条参数路由都能匹配同一请求时,可以通过 `priority` 指定由哪条路由处理。
例如,将 `/languages/:tenant/all` 的优先级设得比 `/languages/:tenant/:language` 高,
以 `/all` 结尾的请求就会命中带字面量路径段的路由。下面的路由使用 `mocking` 插件显示匹配结果。

```shell
curl http://127.0.0.1:9180/apisix/admin/routes/languages-one \
-H "X-API-KEY: $admin_key" -X PUT -d '
{
"uri": "/languages/:tenant/:language",
"methods": ["GET"],
"priority": 0,
"plugins": {
"mocking": {"content_type": "text/plain", "response_example": "one language"}
}
}'

curl http://127.0.0.1:9180/apisix/admin/routes/languages-all \
-H "X-API-KEY: $admin_key" -X PUT -d '
{
"uri": "/languages/:tenant/all",
"methods": ["GET"],
"priority": 10,
"plugins": {
"mocking": {"content_type": "text/plain", "response_example": "all languages"}
}
}'
```

通过 APISIX 发送以下请求。

```shell
curl http://127.0.0.1:9080/languages/acme/all
# all languages

curl http://127.0.0.1:9080/languages/acme/en
# one language
```

更多使用方式请参考:[lua-resty-radixtree#parameters-in-path](https://github.com/api7/lua-resty-radixtree/#parameters-in-path)

默认情况下,参数中的 URL 编码斜杠(`%2F`)会被 Nginx 解码为真实的 `/` 后再进行路由匹配,因此像 `/blog/cat%2Fdog` 这样的请求会被当作 `/blog/cat/dog`,无法匹配 `/blog/:name`。如果希望在匹配时保留 `%2F` 编码(即把它作为参数值的一部分,而不是路径分隔符),可以启用 `match_uri_encoded_slash`:
Expand Down
Loading