A router based on openresty, with an API to update the mappers.
$ make all
Please refer to Makefile, nginx/Dockerfile, api/Dockerfile for more details.
Suppose the urls on docker cluster are:
http://ngxr-app.your-domain.com/ (the proxy router)
http://ngxr-api.your-domain.com/ (the api)
There are two apis currently: /api and /db, every update(add or del) of mapper will take some time (the 'NGXR_DB_DELAY' in nginx/Dockerfile) to take effect.
/api:
This is to update the mappers.
/api/conf: need to set status to "OK" to active the mappers.
$ curl 'http://ngxr-api.your-domain.com/api/conf?status=OK'
/api/add
$ curl 'http://ngxr-api.your-domain.com/api/add?pattern=/robots.txt&target=www.baidu.com'
[
{
"pattern": "/robots.txt",
"target": "www.baidu.com",
"status": "OK"
}
]
$ curl -H 'Content-Type:application/json' -d '[{"pattern": "/good","target": "y.com"},{"pattern": "bad","target": "x.com"},{"pattern": "/foo/","target": "127.0.0.1:8081"},{"pattern": "/bar","target": "127.0.0.1:8081"}]' 'http://ngxr-api.your-domain.com/api/add'
[
{
"pattern": "/good",
"target": "y.com",
"status": "OK"
},
{
"pattern": "bad",
"target": "x.com",
"status": "BAD"
},
{
"pattern": "/foo/",
"target": "127.0.0.1:8081",
"status": "OK"
},
{
"pattern": "/bar",
"target": "127.0.0.1:8081",
"status": "OK"
}
]
/api/list
$ curl 'http://ngxr-api.your-domain.com/api/list'
/api/del
$ curl 'http://ngxr-api.your-domain.com/api/del?pattern=/foo'
/db: This is for access leveldb directly.
/db/keys
$ curl 'http://ngxr-api.your-domain.com/db/keys?p=.*'
/db/get
$ curl 'http://ngxr-api.your-domain.com/db/get?k=ngxr:ptn:/foo/'
/db/set
$ curl 'http://ngxr-api.your-domain.com/db/set?k=x&v=y'
$ curl -H 'Content-Type:application/json' -d '{"k":"ngxr:conf:status","v":"OK"}' 'http://ngxr-api.your-domain.com/db/set'
/db/del
$ curl 'http://ngxr-api.your-domain.com/db/del?k=ngxr:conf:status'
/db/keys
$ curl 'http://ngxr-api.your-domain.com/db/keys'
$ curl 'http://ngxr-api.your-domain.com/db/keys?p=ngxr:conf:.*'
/db/dump
$ curl 'http://ngxr-api.your-domain.com/db/dump'
$ curl 'http://ngxr-api.your-domain.com/db/dump?p=ngxr:ptn:/.*'
- cache will be updated immediately on the start of the nginx, and set a update routine job running on the backend.
- cache will not out of date, until the next update job set the update time to the NGXR_DB_DELAY.
- when successfully connected to db, set the out-of-date time of current cache to be NGXR_DB_DELAY, then read the mappers again.
- uri will started of a "/", and /xxx/ will be effective to /xxx/, /xxx/yyy, /xxx/yyy/zzz, /xxx will only match itself exactly.
- for example, give a "/xxx/yyy/zzz": if there is an exactly mapper in the cache, will use it, if not, will use the father path "/xxx/yyy/" to do the next match, util "/".
- once found a mapper which not appears in cache via the "father path match", it will be put into cache; if there is no match at all, a default match will be put into cache to avoid next match loop, which will get out-of-date after the next cache update job.
- default match is to 127.0.0.1:8081, which will only return an error message.
use nginx_redis.conf and set up a redis service, do not need api and http_level.lua.
Liu Chong mail@clojure.cn