Is it possible to use multiple `init_worker_by_lua_block` calls across nginx configuration files?

367 views
Skip to first unread message

Jose Diaz-Gonzalez

unread,
Jul 13, 2016, 3:41:29 AM7/13/16
to openresty-en
At my dayjob, we currently use an internal haproxy installation to perform service discovery for our developers. When a developer wants a service, they hit `http://localhost:SERVICE_PORT` and the request will be proxied along to the downstream service on a different server. I'd like to replace this with nginx for a variety of reasons, and specifically with the lua-resty-upstream-healthcheck library.

One thing that we're currently doing is we use consul to provide upstreams for each service. We'd like to have something like the following:

server {
  listen  80;
  server_name  {{ $.APP }}.service.localhost;

  location / {
    proxy_pass  http://upstream-{{ $.APP }};
  }
}

upstream upstream-{{ $.APP }} {{{ range service "APP" }}
  server          {{.Node}} {{.Address}}:{{.Port}} check inter 5000 fastinter 1000 maxconn MAXCONN slowstart SLOWSTART{{ end }}
}

# the size depends on the number of servers in upstream {}:
lua_shared_dict healthcheck-{{ $.APP }} 1m;

init_worker_by_lua_block {
    -- healthcheck stuff here
}

What I'd like to do is to be able to duplicate the `init_worker_by_lua_block` section in each included nginx config as we'll be using consul-template to template out those files for services upon which we depend. From what I can tell, this isn't possible. Is there a way around this limitation, perhaps by requiring all lua files within a directory (the directory scan might be blocking) or something else? In theory we could template out a separate set of `init_worker_by_lua` files out separately and concatenate them/reload nginx every time they are generated, though that seems like a lot of extra work.

Does anyone have any ideas as to how I might tackle this problem? Thanks in advance for any advice :)

Yichun Zhang (agentzh)

unread,
Jul 29, 2016, 4:17:17 PM7/29/16
to openresty-en
Hello!

On Wed, Jul 13, 2016 at 12:41 AM, Jose Diaz-Gonzalez wrote:
>
> What I'd like to do is to be able to duplicate the
> `init_worker_by_lua_block` section in each included nginx config as we'll be
> using consul-template to template out those files for services upon which we
> depend. From what I can tell, this isn't possible. Is there a way around
> this limitation, perhaps by requiring all lua files within a directory (the
> directory scan might be blocking) or something else? In theory we could
> template out a separate set of `init_worker_by_lua` files out separately and
> concatenate them/reload nginx every time they are generated, though that
> seems like a lot of extra work.

Do combinations on the Lua level, instead of on the nginx.conf level,
which is way more flexible and also much more efficient.

Just use multiple Lua modules or even dynamically loaded Lua code from
remote services (like redis or memcached) in a single
init_worker_by_lua* handler.

Regards,
-agentzh
Reply all
Reply to author
Forward
0 new messages