libradixtree

what’s libradixtree?

libradixtree, adaptive radix trees implemented in Lua for OpenResty.

APISIX using libradixtree as route dispatching library.

How to use libradixtree in APISIX?

This is Lua-Openresty implementation library base on FFI for rax.

Let’s take a look at a few examples and have an intuitive understanding.

1. Full match

  1. /blog/foo

It will only match /blog/foo.

2. Prefix matching

  1. /blog/bar*

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

3. Match priority

Full match -> 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

How to filter route by Nginx builtin variable

Please take a look at radixtree-new, here is an simple example:

  1. $ curl http://127.0.0.1:9080/apisix/admin/routes/1 -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -i -d '
  2. {
  3. "uri": "/index.html",
  4. "vars": [
  5. ["http_host", "iresty.com"],
  6. ["cookie__device_id", "a66f0cdc4ba2df8c096f74c9110163a9"],
  7. ["arg_name", "jack"]
  8. ],
  9. "upstream": {
  10. "type": "roundrobin",
  11. "nodes": {
  12. "39.97.63.215:80": 1
  13. }
  14. }
  15. }'

This route will require the request header host equal iresty.com, request cookie key _device_id equal a66f0cdc4ba2df8c096f74c9110163a9 etc.