Skip to content

A http dynamic routing example based on openresty and docker.

License

Notifications You must be signed in to change notification settings

liuchong/ngx-router-example

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

ngx-router

FOSSA Status

A router based on openresty, with an API to update the mappers.

Quick start

Deploy:

$ 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)

Use 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:/.*'

logic of nginx router the proxy:

  1. cache will be updated immediately on the start of the nginx, and set a update routine job running on the backend.
  2. cache will not out of date, until the next update job set the update time to the NGXR_DB_DELAY.
  3. when successfully connected to db, set the out-of-date time of current cache to be NGXR_DB_DELAY, then read the mappers again.
  4. uri will started of a "/", and /xxx/ will be effective to /xxx/, /xxx/yyy, /xxx/yyy/zzz, /xxx will only match itself exactly.
  5. 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 "/".
  6. 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.
  7. default match is to 127.0.0.1:8081, which will only return an error message.

redis version

use nginx_redis.conf and set up a redis service, do not need api and http_level.lua.

Author

Liu Chong mail@clojure.cn

License

MIT

FOSSA Status

About

A http dynamic routing example based on openresty and docker.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published