Git Product home page Git Product logo

lua-resty-router's Introduction

lua-resty-router

A simple dynamic request router for nginx. Pluggable backends for flexibility; ngx.shared.DICT caching for performance.

Usage

Install openresty and put lua-resty-router in lualib. Install and run the following nginx.conf:

worker_processes  2;
error_log logs/error.log info;

events {
  worker_connections 1024;
}

http {
  lua_shared_dict locks 1m;
  lua_shared_dict cache_dict 1m;
  lua_package_path "/usr/local/openresty-debug/lualib/?.lua;/usr/local/openresty/lualib/?.lua;;";
  lua_package_cpath "/usr/local/openresty-debug/lualib/?.so;/usr/local/openresty/lualib/?.so;;";

  server {
    listen 8888;
    location / {
      set $rr_route '';
      set $rr_status 'NOTFOUND';
      rewrite_by_lua '
        local router = require "resty.router"
        local r = router:new(
          "resty.router.dns",
          {
            dns_opts = {
              nameservers = { "8.8.8.8" }
            }
          }
        )
        local route = r:get_route(ngx.var.arg_host)
        if not route then
            return ngx.exit(404)
        end
        ngx.var.rr_route = route
        ngx.var.rr_status = ngx.ctx.shcache["resty_router_cache"].cache_status
      ';
      more_set_headers "X-RR-Key: $arg_host";
      more_set_headers "X-RR-State: $rr_status";
      more_set_headers "X-RR-Route: $rr_route";
      proxy_pass http://$rr_route;
    }
  }
}

This example uses the host arg as the key, but could just as easily use any HTTP header, host, or path part.

curl -I http://localhost:8888/?host=example.com

A more inventive approach for a microservices architecture is to use SRV records alongside a same-named record for the router. The careful reader will note our SRV record format is wanting a prefix like "_Service._Proto.". For applications using private DNS this seems unnecessary.

# example-service.example.com  IN CNAME  router.example.com.
# example-service.example.com  IN SRV    1 1 5000 10.1.1.1
# example-service.example.com  IN SRV    1 1 5000 10.1.1.2
# example-service.example.com  IN SRV    1 1 5000 10.1.1.3
...
location / {
  set $rr_route '';
  set $rr_status 'NOTFOUND';
  rewrite_by_lua '
    local router = require "resty.router"
    local dns = require "resty.dns.resolver"
    local r = router:new(
      "resty.router.dns",
      {
        dns_opts = {
          nameservers = { "8.8.8.8" },
          qtype = dns.TYPE_SRV
        }
      }
    )
    local route = r:get_route(ngx.var.http_host)
    if not route then
        return ngx.exit(404)
    end
    ngx.var.rr_route = route
    ngx.var.rr_status = ngx.ctx.shcache["resty_router_cache"].cache_status
  ';
  more_set_headers "X-RR-Key: $arg_host";
  more_set_headers "X-RR-State: $rr_status";
  more_set_headers "X-RR-Route: $rr_route";
  proxy_pass http://$rr_route;
}
...

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.