Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -1161,6 +1161,7 @@ Directives
* [init_worker_by_lua_file](#init_worker_by_lua_file)
* [exit_worker_by_lua_block](#exit_worker_by_lua_block)
* [exit_worker_by_lua_file](#exit_worker_by_lua_file)
* [server_name_resolver_by_lua_block](#server_name_resolver_by_lua_block)
* [set_by_lua](#set_by_lua)
* [set_by_lua_block](#set_by_lua_block)
* [set_by_lua_file](#set_by_lua_file)
Expand Down Expand Up @@ -1757,6 +1758,54 @@ This directive was first introduced in the `v0.10.18` release.

[Back to TOC](#directives)

server_name_resolver_by_lua_block
---------------------------------

**syntax:** *server_name_resolver_by_lua_block { lua-script }*

**context:** *http*

**phase:** *server-selection*

Runs the specified Lua code when Nginx selects a `server {}` block for a host name that matches no exact [server_name](http://nginx.org/en/docs/http/ngx_http_core_module.html#server_name). The host name is passed to the Lua code as its only argument (`...`), in lower case. The code returns the `server_name` that should serve the host, or `nil` to decline.

Nginx then selects the server in this order:

1. an exact `server_name`; the Lua code is not run
1. the name returned by the Lua code, when it is an exact `server_name` on the same listening address; any other value is ignored
1. a wildcard `server_name`
1. a regular expression `server_name`
1. the default server

The code runs both for the TLS SNI name during the handshake and for the `Host` request header, so the selected server answers the handshake with its own certificate. When the SNI name selected a server and the `Host` of a request equals it, the code is not run again for that request.

For example,

```nginx

lua_shared_dict aliases 10m;

server_name_resolver_by_lua_block {
return ngx.shared.aliases:get(...)
}

server {
listen 443 ssl;
server_name tenant-1.example.com;

# also serves every host name stored in the "aliases"
# shared dict with the value "tenant-1.example.com"
}
```

The Lua code runs inside server selection and cannot yield, so Lua APIs that yield (cosockets, `ngx.sleep`, etc.) are disabled in this context. It should answer from memory, for example from [ngx.shared.DICT](#ngxshareddict) or a Lua table. A Lua error in the code is logged and treated as a decline.

Only one `server_name_resolver_by_lua_block` is allowed.

This directive requires an Nginx core that provides the `server_name_resolver` hook in `ngx_http_core_main_conf_t` and clears the `reusable` flag in HTTP/2 stream connections. Without both, the directive is rejected when the configuration is loaded.

[Back to TOC](#directives)

set_by_lua
----------

Expand Down Expand Up @@ -9013,6 +9062,8 @@ Retrieves the current running phase name. Possible return values are
for the context of user callback functions for [ngx.timer.*](#ngxtimerat).
* `exit_worker`
for the context of [exit_worker_by_lua*](#exit_worker_by_lua).
* `server_name_resolver`
for the context of [server_name_resolver_by_lua_block](#server_name_resolver_by_lua_block).

This API was first introduced in the `v0.5.10` release.

Expand Down
16 changes: 16 additions & 0 deletions config
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,7 @@ HTTP_LUA_SRCS=" \
$ngx_addon_dir/src/ngx_http_lua_util.c \
$ngx_addon_dir/src/ngx_http_lua_cache.c \
$ngx_addon_dir/src/ngx_http_lua_precontentby.c \
$ngx_addon_dir/src/ngx_http_lua_server_name_resolverby.c \
$ngx_addon_dir/src/ngx_http_lua_contentby.c \
$ngx_addon_dir/src/ngx_http_lua_server_rewriteby.c \
$ngx_addon_dir/src/ngx_http_lua_rewriteby.c \
Expand Down Expand Up @@ -330,6 +331,7 @@ HTTP_LUA_DEPS=" \
$ngx_addon_dir/src/ngx_http_lua_util.h \
$ngx_addon_dir/src/ngx_http_lua_cache.h \
$ngx_addon_dir/src/ngx_http_lua_precontentby.h \
$ngx_addon_dir/src/ngx_http_lua_server_name_resolverby.h \
$ngx_addon_dir/src/ngx_http_lua_contentby.h \
$ngx_addon_dir/src/ngx_http_lua_server_rewriteby.h \
$ngx_addon_dir/src/ngx_http_lua_rewriteby.h \
Expand Down Expand Up @@ -494,6 +496,20 @@ CC_TEST_FLAGS="$SAVED_CC_TEST_FLAGS"

# ----------------------------------------

# server_name_resolver_by_lua_block needs two things from the nginx core:
# the server_name_resolver hook (server selection), and HTTP/2 stream
# connections created with the reusable flag cleared

if grep 'server_name_resolver' src/http/ngx_http_core_module.h > /dev/null 2>&1
then
if grep 'fc->reusable = 0;' src/http/v2/ngx_http_v2.c > /dev/null 2>&1
then
have=NGX_HTTP_LUA_HAVE_SERVER_NAME_RESOLVER . auto/have
fi
fi

# ----------------------------------------

if [ -n "$ngx_module_link" ]; then
ngx_module_type=HTTP_AUX_FILTER
ngx_module_name=$ngx_addon_name
Expand Down
46 changes: 46 additions & 0 deletions doc/HttpLuaModule.wiki
Original file line number Diff line number Diff line change
Expand Up @@ -1389,6 +1389,50 @@ Similar to [[#exit_worker_by_lua_block|exit_worker_by_lua_block]], but accepts t

This directive was first introduced in the <code>v0.10.18</code> release.

== server_name_resolver_by_lua_block ==

'''syntax:''' ''server_name_resolver_by_lua_block { lua-script }''

'''context:''' ''http''

'''phase:''' ''server-selection''

Runs the specified Lua code when Nginx selects a <code>server {}</code> block for a host name that matches no exact [http://nginx.org/en/docs/http/ngx_http_core_module.html#server_name server_name]. The host name is passed to the Lua code as its only argument (<code>...</code>), in lower case. The code returns the <code>server_name</code> that should serve the host, or <code>nil</code> to decline.

Nginx then selects the server in this order:

# an exact <code>server_name</code>; the Lua code is not run
# the name returned by the Lua code, when it is an exact <code>server_name</code> on the same listening address; any other value is ignored
# a wildcard <code>server_name</code>
# a regular expression <code>server_name</code>
# the default server

The code runs both for the TLS SNI name during the handshake and for the <code>Host</code> request header, so the selected server answers the handshake with its own certificate. When the SNI name selected a server and the <code>Host</code> of a request equals it, the code is not run again for that request.

For example,

<geshi lang="nginx">
lua_shared_dict aliases 10m;

server_name_resolver_by_lua_block {
return ngx.shared.aliases:get(...)
}

server {
listen 443 ssl;
server_name tenant-1.example.com;

# also serves every host name stored in the "aliases"
# shared dict with the value "tenant-1.example.com"
}
</geshi>

The Lua code runs inside server selection and cannot yield, so Lua APIs that yield (cosockets, <code>ngx.sleep</code>, etc.) are disabled in this context. It should answer from memory, for example from [[#ngx.shared.DICT|ngx.shared.DICT]] or a Lua table. A Lua error in the code is logged and treated as a decline.

Only one <code>server_name_resolver_by_lua_block</code> is allowed.

This directive requires an Nginx core that provides the <code>server_name_resolver</code> hook in <code>ngx_http_core_main_conf_t</code> and clears the <code>reusable</code> flag in HTTP/2 stream connections. Without both, the directive is rejected when the configuration is loaded.

== set_by_lua ==

'''syntax:''' ''set_by_lua $res <lua-script-str> [$arg1 $arg2 ...]''
Expand Down Expand Up @@ -7359,6 +7403,8 @@ Retrieves the current running phase name. Possible return values are
: for the context of user callback functions for [[#ngx.timer.at|ngx.timer.*]].
* <code>exit_worker</code>
: for the context of [[#exit_worker_by_lua|exit_worker_by_lua*]].
* <code>server_name_resolver</code>
: for the context of [[#server_name_resolver_by_lua_block|server_name_resolver_by_lua_block]].

This API was first introduced in the <code>v0.5.10</code> release.

Expand Down
7 changes: 7 additions & 0 deletions src/ngx_http_lua_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,8 @@ typedef struct {
#define NGX_HTTP_LUA_CONTEXT_PROXY_SSL_VERIFY 0x00010000
#define NGX_HTTP_LUA_CONTEXT_PRECONTENT 0x00020000
#define NGX_HTTP_LUA_CONTEXT_PROXY_SSL_CERT 0x00040000
#define NGX_HTTP_LUA_CONTEXT_SERVER_NAME_RESOLVER \
0x00080000

#define NGX_HTTP_LUA_FFI_NO_REQ_CTX -100
#define NGX_HTTP_LUA_FFI_BAD_CONTEXT -101
Expand Down Expand Up @@ -288,6 +290,11 @@ struct ngx_http_lua_main_conf_s {
ngx_str_t exit_worker_src;
u_char *exit_worker_chunkname;

ngx_str_t server_name_resolver_src;
u_char *server_name_resolver_src_key;
u_char *server_name_resolver_chunkname;
int server_name_resolver_src_ref;

ngx_chain_t *body_filter_chain;
/* neither yielding nor recursion is possible in
* body_filter_by_lua*, so there cannot be any races among
Expand Down
14 changes: 14 additions & 0 deletions src/ngx_http_lua_module.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
#include "ngx_http_lua_headers_out.h"
#if !(NGX_WIN32)
#include "ngx_http_lua_pipe.h"
#include "ngx_http_lua_server_name_resolverby.h"
#endif

/*
Expand Down Expand Up @@ -277,6 +278,13 @@ static ngx_command_t ngx_http_lua_cmds[] = {
0,
(void *) ngx_http_lua_init_worker_by_file },

{ ngx_string("server_name_resolver_by_lua_block"),
NGX_HTTP_MAIN_CONF|NGX_CONF_BLOCK|NGX_CONF_NOARGS,
ngx_http_lua_server_name_resolver_by_lua_block,
NGX_HTTP_MAIN_CONF_OFFSET,
0,
NULL },

{ ngx_string("exit_worker_by_lua_block"),
NGX_HTTP_MAIN_CONF|NGX_CONF_BLOCK|NGX_CONF_NOARGS,
ngx_http_lua_exit_worker_by_lua_block,
Expand Down Expand Up @@ -894,6 +902,12 @@ ngx_http_lua_init(ngx_conf_t *cf)

cmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_core_module);

#ifdef NGX_HTTP_LUA_HAVE_SERVER_NAME_RESOLVER
if (lmcf->server_name_resolver_src_key) {
cmcf->server_name_resolver = ngx_http_lua_server_name_resolver;
}
#endif

if (lmcf->requires_server_rewrite) {
h = ngx_array_push(
&cmcf->phases[NGX_HTTP_SERVER_REWRITE_PHASE].handlers);
Expand Down
Loading
Loading