router-radixtree

What is Libradixtree?

Libradixtree is an adaptive radix tree that is implemented in Lua for OpenResty and it is based on FFI for rax. APISIX uses libradixtree as a route dispatching library.

How to use Libradixtree in APISIX?

There are several ways to use Libradixtree in APISIX. Let’s take a look at a few examples and have an intuitive understanding.

1. Full match

  1. /blog/foo

It will only match the full path /blog/foo.

2. Prefix matching

  1. /blog/bar*

It will match the path with the prefix /blog/bar. For example, /blog/bar/a, /blog/bar/b, /blog/bar/c/d/e, /blog/bar etc.

3. Match priority

Full match has a higher priority than deep prefix matching.

Here are the rules:

  1. /blog/foo/*
  2. /blog/foo/a/*
  3. /blog/foo/c/*
  4. /blog/foo/bar
pathMatch result
/blog/foo/bar/blog/foo/bar
/blog/foo/a/b/c/blog/foo/a/
/blog/foo/c/d/blog/foo/c/
/blog/foo/gloo/blog/foo/*
/blog/barnot match

4. Different routes have the same uri

When different routes have the same uri, you can set the priority field of the route to determine which route to match first, or add other matching rules to distinguish different routes.

Note: In the matching rules, the priority field takes precedence over other rules except uri.

  1. Different routes have the same uri but different priority field

Create two routes with different priority values ​​(the larger the value, the higher the priority).

router-radixtree - 图1note

You can fetch the admin_key from config.yaml and save to an environment variable with the following command:

  1. admin_key=$(yq '.deployment.admin.admin_key[0].key' conf/config.yaml | sed 's/"//g')
  1. $ curl http://127.0.0.1:9180/apisix/admin/routes/1 -H "X-API-KEY: $admin_key" -X PUT -d '
  2. {
  3. "upstream": {
  4. "nodes": {
  5. "127.0.0.1:1980": 1
  6. },
  7. "type": "roundrobin"
  8. },
  9. "priority": 3,
  10. "uri": "/hello"
  11. }'
  1. $ curl http://127.0.0.1:9180/apisix/admin/routes/2 -H "X-API-KEY: $admin_key" -X PUT -d '
  2. {
  3. "upstream": {
  4. "nodes": {
  5. "127.0.0.1:1981": 1
  6. },
  7. "type": "roundrobin"
  8. },
  9. "priority": 2,
  10. "uri": "/hello"
  11. }'

Test:

  1. curl http://127.0.0.1:1980/hello
  2. 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.

  1. Different routes have the same uri but different matching conditions

To understand this, look at the example of setting host matching rules:

  1. $ curl http://127.0.0.1:9180/apisix/admin/routes/1 -H "X-API-KEY: $admin_key" -X PUT -d '
  2. {
  3. "upstream": {
  4. "nodes": {
  5. "127.0.0.1:1980": 1
  6. },
  7. "type": "roundrobin"
  8. },
  9. "hosts": ["localhost.com"],
  10. "uri": "/hello"
  11. }'
  1. $ curl http://127.0.0.1:9180/apisix/admin/routes/2 -H "X-API-KEY: $admin_key" -X PUT -d '
  2. {
  3. "upstream": {
  4. "nodes": {
  5. "127.0.0.1:1981": 1
  6. },
  7. "type": "roundrobin"
  8. },
  9. "hosts": ["test.com"],
  10. "uri": "/hello"
  11. }'

Test:

  1. $ curl http://127.0.0.1:9080/hello -H 'host: localhost.com'
  2. 1980
  1. $ curl http://127.0.0.1:9080/hello -H 'host: test.com'
  2. 1981
  1. $ curl http://127.0.0.1:9080/hello
  2. {"error_msg":"404 Route Not Found"}

If the host rule matches, the request hits the corresponding upstream, and if the host does not match, the request returns a 404 message.

5. Parameter match

When radixtree_uri_with_parameter is used, we can match routes with parameters.

For example, with configuration:

  1. apisix:
  2. router:
  3. http: 'radixtree_uri_with_parameter'

route like

  1. /blog/:name

will match both /blog/dog and /blog/cat.

For more details, see https://github.com/api7/lua-resty-radixtree/#parameters-in-path.

How to filter route by Nginx built-in variable?

Nginx provides a variety of built-in variables that can be used to filter routes based on certain criteria. Here is an example of how to filter routes by Nginx built-in variables:

  1. $ curl http://127.0.0.1:9180/apisix/admin/routes/1 -H "X-API-KEY: $admin_key" -X PUT -i -d '
  2. {
  3. "uri": "/index.html",
  4. "vars": [
  5. ["http_host", "==", "iresty.com"],
  6. ["cookie_device_id", "==", "a66f0cdc4ba2df8c096f74c9110163a9"],
  7. ["arg_name", "==", "json"],
  8. ["arg_age", ">", "18"],
  9. ["arg_address", "~~", "China.*"]
  10. ],
  11. "upstream": {
  12. "type": "roundrobin",
  13. "nodes": {
  14. "127.0.0.1:1980": 1
  15. }
  16. }
  17. }'

This route will require the request header host equal iresty.com, request cookie key _device_id equal a66f0cdc4ba2df8c096f74c9110163a9 etc. You can learn more at radixtree-new.

How to filter route by POST form attributes?

APISIX supports filtering route by POST form attributes with Content-Type = application/x-www-form-urlencoded.

We can define the following route:

  1. $ curl http://127.0.0.1:9180/apisix/admin/routes/1 -H "X-API-KEY: $admin_key" -X PUT -i -d '
  2. {
  3. "methods": ["POST", "GET"],
  4. "uri": "/_post",
  5. "vars": [
  6. ["post_arg_name", "==", "json"]
  7. ],
  8. "upstream": {
  9. "type": "roundrobin",
  10. "nodes": {
  11. "127.0.0.1:1980": 1
  12. }
  13. }
  14. }'

The route will be matched when the POST form contains name=json.

How to filter route by GraphQL attributes?

APISIX can handle HTTP GET and POST methods. At the same time, the request body can be a GraphQL query string or JSON-formatted content.

APISIX supports filtering routes by some attributes of GraphQL. Currently, we support:

  • graphql_operation
  • graphql_name
  • graphql_root_fields

For instance, with GraphQL like this:

  1. query getRepo {
  2. owner {
  3. name
  4. }
  5. repo {
  6. created
  7. }
  8. }

Where

  • The graphql_operation is query
  • The graphql_name is getRepo,
  • The graphql_root_fields is ["owner", "repo"]

We can filter such route with:

  1. $ curl http://127.0.0.1:9180/apisix/admin/routes/1 -H "X-API-KEY: $admin_key" -X PUT -i -d '
  2. {
  3. "methods": ["POST", "GET"],
  4. "uri": "/graphql",
  5. "vars": [
  6. ["graphql_operation", "==", "query"],
  7. ["graphql_name", "==", "getRepo"],
  8. ["graphql_root_fields", "has", "owner"]
  9. ],
  10. "upstream": {
  11. "type": "roundrobin",
  12. "nodes": {
  13. "127.0.0.1:1980": 1
  14. }
  15. }
  16. }'

We can verify GraphQL matches in the following three ways:

  1. GraphQL query strings
  1. $ curl -H 'content-type: application/graphql' -X POST http://127.0.0.1:9080/graphql -d '
  2. query getRepo {
  3. owner {
  4. name
  5. }
  6. repo {
  7. created
  8. }
  9. }'
  1. JSON format
  1. $ curl -H 'content-type: application/json' -X POST \
  2. http://127.0.0.1:9080/graphql --data '{"query": "query getRepo { owner {name } repo {created}}"}'
  1. Try GET request match
  1. $ curl -H 'content-type: application/graphql' -X GET \
  2. "http://127.0.0.1:9080/graphql?query=query getRepo { owner {name } repo {created}}" -g

To prevent spending too much time reading invalid GraphQL request body, we only read the first 1 MiB data from the request body. This limitation is configured via:

  1. graphql:
  2. max_size: 1048576

If you need to pass a GraphQL body which is larger than the limitation, you can increase the value in conf/config.yaml.