From 2800b24ba92a26e24de0a94538f7a3d710015d02 Mon Sep 17 00:00:00 2001 From: letian Date: Tue, 13 Dec 2016 09:49:32 +0800 Subject: [PATCH 01/22] add exit_worker_by* feature --- src/ngx_http_lua_common.h | 3 ++ src/ngx_http_lua_directive.c | 58 +++++++++++++++++++++++++++++++ src/ngx_http_lua_directive.h | 4 +++ src/ngx_http_lua_exitworkerby.c | 60 +++++++++++++++++++++++++++++++++ src/ngx_http_lua_exitworkerby.h | 25 ++++++++++++++ src/ngx_http_lua_module.c | 21 ++++++++++++ 6 files changed, 171 insertions(+) create mode 100644 src/ngx_http_lua_exitworkerby.c create mode 100644 src/ngx_http_lua_exitworkerby.h diff --git a/src/ngx_http_lua_common.h b/src/ngx_http_lua_common.h index 1232984911..6e51c69b54 100644 --- a/src/ngx_http_lua_common.h +++ b/src/ngx_http_lua_common.h @@ -226,6 +226,9 @@ struct ngx_http_lua_main_conf_s { ngx_http_lua_main_conf_handler_pt init_worker_handler; ngx_str_t init_worker_src; + ngx_http_lua_main_conf_handler_pt exit_worker_handler; + ngx_str_t exit_worker_src; + ngx_http_lua_balancer_peer_data_t *balancer_peer_data; /* neither yielding nor recursion is possible in * balancer_by_lua*, so there cannot be any races among diff --git a/src/ngx_http_lua_directive.c b/src/ngx_http_lua_directive.c index 6b7ccc7cbe..6b787cd7b1 100644 --- a/src/ngx_http_lua_directive.c +++ b/src/ngx_http_lua_directive.c @@ -1200,6 +1200,64 @@ ngx_http_lua_init_worker_by_lua(ngx_conf_t *cf, ngx_command_t *cmd, } +char * +ngx_http_lua_exit_worker_by_lua_block(ngx_conf_t *cf, ngx_command_t *cmd, + void *conf) +{ + char *rv + ngx_conf_t save; + + save = *cf; + cf->handler = ngx_http_lua_exit_worker_by_lua; + cf->handler_conf = conf; + + rv = ngx_http_lua_conf_lua_block_parse(cf, cmd); + + *cf = save; + + return rv; +} + +char * +ngx_http_lua_exit_worker_by_lua(ngx_conf_t *cf, ngx_command_t *cmd, + void *conf) +{ + u_char *name; + ngx_str_t *value; + ngx_http_lua_main_conf_t *lmcf = conf; + + dd("enter"); + + /* must specify a content handler */ + if (cmd->post == NULL) { + return NGX_CONF_ERROR; + } + + if (lmcf->exit_worker_handler) { + return "is duplicate"; + } + + value = cf->args->elts; + + lmcf->exit_worker_handler = (ngx_http_lua_main_conf_handler_pt) cmd->post; + + if (cmd->pos == ngx_http_lua_exit_worker_by_lua_file) { + name = ngx_http_lua_rebase_path(cf->pool, value[1].data, + value[1].len); + if (name == NULL) { + return NGX_CONF_ERROR; + } + + lmcf->exit_worker_src.data = name; + lmcf->exit_worker_src.len = ngx_strlen(name); + } else { + lmcf->exit_worker_src = value[1]; + } + + return NGX_CONF_OK; +} + + #if defined(NDK) && NDK static ngx_int_t ngx_http_lua_set_by_lua_init(ngx_http_request_t *r) diff --git a/src/ngx_http_lua_directive.h b/src/ngx_http_lua_directive.h index 8555e2f364..adfba12761 100644 --- a/src/ngx_http_lua_directive.h +++ b/src/ngx_http_lua_directive.h @@ -53,6 +53,10 @@ char *ngx_http_lua_init_worker_by_lua_block(ngx_conf_t *cf, ngx_command_t *cmd, void *conf); char *ngx_http_lua_init_worker_by_lua(ngx_conf_t *cf, ngx_command_t *cmd, void *conf); +char *ngx_http_lua_exit_worker_by_lua_block(ngx_conf_t *cf, + ngx_command_t *cmd, void *conf); +char *ngx_http_lua_exit_worker_by_lua(ngx_conf_t *cf, ngx_command_t *cmd, + void *conf); char *ngx_http_lua_code_cache(ngx_conf_t *cf, ngx_command_t *cmd, void *conf); char *ngx_http_lua_load_resty_core(ngx_conf_t *cf, ngx_command_t *cmd, void *conf); diff --git a/src/ngx_http_lua_exitworkerby.c b/src/ngx_http_lua_exitworkerby.c new file mode 100644 index 0000000000..281a7ebbe4 --- /dev/null +++ b/src/ngx_http_lua_exitworkerby.c @@ -0,0 +1,60 @@ + +/* + * Copyright (C) Xianliang Li + */ + + +#ifndef DDEBUG +#define DDEBUG 0 +#endif +#include "ddebug.h" + + +#include "ngx_http_lua_exitworkerby.h" +#include "ngx_http_lua_util.h" + + +ngx_int_t +ngx_http_lua_exit_worker(ngx_cycle_t *cycle) +{ + ngx_http_lua_main_conf_t *lmcf; + + lmcf = ngx_http_cycle_get_module_main_conf(cycle, ngx_http_lua_module); + if (lmcf == NULL + || lmcf->exit_worker_handler == NULL + || lmcf->lua == NULL) + { + return NGX_OK; + } + + (void) lmcf->exit_worker_handler(cycle->log, lmcf, lmcf->lua); + + return NGX_OK; +} + + +ngx_int_t +ngx_http_lua_exit_worker_by_inline(ngx_log_t *log, + ngx_http_lua_main_conf_t *lmcf, lua_State *L) +{ + int status; + + status = luaL_loadbuffer(L, (char *) lmcf->exit_worker_src.data, + lmcf->exit_worker_src.len, "=exit_worker_by_lua" + || ngx_http_lua_do_call(log, L)); + + return ngx_http_lua_report(log, L, status, "exit_worker_by_lua"); +} + + +ngx_int_t +ngx_http_lua_exit_worker_by_file(ngx_log_t *log, + ngx_http_lua_main_conf_t *lmcf, lua_State *L) +{ + int status; + + status = luaL_loadfile(L, (char *) lmcf->exit_worker_src.data) + || ngx_http_lua_do_call(log, L); + + return ngx_http_lua_report(log, L, status, "exit_worker_by_file"); +} diff --git a/src/ngx_http_lua_exitworkerby.h b/src/ngx_http_lua_exitworkerby.h new file mode 100644 index 0000000000..09f07ef186 --- /dev/null +++ b/src/ngx_http_lua_exitworkerby.h @@ -0,0 +1,25 @@ + +/* + * Copyright (C) Xianliang Li + */ + + +#ifndef _NGX_HTTP_LUA_EXITWORKERBY_H_INCLUDE_ +#define _NGX_HTTP_LUA_EXITWORKERBY_H_INCLUDE_ + + +#include "ngx_http_lua_common.h" + + +ngx_int_t ngx_http_lua_exit_worker_by_inline(ngx_log_t *log, + ngx_http_lua_main_conf_t *lmcf, lua_State *L); + +ngx_int_t ngx_http_lua_exit_worker_by_file(ngx_log_t *log, + ngx_http_lua_main_conf_t *lmcf, lua_State *L); + +ngx_int_t ngx_http_lua_exit_worker(ngx_cycle_t *cycle); + + +#endif /* _NGX_HTTP_LUA_EXITWORKERBY_H_INCLUDE_ */ + +/* vi:set ft=c ts=4 sw=4 et fdm=marker: */ diff --git a/src/ngx_http_lua_module.c b/src/ngx_http_lua_module.c index 9816d86441..b90f48bc55 100644 --- a/src/ngx_http_lua_module.c +++ b/src/ngx_http_lua_module.c @@ -229,6 +229,27 @@ static ngx_command_t ngx_http_lua_cmds[] = { 0, (void *) ngx_http_lua_init_worker_by_file }, + { 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, + NGX_HTTP_MAIN_CONF_OFFSET, + 0, + (void *) ngx_http_lua_exit_worker_by_inline }, + + { ngx_string("exit_worker_by_lua"), + NGX_HTTP_MAIN_CONF|NGX_CONF_TAKE1, + ngx_http_lua_exit_worker_by_lua, + NGX_HTTP_MAIN_CONF_OFFSET, + 0, + (void *) ngx_http_lua_exit_worker_by_inline }, + + { ngx_string("exit_worker_by_lua_file"), + NGX_HTTP_MAIN_CONF|NGX_CONF_TAKE1, + ngx_http_lua_exit_worker_by_lua, + NGX_HTTP_MAIN_CONF_OFFSET, + 0, + (void *) ngx_http_lua_exit_worker_by_file }, + #if defined(NDK) && NDK /* set_by_lua_block $res { inline Lua code } */ { ngx_string("set_by_lua_block"), From f890764acf6cb587bcf1cfd60c44606134e51fbf Mon Sep 17 00:00:00 2001 From: letian Date: Mon, 2 Jan 2017 08:30:01 +0800 Subject: [PATCH 02/22] add exit feature --- config | 2 ++ src/ngx_http_lua_directive.c | 7 +++++-- src/ngx_http_lua_exitworkerby.c | 12 ++++++------ src/ngx_http_lua_exitworkerby.h | 4 ++-- src/ngx_http_lua_module.c | 10 ++-------- 5 files changed, 17 insertions(+), 18 deletions(-) diff --git a/config b/config index 18014aa97c..6cb951bb42 100644 --- a/config +++ b/config @@ -278,6 +278,7 @@ HTTP_LUA_SRCS=" \ $ngx_addon_dir/src/ngx_http_lua_bodyfilterby.c \ $ngx_addon_dir/src/ngx_http_lua_initby.c \ $ngx_addon_dir/src/ngx_http_lua_initworkerby.c \ + $ngx_addon_dir/src/ngx_http_lua_exitworkerby.c \ $ngx_addon_dir/src/ngx_http_lua_socket_udp.c \ $ngx_addon_dir/src/ngx_http_lua_req_method.c \ $ngx_addon_dir/src/ngx_http_lua_phase.c \ @@ -339,6 +340,7 @@ HTTP_LUA_DEPS=" \ $ngx_addon_dir/src/ngx_http_lua_bodyfilterby.h \ $ngx_addon_dir/src/ngx_http_lua_initby.h \ $ngx_addon_dir/src/ngx_http_lua_initworkerby.h \ + $ngx_addon_dir/src/ngx_http_lua_exitworkerby.h \ $ngx_addon_dir/src/ngx_http_lua_socket_udp.h \ $ngx_addon_dir/src/ngx_http_lua_probe.h \ $ngx_addon_dir/src/ngx_http_lua_uthread.h \ diff --git a/src/ngx_http_lua_directive.c b/src/ngx_http_lua_directive.c index 6b787cd7b1..949cc15cbb 100644 --- a/src/ngx_http_lua_directive.c +++ b/src/ngx_http_lua_directive.c @@ -23,6 +23,7 @@ #include "ngx_http_lua_bodyfilterby.h" #include "ngx_http_lua_initby.h" #include "ngx_http_lua_initworkerby.h" +#include "ngx_http_lua_exitworkerby.h" #include "ngx_http_lua_shdict.h" #include "ngx_http_lua_ssl_certby.h" #include "ngx_http_lua_lex.h" @@ -1204,7 +1205,7 @@ char * ngx_http_lua_exit_worker_by_lua_block(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) { - char *rv + char *rv; ngx_conf_t save; save = *cf; @@ -1218,6 +1219,7 @@ ngx_http_lua_exit_worker_by_lua_block(ngx_conf_t *cf, ngx_command_t *cmd, return rv; } + char * ngx_http_lua_exit_worker_by_lua(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) @@ -1241,7 +1243,7 @@ ngx_http_lua_exit_worker_by_lua(ngx_conf_t *cf, ngx_command_t *cmd, lmcf->exit_worker_handler = (ngx_http_lua_main_conf_handler_pt) cmd->post; - if (cmd->pos == ngx_http_lua_exit_worker_by_lua_file) { + if (cmd->post == ngx_http_lua_exit_worker_by_file) { name = ngx_http_lua_rebase_path(cf->pool, value[1].data, value[1].len); if (name == NULL) { @@ -1250,6 +1252,7 @@ ngx_http_lua_exit_worker_by_lua(ngx_conf_t *cf, ngx_command_t *cmd, lmcf->exit_worker_src.data = name; lmcf->exit_worker_src.len = ngx_strlen(name); + } else { lmcf->exit_worker_src = value[1]; } diff --git a/src/ngx_http_lua_exitworkerby.c b/src/ngx_http_lua_exitworkerby.c index 281a7ebbe4..f82c74bcbc 100644 --- a/src/ngx_http_lua_exitworkerby.c +++ b/src/ngx_http_lua_exitworkerby.c @@ -1,6 +1,6 @@ /* - * Copyright (C) Xianliang Li + * Copyright (C) Yichun Zhang (agentzh) */ @@ -14,7 +14,7 @@ #include "ngx_http_lua_util.h" -ngx_int_t +void ngx_http_lua_exit_worker(ngx_cycle_t *cycle) { ngx_http_lua_main_conf_t *lmcf; @@ -24,12 +24,12 @@ ngx_http_lua_exit_worker(ngx_cycle_t *cycle) || lmcf->exit_worker_handler == NULL || lmcf->lua == NULL) { - return NGX_OK; + return; } (void) lmcf->exit_worker_handler(cycle->log, lmcf, lmcf->lua); - return NGX_OK; + return; } @@ -40,8 +40,8 @@ ngx_http_lua_exit_worker_by_inline(ngx_log_t *log, int status; status = luaL_loadbuffer(L, (char *) lmcf->exit_worker_src.data, - lmcf->exit_worker_src.len, "=exit_worker_by_lua" - || ngx_http_lua_do_call(log, L)); + lmcf->exit_worker_src.len, "=exit_worker_by_lua") + || ngx_http_lua_do_call(log, L); return ngx_http_lua_report(log, L, status, "exit_worker_by_lua"); } diff --git a/src/ngx_http_lua_exitworkerby.h b/src/ngx_http_lua_exitworkerby.h index 09f07ef186..f904303631 100644 --- a/src/ngx_http_lua_exitworkerby.h +++ b/src/ngx_http_lua_exitworkerby.h @@ -1,6 +1,6 @@ /* - * Copyright (C) Xianliang Li + * Copyright (C) Yichun Zhang (agentzh) */ @@ -17,7 +17,7 @@ ngx_int_t ngx_http_lua_exit_worker_by_inline(ngx_log_t *log, ngx_int_t ngx_http_lua_exit_worker_by_file(ngx_log_t *log, ngx_http_lua_main_conf_t *lmcf, lua_State *L); -ngx_int_t ngx_http_lua_exit_worker(ngx_cycle_t *cycle); +void ngx_http_lua_exit_worker(ngx_cycle_t *cycle); #endif /* _NGX_HTTP_LUA_EXITWORKERBY_H_INCLUDE_ */ diff --git a/src/ngx_http_lua_module.c b/src/ngx_http_lua_module.c index b90f48bc55..098fe87add 100644 --- a/src/ngx_http_lua_module.c +++ b/src/ngx_http_lua_module.c @@ -22,6 +22,7 @@ #include "ngx_http_lua_bodyfilterby.h" #include "ngx_http_lua_initby.h" #include "ngx_http_lua_initworkerby.h" +#include "ngx_http_lua_exitworkerby.h" #include "ngx_http_lua_probe.h" #include "ngx_http_lua_semaphore.h" #include "ngx_http_lua_balancer.h" @@ -236,13 +237,6 @@ static ngx_command_t ngx_http_lua_cmds[] = { 0, (void *) ngx_http_lua_exit_worker_by_inline }, - { ngx_string("exit_worker_by_lua"), - NGX_HTTP_MAIN_CONF|NGX_CONF_TAKE1, - ngx_http_lua_exit_worker_by_lua, - NGX_HTTP_MAIN_CONF_OFFSET, - 0, - (void *) ngx_http_lua_exit_worker_by_inline }, - { ngx_string("exit_worker_by_lua_file"), NGX_HTTP_MAIN_CONF|NGX_CONF_TAKE1, ngx_http_lua_exit_worker_by_lua, @@ -657,7 +651,7 @@ ngx_module_t ngx_http_lua_module = { ngx_http_lua_init_worker, /* init process */ NULL, /* init thread */ NULL, /* exit thread */ - NULL, /* exit process */ + ngx_http_lua_exit_worker, /* exit process */ NULL, /* exit master */ NGX_MODULE_V1_PADDING }; From aa18a59eff733c296e905e5394d68dee1220f24c Mon Sep 17 00:00:00 2001 From: letian Date: Tue, 3 Jan 2017 10:31:18 +0800 Subject: [PATCH 03/22] add exit-worker test and exit_worker_by* the manual --- README.markdown | 39 +++++++++++++++++++++++++++++ t/151-exit-worker.t | 60 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 99 insertions(+) create mode 100644 t/151-exit-worker.t diff --git a/README.markdown b/README.markdown index 4046dfe15f..8105ea30c6 100644 --- a/README.markdown +++ b/README.markdown @@ -1064,6 +1064,8 @@ Directives * [init_worker_by_lua](#init_worker_by_lua) * [init_worker_by_lua_block](#init_worker_by_lua_block) * [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) * [set_by_lua](#set_by_lua) * [set_by_lua_block](#set_by_lua_block) * [set_by_lua_file](#set_by_lua_file) @@ -1578,6 +1580,43 @@ This hook no longer runs in the cache manager and cache loader processes since t [Back to TOC](#directives) +exit_worker_by_lua_block +------------------------ + +**syntax:** *exit_worker_by_lua_block { lua-script }* + +**context:** *http* + +**phase:** *exitting-worker* + +Runs the specified Lua code upon every Nginx worker process's exiting when the master process is enabled. + +This hook is often used to release resources When init_worker_by* create resources, To prevent the worker exit abnormal + +For instance, + +```nginx + + exit_worker_by_lua_block { + print("log from exit_worker_by_lua_block") + } +``` + +[Back to TOC](#directives) + +exit_worker_by_lua_block +------------------------ + +**syntax:** *exit_worker_by_lua_file <lua-file-path>* + +**context:** *http* + +**phase:** *exitting-worker* + +Similar to [exit_worker_by_lua_block](#exit_worker_by_lua_block), but accepts the file path to a Lua source file or Lua bytecode file. + +[Back to TOC](#directives) + set_by_lua ---------- diff --git a/t/151-exit-worker.t b/t/151-exit-worker.t new file mode 100644 index 0000000000..f9e841b08a --- /dev/null +++ b/t/151-exit-worker.t @@ -0,0 +1,60 @@ +# vim:set ft= ts=4 sw=4 et fdm=marker: + +use Test::Nginx::Socket 'no_plan'; +use Test::Nginx::Socket::Lua; + + +#log_level("warn"); +no_long_string(); + +run_tests(); + +__DATA__ + +=== TEST 1: simple exit_worker_by_lua_block +--- http_config + exit_worker_by_lua_block { + ngx.log(ngx.NOTICE, "log from exit_worker_by_lua_block") + -- grep_error_log chop + -- log from exit_worker_by_lua_block + -- --- grep_error_log_out eval + -- ["log from exit_worker_by_lua_block\n", ""] + -- Due to the use grep_error_log and grep_error_log_out is invalid, So use grep verification test(exit_worker_by_lua* run when worker exit) + -- grep "log from exit_worker_by_lua_block" t/servroot/logs/error.log + } +--- config + location /t { + echo "ok"; + } +--- request +GET /t +--- response_body +ok +--- no_error_log +[error] + + + +=== TEST 1: simple exit_worker_by_lua_file +--- http_config + exit_worker_by_lua_file html/exit_worker.lua; + # grep_error_log chop + # log from exit_worker_by_lua_file + # --- grep_error_log_out eval + # ["log from exit_worker_by_lua_file\n", ""] + # Due to the use grep_error_log and grep_error_log_out is invalid, So use grep verification test(exit_worker_by_lua* run when worker exit) + # grep "log from exit_worker_by_lua_file" t/servroot/logs/error.log +--- config + location /t { + echo "ok"; + } +--- user_files +>>> exit_worker.lua +ngx.log(ngx.NOTICE, "log from exit_worker_by_lua_file") +--- request +GET /t +--- response_body +ok +--- no_error_log +[error] + From d0573706277f8632ac135be38e258f0bd6c9df45 Mon Sep 17 00:00:00 2001 From: letian Date: Tue, 3 Jan 2017 10:37:10 +0800 Subject: [PATCH 04/22] When => when --- README.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.markdown b/README.markdown index 8105ea30c6..bf16ecae3c 100644 --- a/README.markdown +++ b/README.markdown @@ -1591,7 +1591,7 @@ exit_worker_by_lua_block Runs the specified Lua code upon every Nginx worker process's exiting when the master process is enabled. -This hook is often used to release resources When init_worker_by* create resources, To prevent the worker exit abnormal +This hook is often used to release resources when init_worker_by* create resources, To prevent the worker exit abnormal For instance, From 5eb077113a4d9fdce30c23c75f86bb222d091417 Mon Sep 17 00:00:00 2001 From: letian Date: Tue, 3 Jan 2017 18:32:29 +0800 Subject: [PATCH 05/22] fix code style --- src/ngx_http_lua_exitworkerby.c | 8 ++++---- src/ngx_http_lua_exitworkerby.h | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/ngx_http_lua_exitworkerby.c b/src/ngx_http_lua_exitworkerby.c index f82c74bcbc..71e08b486b 100644 --- a/src/ngx_http_lua_exitworkerby.c +++ b/src/ngx_http_lua_exitworkerby.c @@ -22,11 +22,11 @@ ngx_http_lua_exit_worker(ngx_cycle_t *cycle) lmcf = ngx_http_cycle_get_module_main_conf(cycle, ngx_http_lua_module); if (lmcf == NULL || lmcf->exit_worker_handler == NULL - || lmcf->lua == NULL) + || lmcf->lua == NULL) { - return; + return; } - + (void) lmcf->exit_worker_handler(cycle->log, lmcf, lmcf->lua); return; @@ -48,7 +48,7 @@ ngx_http_lua_exit_worker_by_inline(ngx_log_t *log, ngx_int_t -ngx_http_lua_exit_worker_by_file(ngx_log_t *log, +ngx_http_lua_exit_worker_by_file(ngx_log_t *log, ngx_http_lua_main_conf_t *lmcf, lua_State *L) { int status; diff --git a/src/ngx_http_lua_exitworkerby.h b/src/ngx_http_lua_exitworkerby.h index f904303631..38d28fe1a0 100644 --- a/src/ngx_http_lua_exitworkerby.h +++ b/src/ngx_http_lua_exitworkerby.h @@ -17,7 +17,7 @@ ngx_int_t ngx_http_lua_exit_worker_by_inline(ngx_log_t *log, ngx_int_t ngx_http_lua_exit_worker_by_file(ngx_log_t *log, ngx_http_lua_main_conf_t *lmcf, lua_State *L); -void ngx_http_lua_exit_worker(ngx_cycle_t *cycle); +void ngx_http_lua_exit_worker(ngx_cycle_t *cycle); #endif /* _NGX_HTTP_LUA_EXITWORKERBY_H_INCLUDE_ */ From 59f94eb0c961602ee0a26e3d1d5bc5c9e62b28f6 Mon Sep 17 00:00:00 2001 From: letian Date: Fri, 6 Jan 2017 12:56:37 +0800 Subject: [PATCH 06/22] fix 151-exit-worker.t base on iresty/test-nginx --- t/151-exit-worker.t | 28 ++++++++++------------------ 1 file changed, 10 insertions(+), 18 deletions(-) diff --git a/t/151-exit-worker.t b/t/151-exit-worker.t index f9e841b08a..0d860707a8 100644 --- a/t/151-exit-worker.t +++ b/t/151-exit-worker.t @@ -1,8 +1,10 @@ # vim:set ft= ts=4 sw=4 et fdm=marker: -use Test::Nginx::Socket 'no_plan'; use Test::Nginx::Socket::Lua; +repeat_each(1); + +plan tests => repeat_each() * (blocks() * 1 + 4); #log_level("warn"); no_long_string(); @@ -15,12 +17,6 @@ __DATA__ --- http_config exit_worker_by_lua_block { ngx.log(ngx.NOTICE, "log from exit_worker_by_lua_block") - -- grep_error_log chop - -- log from exit_worker_by_lua_block - -- --- grep_error_log_out eval - -- ["log from exit_worker_by_lua_block\n", ""] - -- Due to the use grep_error_log and grep_error_log_out is invalid, So use grep verification test(exit_worker_by_lua* run when worker exit) - -- grep "log from exit_worker_by_lua_block" t/servroot/logs/error.log } --- config location /t { @@ -30,20 +26,15 @@ __DATA__ GET /t --- response_body ok ---- no_error_log -[error] +--- stop_after_request +--- error_log +log from exit_worker_by_lua_block -=== TEST 1: simple exit_worker_by_lua_file +=== TEST 2: simple exit_worker_by_lua_file --- http_config exit_worker_by_lua_file html/exit_worker.lua; - # grep_error_log chop - # log from exit_worker_by_lua_file - # --- grep_error_log_out eval - # ["log from exit_worker_by_lua_file\n", ""] - # Due to the use grep_error_log and grep_error_log_out is invalid, So use grep verification test(exit_worker_by_lua* run when worker exit) - # grep "log from exit_worker_by_lua_file" t/servroot/logs/error.log --- config location /t { echo "ok"; @@ -55,6 +46,7 @@ ngx.log(ngx.NOTICE, "log from exit_worker_by_lua_file") GET /t --- response_body ok ---- no_error_log -[error] +--- stop_after_request +--- error_log +log from exit_worker_by_lua_file From 5ec51d0a130c0ad042d7aedec4bc16d1222dfd47 Mon Sep 17 00:00:00 2001 From: letian Date: Sat, 7 Jan 2017 11:27:09 +0800 Subject: [PATCH 07/22] upgrade markdown and wiki --- README.markdown | 16 ++++++++-------- doc/HttpLuaModule.wiki | 30 ++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+), 8 deletions(-) diff --git a/README.markdown b/README.markdown index bf16ecae3c..8c6158f199 100644 --- a/README.markdown +++ b/README.markdown @@ -1587,13 +1587,13 @@ exit_worker_by_lua_block **context:** *http* -**phase:** *exitting-worker* +**phase:** *exiting-worker* Runs the specified Lua code upon every Nginx worker process's exiting when the master process is enabled. -This hook is often used to release resources when init_worker_by* create resources, To prevent the worker exit abnormal +This hook is often used to release resources when init_worker_by* create resources, To prevent the worker exit abnormal. -For instance, +For example, ```nginx @@ -1604,16 +1604,16 @@ For instance, [Back to TOC](#directives) -exit_worker_by_lua_block ------------------------- +exit_worker_by_lua_file +----------------------- -**syntax:** *exit_worker_by_lua_file <lua-file-path>* +**syntax:** *exit_worker_by_lua_file { lua--file-path }* **context:** *http* -**phase:** *exitting-worker* +**phase:** *exiting-worker* -Similar to [exit_worker_by_lua_block](#exit_worker_by_lua_block), but accepts the file path to a Lua source file or Lua bytecode file. +Similar to [[#exit_worker_by_lua_block]], but accepts the file path to a Lua source file or Lua bytecode file. [Back to TOC](#directives) diff --git a/doc/HttpLuaModule.wiki b/doc/HttpLuaModule.wiki index 37d8a1367b..b033790bb2 100644 --- a/doc/HttpLuaModule.wiki +++ b/doc/HttpLuaModule.wiki @@ -1281,6 +1281,36 @@ This directive was first introduced in the v0.9.5 release. This hook no longer runs in the cache manager and cache loader processes since the v0.10.12 release. +== exit_worker_by_lua_block == + +'''syntax:''' ''exit_worker_by_lua_block { lua-script }'' + +'''context:''' ''http'' + +'''phase:''' ''exiting-worker'' + +Runs the specified Lua code upon every Nginx worker process's exiting when the master process is enabled. + +This hook is often used to release resources when init_worker_by* create resources, To prevent the worker exit abnormal. + +For example, + + + exit_worker_by_lua_block { + print("log from exit_worker_by_lua_block") + } + + +== exit_worker_by_lua_file == + +'''syntax:''' ''exit_worker_by_lua_file { lua--file-path }'' + +'''context:''' ''http'' + +'''phase:''' ''exiting-worker'' + +Similar to [[#exit_worker_by_lua_block]], but accepts the file path to a Lua source file or Lua bytecode file. + == set_by_lua == '''syntax:''' ''set_by_lua $res [$arg1 $arg2 ...]'' From 3d1a8f7e4e90bd9dae4dcfa7a5de4bd61266ecc9 Mon Sep 17 00:00:00 2001 From: letian Date: Sat, 7 Jan 2017 11:33:26 +0800 Subject: [PATCH 08/22] fix exit_worker_by_lua_file HttpLuaModule.wiki --- README.markdown | 2 +- doc/HttpLuaModule.wiki | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/README.markdown b/README.markdown index 8c6158f199..d318837c72 100644 --- a/README.markdown +++ b/README.markdown @@ -1613,7 +1613,7 @@ exit_worker_by_lua_file **phase:** *exiting-worker* -Similar to [[#exit_worker_by_lua_block]], but accepts the file path to a Lua source file or Lua bytecode file. +Similar to [exit_worker_by_lua_block](#exit_worker_by_lua_block), but accepts the file path to a Lua source file or Lua bytecode file. [Back to TOC](#directives) diff --git a/doc/HttpLuaModule.wiki b/doc/HttpLuaModule.wiki index b033790bb2..0c2bce8eed 100644 --- a/doc/HttpLuaModule.wiki +++ b/doc/HttpLuaModule.wiki @@ -1309,7 +1309,7 @@ For example, '''phase:''' ''exiting-worker'' -Similar to [[#exit_worker_by_lua_block]], but accepts the file path to a Lua source file or Lua bytecode file. +Similar to [[#exit_worker_by_lua_block|exit_worker_by_lua_block]], but accepts the file path to a Lua source file or Lua bytecode file. == set_by_lua == From fbb7a700fefc125877c547675760d91648ceb43d Mon Sep 17 00:00:00 2001 From: tan <312841925@qq.com> Date: Sat, 21 Jan 2017 23:00:29 +0800 Subject: [PATCH 09/22] add NGX_HTTP_LUA_CONTEXT_EXIT_WORKER and tests --- src/ngx_http_lua_common.h | 1 + src/ngx_http_lua_exitworkerby.c | 43 +++++++++++++- src/ngx_http_lua_util.h | 1 + t/089-phase.t | 21 +++++++ t/151-exit-worker.t | 93 +++++++++++++++++++++++++++++-- t/152-hup-master-on-exit-worker.t | 44 +++++++++++++++ 6 files changed, 196 insertions(+), 7 deletions(-) create mode 100644 t/152-hup-master-on-exit-worker.t diff --git a/src/ngx_http_lua_common.h b/src/ngx_http_lua_common.h index 6e51c69b54..57c031f276 100644 --- a/src/ngx_http_lua_common.h +++ b/src/ngx_http_lua_common.h @@ -138,6 +138,7 @@ typedef struct { #define NGX_HTTP_LUA_CONTEXT_SSL_CERT 0x0400 #define NGX_HTTP_LUA_CONTEXT_SSL_SESS_STORE 0x0800 #define NGX_HTTP_LUA_CONTEXT_SSL_SESS_FETCH 0x1000 +#define NGX_HTTP_LUA_CONTEXT_EXIT_WORKER 0x2000 #define NGX_HTTP_LUA_FFI_NO_REQ_CTX -100 diff --git a/src/ngx_http_lua_exitworkerby.c b/src/ngx_http_lua_exitworkerby.c index 71e08b486b..01cff24856 100644 --- a/src/ngx_http_lua_exitworkerby.c +++ b/src/ngx_http_lua_exitworkerby.c @@ -18,6 +18,10 @@ void ngx_http_lua_exit_worker(ngx_cycle_t *cycle) { ngx_http_lua_main_conf_t *lmcf; + ngx_connection_t *c = NULL; + ngx_http_request_t *r = NULL; + ngx_http_lua_ctx_t *ctx; + ngx_http_conf_ctx_t *conf_ctx; lmcf = ngx_http_cycle_get_module_main_conf(cycle, ngx_http_lua_module); if (lmcf == NULL @@ -27,8 +31,45 @@ ngx_http_lua_exit_worker(ngx_cycle_t *cycle) return; } + conf_ctx = ((ngx_http_conf_ctx_t *) cycle->conf_ctx[ngx_http_module.index]); + + c = ngx_http_lua_create_fake_connection(NULL); + if (c == NULL) { + goto failed; + } + + c->log = ngx_cycle->log; + + r = ngx_http_lua_create_fake_request(c); + if (r == NULL) { + goto failed; + } + + r->main_conf = conf_ctx->main_conf; + r->srv_conf = conf_ctx->srv_conf; + r->loc_conf = conf_ctx->loc_conf; + + ctx = ngx_http_lua_create_ctx(r); + if (ctx == NULL) { + goto failed; + } + + ctx->context = NGX_HTTP_LUA_CONTEXT_EXIT_WORKER; + ctx->cur_co_ctx = NULL; + + ngx_http_lua_set_req(lmcf->lua, r); + (void) lmcf->exit_worker_handler(cycle->log, lmcf, lmcf->lua); + ngx_destroy_pool(c->pool); + return; + +failed: + + if (c) { + ngx_http_lua_close_fake_connection(c); + } + return; } @@ -40,7 +81,7 @@ ngx_http_lua_exit_worker_by_inline(ngx_log_t *log, int status; status = luaL_loadbuffer(L, (char *) lmcf->exit_worker_src.data, - lmcf->exit_worker_src.len, "=exit_worker_by_lua") + lmcf->exit_worker_src.len, "=exit_worker_by_lua") || ngx_http_lua_do_call(log, L); return ngx_http_lua_report(log, L, status, "exit_worker_by_lua"); diff --git a/src/ngx_http_lua_util.h b/src/ngx_http_lua_util.h index 57c0814bfb..52d804bff0 100644 --- a/src/ngx_http_lua_util.h +++ b/src/ngx_http_lua_util.h @@ -53,6 +53,7 @@ : (c) == NGX_HTTP_LUA_CONTEXT_BODY_FILTER ? "body_filter_by_lua*" \ : (c) == NGX_HTTP_LUA_CONTEXT_TIMER ? "ngx.timer" \ : (c) == NGX_HTTP_LUA_CONTEXT_INIT_WORKER ? "init_worker_by_lua*" \ + : (c) == NGX_HTTP_LUA_CONTEXT_EXIT_WORKER ? "exit_worker_by_lua*" \ : (c) == NGX_HTTP_LUA_CONTEXT_BALANCER ? "balancer_by_lua*" \ : (c) == NGX_HTTP_LUA_CONTEXT_SSL_CERT ? "ssl_certificate_by_lua*" \ : (c) == NGX_HTTP_LUA_CONTEXT_SSL_SESS_STORE ? \ diff --git a/t/089-phase.t b/t/089-phase.t index 94b7619ec9..1323405fab 100644 --- a/t/089-phase.t +++ b/t/089-phase.t @@ -176,3 +176,24 @@ GET /lua init_worker --- no_error_log [error] + + + +=== TEST 11: get_phase in exit_worker_by_lua +--- http_config + exit_worker_by_lua_block { + local phase = ngx.get_phase() + ngx.log(ngx.NOTICE, phase) + } +--- config + location /lua { + content_by_lua_block { + ngx.say("ok") + } + } +--- request +GET /lua +--- response_body +ok +--- shutdown_error_log +exit_worker diff --git a/t/151-exit-worker.t b/t/151-exit-worker.t index 0d860707a8..bd16fa874d 100644 --- a/t/151-exit-worker.t +++ b/t/151-exit-worker.t @@ -2,12 +2,13 @@ use Test::Nginx::Socket::Lua; -repeat_each(1); +repeat_each(2); -plan tests => repeat_each() * (blocks() * 1 + 4); +plan tests => repeat_each() * (blocks() * 2); #log_level("warn"); no_long_string(); +our $HtmlDir = html_dir; run_tests(); @@ -26,8 +27,7 @@ __DATA__ GET /t --- response_body ok ---- stop_after_request ---- error_log +--- shutdown_error_log log from exit_worker_by_lua_block @@ -46,7 +46,88 @@ ngx.log(ngx.NOTICE, "log from exit_worker_by_lua_file") GET /t --- response_body ok ---- stop_after_request ---- error_log +--- shutdown_error_log log from exit_worker_by_lua_file + + +=== TEST 3: exit_worker_by_lua (require a global table) +--- http_config eval + qq{lua_package_path '$::HtmlDir/?.lua;;'; + exit_worker_by_lua_block { + foo = require("foo") + ngx.log(ngx.NOTICE, foo.bar) + }} +--- config + location /t { + content_by_lua_block { + foo = require("foo") + foo.bar = "hello, world" + ngx.say("ok") + } + } +--- user_files +>>> foo.lua +return {} +--- request +GET /t +--- response_body +ok +--- shutdown_error_log +hello, world + + + +=== TEST 4: exit_worker_by_lua single process ngx.timer not work +--- http_config + exit_worker_by_lua_block { + local function bar() + ngx.log(ngx.ERR, "run the timer!" + end + + local ok, err = ngx.timer.at(0, bar) + if not ok then + ngx.log(ngx.ERR, "failed to create timer: " .. err) + else + ngx.log(ngx.NOTICE, "success") + end + } +--- config + location /t { + echo "ok"; + } +--- request +GET /t +--- response_body +ok +--- no_error_log + + + +=== TEST 4: exit_worker_by_lua use shdict +--- http_config + lua_shared_dict dog 10m; + exit_worker_by_lua_block { + local dog = ngx.shared.dog + local val, err = dog:get("foo") + if not val then + ngx.log(ngx.ERR, "failed get shdict: " .. err) + else + ngx.log(ngx.NOTICE, "get val: " .. val) + end + } +--- config + location /t { + content_by_lua_block { + local dog = ngx.shared.dog + dog:set("foo", 100) + ngx.say("ok") + } + } +--- request +GET /t +--- response_body +ok +--- shutdown_error_log +get 11val: 1000 + diff --git a/t/152-hup-master-on-exit-worker.t b/t/152-hup-master-on-exit-worker.t new file mode 100644 index 0000000000..d5fe63fe85 --- /dev/null +++ b/t/152-hup-master-on-exit-worker.t @@ -0,0 +1,44 @@ +# vim:set ft= ts=4 sw=4 et fdm=marker: + +our $SkipReason; + +BEGIN { + if ($ENV{TEST_NGINX_CHECK_LEAK}) { + $SkipReason = "unavailable for the hup tests"; + + } else { + $ENV{TEST_NGINX_USE_HUP} = 1; + undef $ENV{TEST_NGINX_USE_STAP}; + } +} + +use Test::Nginx::Socket::Lua $SkipReason ? (skip_all => $SkipReason) : (); + +repeat_each(2); + +plan tests => repeat_each() * (blocks() * 2); + +no_long_string(); + +master_on(); + +run_tests(); + +__DATA__ + +=== TEST 1: exit_worker_by_lua_block reload +--- http_config + exit_worker_by_lua_block { + ngx.log(ngx.NOTICE, "log from exit_worker_by_lua_block") + } +--- config + location /t { + echo "ok"; + } +--- request +GET /t +--- response_body +ok +--- shutdown_error_log +log from exit_worker_by_lua_block + From f2d3ce33ca334a1210cce109cfe71666ebb232c5 Mon Sep 17 00:00:00 2001 From: letian Date: Thu, 9 Feb 2017 09:41:10 +0800 Subject: [PATCH 10/22] fix wiki --- README.markdown | 2 +- doc/HttpLuaModule.wiki | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/README.markdown b/README.markdown index d318837c72..e3c6ee2fb4 100644 --- a/README.markdown +++ b/README.markdown @@ -1591,7 +1591,7 @@ exit_worker_by_lua_block Runs the specified Lua code upon every Nginx worker process's exiting when the master process is enabled. -This hook is often used to release resources when init_worker_by* create resources, To prevent the worker exit abnormal. +This hook is often used to release resources when [init_worker_by_lua*](#init_worker_by_lua_block) create resources, To prevent the worker exit abnormal. For example, diff --git a/doc/HttpLuaModule.wiki b/doc/HttpLuaModule.wiki index 0c2bce8eed..e96137f1ef 100644 --- a/doc/HttpLuaModule.wiki +++ b/doc/HttpLuaModule.wiki @@ -1291,7 +1291,7 @@ This hook no longer runs in the cache manager and cache loader processes since t Runs the specified Lua code upon every Nginx worker process's exiting when the master process is enabled. -This hook is often used to release resources when init_worker_by* create resources, To prevent the worker exit abnormal. +This hook is often used to release resources when [[#init_worker_by_lua_block|init_worker_by_lua*]] create resources, To prevent the worker exit abnormal. For example, From 7f686f4401b657fd0635b5759fcfb1ae7728b1d7 Mon Sep 17 00:00:00 2001 From: letian Date: Fri, 10 Feb 2017 19:06:43 +0800 Subject: [PATCH 11/22] fix 151-exit-worker.t test --- t/151-exit-worker.t | 4 +-- t/152-hup-master-on-exit-worker.t | 44 ------------------------------- 2 files changed, 2 insertions(+), 46 deletions(-) delete mode 100644 t/152-hup-master-on-exit-worker.t diff --git a/t/151-exit-worker.t b/t/151-exit-worker.t index bd16fa874d..901924d978 100644 --- a/t/151-exit-worker.t +++ b/t/151-exit-worker.t @@ -104,7 +104,7 @@ ok -=== TEST 4: exit_worker_by_lua use shdict +=== TEST 5: exit_worker_by_lua use shdict --- http_config lua_shared_dict dog 10m; exit_worker_by_lua_block { @@ -129,5 +129,5 @@ GET /t --- response_body ok --- shutdown_error_log -get 11val: 1000 +get val: 100 diff --git a/t/152-hup-master-on-exit-worker.t b/t/152-hup-master-on-exit-worker.t deleted file mode 100644 index d5fe63fe85..0000000000 --- a/t/152-hup-master-on-exit-worker.t +++ /dev/null @@ -1,44 +0,0 @@ -# vim:set ft= ts=4 sw=4 et fdm=marker: - -our $SkipReason; - -BEGIN { - if ($ENV{TEST_NGINX_CHECK_LEAK}) { - $SkipReason = "unavailable for the hup tests"; - - } else { - $ENV{TEST_NGINX_USE_HUP} = 1; - undef $ENV{TEST_NGINX_USE_STAP}; - } -} - -use Test::Nginx::Socket::Lua $SkipReason ? (skip_all => $SkipReason) : (); - -repeat_each(2); - -plan tests => repeat_each() * (blocks() * 2); - -no_long_string(); - -master_on(); - -run_tests(); - -__DATA__ - -=== TEST 1: exit_worker_by_lua_block reload ---- http_config - exit_worker_by_lua_block { - ngx.log(ngx.NOTICE, "log from exit_worker_by_lua_block") - } ---- config - location /t { - echo "ok"; - } ---- request -GET /t ---- response_body -ok ---- shutdown_error_log -log from exit_worker_by_lua_block - From e98f64ecd196d5b6e64e97549b758d21106f7a21 Mon Sep 17 00:00:00 2001 From: letian Date: Sun, 12 Feb 2017 07:42:10 +0800 Subject: [PATCH 12/22] remove 151-exit-worker.t blank line --- t/151-exit-worker.t | 1 - 1 file changed, 1 deletion(-) diff --git a/t/151-exit-worker.t b/t/151-exit-worker.t index 901924d978..38eaf92327 100644 --- a/t/151-exit-worker.t +++ b/t/151-exit-worker.t @@ -130,4 +130,3 @@ GET /t ok --- shutdown_error_log get val: 100 - From b8bf381d531f1870424179b716d2fdb9fdc0c117 Mon Sep 17 00:00:00 2001 From: "jinhua.tan" Date: Thu, 2 Apr 2020 19:10:34 +0800 Subject: [PATCH 13/22] test: update travis.yaml to pass the test --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index bf433bef5e..dd4be41a31 100644 --- a/.travis.yml +++ b/.travis.yml @@ -83,7 +83,7 @@ install: - git clone https://github.com/openresty/rds-json-nginx-module.git ../rds-json-nginx-module - git clone https://github.com/openresty/srcache-nginx-module.git ../srcache-nginx-module - git clone https://github.com/openresty/redis2-nginx-module.git ../redis2-nginx-module - - git clone https://github.com/openresty/lua-resty-core.git ../lua-resty-core + - git clone -b exit_worker https://github.com/rainingmaster/lua-resty-core.git ../lua-resty-core - git clone https://github.com/openresty/lua-resty-lrucache.git ../lua-resty-lrucache - git clone https://github.com/openresty/lua-resty-mysql.git ../lua-resty-mysql - git clone https://github.com/openresty/lua-resty-string.git ../lua-resty-string From 5aa72f8b4af73a90b7599b04c40e41741eee8a38 Mon Sep 17 00:00:00 2001 From: "jinhua.tan" Date: Thu, 2 Apr 2020 21:07:49 +0800 Subject: [PATCH 14/22] test: add some test cases with HUP to reload, and fix the num of cases. --- src/ngx_http_lua_directive.c | 2 - t/089-phase.t | 6 +- t/{151-exit-worker.t => 162-exit-worker.t} | 2 +- t/163-exit-worker-hup.t | 86 ++++++++++++++++++++++ 4 files changed, 90 insertions(+), 6 deletions(-) rename t/{151-exit-worker.t => 162-exit-worker.t} (98%) create mode 100644 t/163-exit-worker-hup.t diff --git a/src/ngx_http_lua_directive.c b/src/ngx_http_lua_directive.c index 949cc15cbb..1ec641e079 100644 --- a/src/ngx_http_lua_directive.c +++ b/src/ngx_http_lua_directive.c @@ -1228,8 +1228,6 @@ ngx_http_lua_exit_worker_by_lua(ngx_conf_t *cf, ngx_command_t *cmd, ngx_str_t *value; ngx_http_lua_main_conf_t *lmcf = conf; - dd("enter"); - /* must specify a content handler */ if (cmd->post == NULL) { return NGX_CONF_ERROR; diff --git a/t/089-phase.t b/t/089-phase.t index 1323405fab..38b18b49d5 100644 --- a/t/089-phase.t +++ b/t/089-phase.t @@ -8,7 +8,7 @@ log_level('warn'); repeat_each(2); -plan tests => repeat_each() * (blocks() * 2 + 1); +plan tests => repeat_each() * (blocks() * 2) + 3; #no_diff(); #no_long_string(); @@ -183,7 +183,7 @@ init_worker --- http_config exit_worker_by_lua_block { local phase = ngx.get_phase() - ngx.log(ngx.NOTICE, phase) + ngx.log(ngx.ERR, phase) } --- config location /lua { @@ -196,4 +196,4 @@ GET /lua --- response_body ok --- shutdown_error_log -exit_worker +exit_worker_by_lua:3: exit_worker diff --git a/t/151-exit-worker.t b/t/162-exit-worker.t similarity index 98% rename from t/151-exit-worker.t rename to t/162-exit-worker.t index 38eaf92327..19990141f2 100644 --- a/t/151-exit-worker.t +++ b/t/162-exit-worker.t @@ -4,7 +4,7 @@ use Test::Nginx::Socket::Lua; repeat_each(2); -plan tests => repeat_each() * (blocks() * 2); +plan tests => repeat_each() * (blocks() * 2 + 2); #log_level("warn"); no_long_string(); diff --git a/t/163-exit-worker-hup.t b/t/163-exit-worker-hup.t new file mode 100644 index 0000000000..3f516aba98 --- /dev/null +++ b/t/163-exit-worker-hup.t @@ -0,0 +1,86 @@ +# vim:set ft= ts=4 sw=4 et fdm=marker: + +our $SkipReason; + +BEGIN { + if ($ENV{TEST_NGINX_CHECK_LEAK}) { + $SkipReason = "unavailable for the hup tests"; + + } else { + $ENV{TEST_NGINX_USE_HUP} = 1; + undef $ENV{TEST_NGINX_USE_STAP}; + } +} + +use Test::Nginx::Socket::Lua $SkipReason ? (skip_all => $SkipReason) : (); + +repeat_each(2); + +plan tests => repeat_each() * (blocks() * 2 + 2); + +no_long_string(); + +worker_connections(1024); +run_tests(); + +__DATA__ + +=== TEST 1: simple exit_worker_by_lua_block with hup +--- http_config + exit_worker_by_lua_block { + ngx.log(ngx.NOTICE, "log from exit_worker_by_lua_block") + } +--- config + location /t { + content_by_lua_block { + ngx.say("ok") + } + } +--- request +GET /t +--- response_body +ok +--- shutdown_error_log +log from exit_worker_by_lua_block + + + +=== TEST 2: exit after worker_shutdown_timeout +--- main_config + worker_shutdown_timeout 1; +--- http_config + exit_worker_by_lua_block { + ngx.log(ngx.NOTICE, "log from exit_worker_by_lua_block") + } + + server { + listen 12345; + + location = /t { + echo 'hello world'; + } + } +--- config + location /t { + content_by_lua_block { + ngx.timer.at(0, function () + local sock = ngx.socket.tcp() + sock:connect("127.0.0.1", 12345) + local reader = sock:receiveuntil("unknow") + ngx.log(ngx.NOTICE, "reading to block the exiting") + reader() + end) + + ngx.sleep(0) + + ngx.say("ok") + } + } +--- request +GET /t +--- response_body +ok +--- error_log +reading to block the exiting +--- shutdown_error_log +log from exit_worker_by_lua_block From f265995d67cfdc94bdbb660ae17686249572aeff Mon Sep 17 00:00:00 2001 From: rainingmaster <312841925@qq.com> Date: Thu, 9 Apr 2020 21:49:49 +0800 Subject: [PATCH 15/22] doc: fix some grammar problem. --- doc/HttpLuaModule.wiki | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/doc/HttpLuaModule.wiki b/doc/HttpLuaModule.wiki index e96137f1ef..6bbcc46763 100644 --- a/doc/HttpLuaModule.wiki +++ b/doc/HttpLuaModule.wiki @@ -1289,9 +1289,9 @@ This hook no longer runs in the cache manager and cache loader processes since t '''phase:''' ''exiting-worker'' -Runs the specified Lua code upon every Nginx worker process's exiting when the master process is enabled. +Runs the specified Lua code upon every Nginx worker process's exiting when the master process is enabled. When the master process is disabled, this hook will before run Nginx process exiting. -This hook is often used to release resources when [[#init_worker_by_lua_block|init_worker_by_lua*]] create resources, To prevent the worker exit abnormal. +This hook is often used to release resources when [[#init_worker_by_lua|init_worker_by_lua*]] create resources, to prevent the worker from exiting abnormally. For example, From 18c3c7d571df84aadc4f479a26b48630af54b9d0 Mon Sep 17 00:00:00 2001 From: rainingmaster <312841925@qq.com> Date: Fri, 10 Apr 2020 22:00:04 +0800 Subject: [PATCH 16/22] fix: fix typo --- README.markdown | 8 ++-- doc/HttpLuaModule.wiki | 8 ++-- src/ngx_http_lua_exitworkerby.c | 16 ++++++-- t/089-phase.t | 10 +++-- t/162-exit-worker.t | 69 ++++++++++++++++++++++++++++++--- 5 files changed, 93 insertions(+), 18 deletions(-) diff --git a/README.markdown b/README.markdown index e3c6ee2fb4..ba51b71f75 100644 --- a/README.markdown +++ b/README.markdown @@ -1589,9 +1589,9 @@ exit_worker_by_lua_block **phase:** *exiting-worker* -Runs the specified Lua code upon every Nginx worker process's exiting when the master process is enabled. +Runs the specified Lua code upon every Nginx worker process's exit when the master process is enabled. When the master process is disabled, this hook will before run Nginx process exiting. -This hook is often used to release resources when [init_worker_by_lua*](#init_worker_by_lua_block) create resources, To prevent the worker exit abnormal. +This hook is ogten used to release resources allocated by each worker (e.g. resources allocated by [init_worker_by_lua*](#init_worker_by_lua)), or to prevent workers from exiting abnormally. For example, @@ -1607,7 +1607,7 @@ For example, exit_worker_by_lua_file ----------------------- -**syntax:** *exit_worker_by_lua_file { lua--file-path }* +**syntax:** *exit_worker_by_lua_file { lua-file-path }* **context:** *http* @@ -7877,6 +7877,8 @@ Retrieves the current running phase name. Possible return values are for the context of [log_by_lua*](#log_by_lua). * `timer` 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). This API was first introduced in the `v0.5.10` release. diff --git a/doc/HttpLuaModule.wiki b/doc/HttpLuaModule.wiki index 6bbcc46763..ac48df8029 100644 --- a/doc/HttpLuaModule.wiki +++ b/doc/HttpLuaModule.wiki @@ -1289,9 +1289,9 @@ This hook no longer runs in the cache manager and cache loader processes since t '''phase:''' ''exiting-worker'' -Runs the specified Lua code upon every Nginx worker process's exiting when the master process is enabled. When the master process is disabled, this hook will before run Nginx process exiting. +Runs the specified Lua code upon every Nginx worker process's exit when the master process is enabled. When the master process is disabled, this hook will before run Nginx process exiting. -This hook is often used to release resources when [[#init_worker_by_lua|init_worker_by_lua*]] create resources, to prevent the worker from exiting abnormally. +This hook is ogten used to release resources allocated by each worker (e.g. resources allocated by [[#init_worker_by_lua|init_worker_by_lua*]]), or to prevent workers from exiting abnormally. For example, @@ -1303,7 +1303,7 @@ For example, == exit_worker_by_lua_file == -'''syntax:''' ''exit_worker_by_lua_file { lua--file-path }'' +'''syntax:''' ''exit_worker_by_lua_file { lua-file-path }'' '''context:''' ''http'' @@ -6705,6 +6705,8 @@ Retrieves the current running phase name. Possible return values are : for the context of [[#log_by_lua|log_by_lua*]]. * timer : for the context of user callback functions for [[#ngx.timer.at|ngx.timer.*]]. +* exit_worker +: for the context of [[#exit_worker_by_lua|exit_worker_by_lua*]]. This API was first introduced in the v0.5.10 release. diff --git a/src/ngx_http_lua_exitworkerby.c b/src/ngx_http_lua_exitworkerby.c index 01cff24856..ad06c4ec66 100644 --- a/src/ngx_http_lua_exitworkerby.c +++ b/src/ngx_http_lua_exitworkerby.c @@ -26,7 +26,15 @@ ngx_http_lua_exit_worker(ngx_cycle_t *cycle) lmcf = ngx_http_cycle_get_module_main_conf(cycle, ngx_http_lua_module); if (lmcf == NULL || lmcf->exit_worker_handler == NULL - || lmcf->lua == NULL) + || lmcf->lua == NULL +#if !(NGX_WIN32) + || (ngx_process == NGX_PROCESS_HELPER +# ifdef HAVE_PRIVILEGED_PROCESS_PATCH + && !ngx_is_privileged_agent +# endif + ) +#endif /* NGX_WIN32 */ + ) { return; } @@ -89,13 +97,13 @@ ngx_http_lua_exit_worker_by_inline(ngx_log_t *log, ngx_int_t -ngx_http_lua_exit_worker_by_file(ngx_log_t *log, - ngx_http_lua_main_conf_t *lmcf, lua_State *L) +ngx_http_lua_exit_worker_by_file(ngx_log_t *log, ngx_http_lua_main_conf_t *lmcf, + lua_State *L) { int status; status = luaL_loadfile(L, (char *) lmcf->exit_worker_src.data) || ngx_http_lua_do_call(log, L); - return ngx_http_lua_report(log, L, status, "exit_worker_by_file"); + return ngx_http_lua_report(log, L, status, "exit_worker_by_lua_file"); } diff --git a/t/089-phase.t b/t/089-phase.t index 38b18b49d5..538dff8723 100644 --- a/t/089-phase.t +++ b/t/089-phase.t @@ -8,7 +8,7 @@ log_level('warn'); repeat_each(2); -plan tests => repeat_each() * (blocks() * 2) + 3; +plan tests => repeat_each() * (blocks() * 2 + 2); #no_diff(); #no_long_string(); @@ -184,6 +184,7 @@ init_worker exit_worker_by_lua_block { local phase = ngx.get_phase() ngx.log(ngx.ERR, phase) + ngx.log(ngx.ERR, "exiting now") } --- config location /lua { @@ -195,5 +196,8 @@ init_worker GET /lua --- response_body ok ---- shutdown_error_log -exit_worker_by_lua:3: exit_worker +--- shutdown_error_log eval +[ +qr/exit_worker_by_lua:\d+: exit_worker/, +qr/exiting now$/, +] diff --git a/t/162-exit-worker.t b/t/162-exit-worker.t index 19990141f2..491f34a244 100644 --- a/t/162-exit-worker.t +++ b/t/162-exit-worker.t @@ -2,9 +2,10 @@ use Test::Nginx::Socket::Lua; +master_on(); repeat_each(2); -plan tests => repeat_each() * (blocks() * 2 + 2); +plan tests => repeat_each() * (blocks() * 2 + 7); #log_level("warn"); no_long_string(); @@ -87,7 +88,7 @@ hello, world local ok, err = ngx.timer.at(0, bar) if not ok then - ngx.log(ngx.ERR, "failed to create timer: " .. err) + ngx.log(ngx.ERR, "failed to create timer: ", err) else ngx.log(ngx.NOTICE, "success") end @@ -101,19 +102,20 @@ GET /t --- response_body ok --- no_error_log +[error] === TEST 5: exit_worker_by_lua use shdict --- http_config - lua_shared_dict dog 10m; + lua_shared_dict dog 1m; exit_worker_by_lua_block { local dog = ngx.shared.dog local val, err = dog:get("foo") if not val then - ngx.log(ngx.ERR, "failed get shdict: " .. err) + ngx.log(ngx.ERR, "failed get shdict: ", err) else - ngx.log(ngx.NOTICE, "get val: " .. val) + ngx.log(ngx.NOTICE, "get val: ", val) end } --- config @@ -130,3 +132,60 @@ GET /t ok --- shutdown_error_log get val: 100 + + + +=== TEST 6: skip in cache processes (with exit worker and privileged agent) +--- http_config + lua_package_path "../lua-resty-core/lib/?.lua;../lua-resty-lrucache/lib/?.lua;;"; + + proxy_cache_path /tmp/cache levels=1:2 keys_zone=cache:1m; + + init_by_lua_block { + assert(require "ngx.process".enable_privileged_agent()) + } + + exit_worker_by_lua_block { + ngx.log(ngx.NOTICE, "hello from exit worker by lua") + } +--- config + location = /t { + return 200; + } +--- request + GET /t +--- no_error_log +[error] +--- shutdown_error_log eval +[ +qr/cache loader process \d+ exited/, +qr/cache manager process \d+ exited/, +qr/hello from exit worker by lua$/, +qr/hello from exit worker by lua$/, +qr/privileged agent process \d+ exited/, +] + + + +=== TEST 7: skipin cache processes (with init worker but without privileged agent) +--- http_config + lua_package_path "../lua-resty-core/lib/?.lua;../lua-resty-lrucache/lib/?.lua;;"; + + proxy_cache_path /tmp/cache levels=1:2 keys_zone=cache:1m; + + exit_worker_by_lua_block { + ngx.log(ngx.WARN, "hello from exit worker by lua") + } +--- config + location = /t { + return 200; + } +--- request + GET /t +--- no_error_log +[error] +start privileged agent process +--- shutdown_error_log eval +[ +qr/hello from exit worker by lua$/, +] From 696b062f73646b58ee490c80b68959cf508dd8f0 Mon Sep 17 00:00:00 2001 From: rainingmaster <312841925@qq.com> Date: Sun, 12 Apr 2020 10:42:30 +0800 Subject: [PATCH 17/22] doc: add vim modeline in exitworkerbylua.c --- src/ngx_http_lua_exitworkerby.c | 3 +++ src/ngx_http_lua_exitworkerby.h | 6 +++--- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/src/ngx_http_lua_exitworkerby.c b/src/ngx_http_lua_exitworkerby.c index ad06c4ec66..140866c90c 100644 --- a/src/ngx_http_lua_exitworkerby.c +++ b/src/ngx_http_lua_exitworkerby.c @@ -107,3 +107,6 @@ ngx_http_lua_exit_worker_by_file(ngx_log_t *log, ngx_http_lua_main_conf_t *lmcf, return ngx_http_lua_report(log, L, status, "exit_worker_by_lua_file"); } + + +/* vi:set ft=c ts=4 sw=4 et fdm=marker: */ diff --git a/src/ngx_http_lua_exitworkerby.h b/src/ngx_http_lua_exitworkerby.h index 38d28fe1a0..3a4274c042 100644 --- a/src/ngx_http_lua_exitworkerby.h +++ b/src/ngx_http_lua_exitworkerby.h @@ -4,8 +4,8 @@ */ -#ifndef _NGX_HTTP_LUA_EXITWORKERBY_H_INCLUDE_ -#define _NGX_HTTP_LUA_EXITWORKERBY_H_INCLUDE_ +#ifndef _NGX_HTTP_LUA_EXITWORKERBY_H_INCLUDED_ +#define _NGX_HTTP_LUA_EXITWORKERBY_H_INCLUDED_ #include "ngx_http_lua_common.h" @@ -20,6 +20,6 @@ ngx_int_t ngx_http_lua_exit_worker_by_file(ngx_log_t *log, void ngx_http_lua_exit_worker(ngx_cycle_t *cycle); -#endif /* _NGX_HTTP_LUA_EXITWORKERBY_H_INCLUDE_ */ +#endif /* _NGX_HTTP_LUA_EXITWORKERBY_H_INCLUDED_ */ /* vi:set ft=c ts=4 sw=4 et fdm=marker: */ From 6ec87f1c886e66b8ef0af8ff8fc12eb22dd34373 Mon Sep 17 00:00:00 2001 From: rainingmaster <312841925@qq.com> Date: Sun, 12 Apr 2020 11:18:19 +0800 Subject: [PATCH 18/22] test(162-exit-worker.t): improve the test cases --- t/162-exit-worker.t | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/t/162-exit-worker.t b/t/162-exit-worker.t index 491f34a244..cb0edf7076 100644 --- a/t/162-exit-worker.t +++ b/t/162-exit-worker.t @@ -5,7 +5,7 @@ use Test::Nginx::Socket::Lua; master_on(); repeat_each(2); -plan tests => repeat_each() * (blocks() * 2 + 7); +plan tests => repeat_each() * (blocks() * 2 + 8); #log_level("warn"); no_long_string(); @@ -146,7 +146,8 @@ get val: 100 } exit_worker_by_lua_block { - ngx.log(ngx.NOTICE, "hello from exit worker by lua") + local process = require "ngx.process" + ngx.log(ngx.INFO, "hello from exit worker by lua, process type: ", process.type()) } --- config location = /t { @@ -160,8 +161,8 @@ get val: 100 [ qr/cache loader process \d+ exited/, qr/cache manager process \d+ exited/, -qr/hello from exit worker by lua$/, -qr/hello from exit worker by lua$/, +qr/hello from exit worker by lua, process type: worker/, +qr/hello from exit worker by lua, process type: privileged agent/, qr/privileged agent process \d+ exited/, ] @@ -174,7 +175,8 @@ qr/privileged agent process \d+ exited/, proxy_cache_path /tmp/cache levels=1:2 keys_zone=cache:1m; exit_worker_by_lua_block { - ngx.log(ngx.WARN, "hello from exit worker by lua") + local process = require "ngx.process" + ngx.log(ngx.INFO, "hello from exit worker by lua, process type: ", process.type()) } --- config location = /t { @@ -187,5 +189,7 @@ qr/privileged agent process \d+ exited/, start privileged agent process --- shutdown_error_log eval [ -qr/hello from exit worker by lua$/, +qr/cache loader process \d+ exited/, +qr/cache manager process \d+ exited/, +qr/hello from exit worker by lua, process type: worker/, ] From 98fb2bb16c97b0dd40571714c6cc8b1d5e71fd93 Mon Sep 17 00:00:00 2001 From: rainingmaster <312841925@qq.com> Date: Tue, 14 Apr 2020 18:17:01 +0800 Subject: [PATCH 19/22] doc: fix some typo. --- README.markdown | 4 ++-- doc/HttpLuaModule.wiki | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/README.markdown b/README.markdown index ba51b71f75..8201e3c1e0 100644 --- a/README.markdown +++ b/README.markdown @@ -1589,9 +1589,9 @@ exit_worker_by_lua_block **phase:** *exiting-worker* -Runs the specified Lua code upon every Nginx worker process's exit when the master process is enabled. When the master process is disabled, this hook will before run Nginx process exiting. +Runs the specified Lua code upon every Nginx worker process's exit when the master process is enabled. When the master process is disabled, this hook will run before the Nginx process exits. -This hook is ogten used to release resources allocated by each worker (e.g. resources allocated by [init_worker_by_lua*](#init_worker_by_lua)), or to prevent workers from exiting abnormally. +This hook is often used to release resources allocated by each worker (e.g. resources allocated by [init_worker_by_lua*](#init_worker_by_lua)), or to prevent workers from exiting abnormally. For example, diff --git a/doc/HttpLuaModule.wiki b/doc/HttpLuaModule.wiki index ac48df8029..4ce3494904 100644 --- a/doc/HttpLuaModule.wiki +++ b/doc/HttpLuaModule.wiki @@ -1289,9 +1289,9 @@ This hook no longer runs in the cache manager and cache loader processes since t '''phase:''' ''exiting-worker'' -Runs the specified Lua code upon every Nginx worker process's exit when the master process is enabled. When the master process is disabled, this hook will before run Nginx process exiting. +Runs the specified Lua code upon every Nginx worker process's exit when the master process is enabled. When the master process is disabled, this hook will run before the Nginx process exits. -This hook is ogten used to release resources allocated by each worker (e.g. resources allocated by [[#init_worker_by_lua|init_worker_by_lua*]]), or to prevent workers from exiting abnormally. +This hook is often used to release resources allocated by each worker (e.g. resources allocated by [[#init_worker_by_lua|init_worker_by_lua*]]), or to prevent workers from exiting abnormally. For example, From 6a7f72b0997558d254e4a6c685e99fad6b18635c Mon Sep 17 00:00:00 2001 From: rainingmaster <312841925@qq.com> Date: Sun, 7 Jun 2020 17:03:03 +0800 Subject: [PATCH 20/22] keep only one blank line before vi:set --- src/ngx_http_lua_exitworkerby.c | 1 - 1 file changed, 1 deletion(-) diff --git a/src/ngx_http_lua_exitworkerby.c b/src/ngx_http_lua_exitworkerby.c index 140866c90c..cd59dc4bed 100644 --- a/src/ngx_http_lua_exitworkerby.c +++ b/src/ngx_http_lua_exitworkerby.c @@ -108,5 +108,4 @@ ngx_http_lua_exit_worker_by_file(ngx_log_t *log, ngx_http_lua_main_conf_t *lmcf, return ngx_http_lua_report(log, L, status, "exit_worker_by_lua_file"); } - /* vi:set ft=c ts=4 sw=4 et fdm=marker: */ From cd8450237ae0785651f2e6a842d548157d561a44 Mon Sep 17 00:00:00 2001 From: rainingmaster <312841925@qq.com> Date: Sun, 28 Jun 2020 16:57:22 +0800 Subject: [PATCH 21/22] chore: update doc to reflect APIs is available in exit_worker_by_lua --- README.markdown | 70 +++++++++++++++++++++--------------------- doc/HttpLuaModule.wiki | 70 +++++++++++++++++++++--------------------- 2 files changed, 70 insertions(+), 70 deletions(-) diff --git a/README.markdown b/README.markdown index 8201e3c1e0..e37bda90fc 100644 --- a/README.markdown +++ b/README.markdown @@ -3616,7 +3616,7 @@ HTTP status constants Nginx log level constants ------------------------- -**context:** *init_by_lua*, init_worker_by_lua*, set_by_lua*, rewrite_by_lua*, access_by_lua*, content_by_lua*, header_filter_by_lua*, body_filter_by_lua*, log_by_lua*, ngx.timer.*, balancer_by_lua*, ssl_certificate_by_lua*, ssl_session_fetch_by_lua*, ssl_session_store_by_lua** +**context:** *init_by_lua*, init_worker_by_lua*, set_by_lua*, rewrite_by_lua*, access_by_lua*, content_by_lua*, header_filter_by_lua*, body_filter_by_lua*, log_by_lua*, ngx.timer.*, balancer_by_lua*, ssl_certificate_by_lua*, ssl_session_fetch_by_lua*, ssl_session_store_by_lua*, exit_worker_by_lua** ```lua @@ -3640,7 +3640,7 @@ print **syntax:** *print(...)* -**context:** *init_by_lua*, init_worker_by_lua*, set_by_lua*, rewrite_by_lua*, access_by_lua*, content_by_lua*, header_filter_by_lua*, body_filter_by_lua*, log_by_lua*, ngx.timer.*, balancer_by_lua*, ssl_certificate_by_lua*, ssl_session_fetch_by_lua*, ssl_session_store_by_lua** +**context:** *init_by_lua*, init_worker_by_lua*, set_by_lua*, rewrite_by_lua*, access_by_lua*, content_by_lua*, header_filter_by_lua*, body_filter_by_lua*, log_by_lua*, ngx.timer.*, balancer_by_lua*, ssl_certificate_by_lua*, ssl_session_fetch_by_lua*, ssl_session_store_by_lua*, exit_worker_by_lua** Writes argument values into the Nginx `error.log` file with the `ngx.NOTICE` log level. @@ -3660,7 +3660,7 @@ There is a hard coded `2048` byte limitation on error message lengths in the Ngi ngx.ctx ------- -**context:** *init_worker_by_lua*, set_by_lua*, rewrite_by_lua*, access_by_lua*, content_by_lua*, header_filter_by_lua*, body_filter_by_lua*, log_by_lua*, ngx.timer.*, balancer_by_lua** +**context:** *init_worker_by_lua*, set_by_lua*, rewrite_by_lua*, access_by_lua*, content_by_lua*, header_filter_by_lua*, body_filter_by_lua*, log_by_lua*, ngx.timer.*, balancer_by_lua*, exit_worker_by_lua** This table can be used to store per-request Lua context data and has a life time identical to the current request (as with the Nginx variables). @@ -5487,7 +5487,7 @@ ngx.log **syntax:** *ngx.log(log_level, ...)* -**context:** *init_by_lua*, init_worker_by_lua*, set_by_lua*, rewrite_by_lua*, access_by_lua*, content_by_lua*, header_filter_by_lua*, body_filter_by_lua*, log_by_lua*, ngx.timer.*, balancer_by_lua*, ssl_certificate_by_lua*, ssl_session_fetch_by_lua*, ssl_session_store_by_lua** +**context:** *init_by_lua*, init_worker_by_lua*, set_by_lua*, rewrite_by_lua*, access_by_lua*, content_by_lua*, header_filter_by_lua*, body_filter_by_lua*, log_by_lua*, ngx.timer.*, balancer_by_lua*, ssl_certificate_by_lua*, ssl_session_fetch_by_lua*, ssl_session_store_by_lua*, exit_worker_by_lua** Log arguments concatenated to error.log with the given logging level. @@ -5634,7 +5634,7 @@ ngx.escape_uri **syntax:** *newstr = ngx.escape_uri(str, type?)* -**context:** *init_by_lua*, init_worker_by_lua*, set_by_lua*, rewrite_by_lua*, access_by_lua*, content_by_lua*, header_filter_by_lua*, body_filter_by_lua*, log_by_lua*, ngx.timer.*, balancer_by_lua*, ssl_certificate_by_lua*, ssl_session_fetch_by_lua*, ssl_session_store_by_lua** +**context:** *init_by_lua*, init_worker_by_lua*, set_by_lua*, rewrite_by_lua*, access_by_lua*, content_by_lua*, header_filter_by_lua*, body_filter_by_lua*, log_by_lua*, ngx.timer.*, balancer_by_lua*, ssl_certificate_by_lua*, ssl_session_fetch_by_lua*, ssl_session_store_by_lua*, exit_worker_by_lua** Since `v0.10.16`, this function accepts an optional `type` argument. It accepts the following values (defaults to `2`): @@ -5653,7 +5653,7 @@ ngx.unescape_uri **syntax:** *newstr = ngx.unescape_uri(str)* -**context:** *init_by_lua*, init_worker_by_lua*, set_by_lua*, rewrite_by_lua*, access_by_lua*, content_by_lua*, header_filter_by_lua*, body_filter_by_lua*, log_by_lua*, ngx.timer.*, balancer_by_lua*, ssl_certificate_by_lua** +**context:** *init_by_lua*, init_worker_by_lua*, set_by_lua*, rewrite_by_lua*, access_by_lua*, content_by_lua*, header_filter_by_lua*, body_filter_by_lua*, log_by_lua*, ngx.timer.*, balancer_by_lua*, ssl_certificate_by_lua*, exit_worker_by_lua** Unescape `str` as an escaped URI component. @@ -5917,7 +5917,7 @@ ngx.today **syntax:** *str = ngx.today()* -**context:** *init_worker_by_lua*, set_by_lua*, rewrite_by_lua*, access_by_lua*, content_by_lua*, header_filter_by_lua*, body_filter_by_lua*, log_by_lua*, ngx.timer.*, balancer_by_lua*, ssl_certificate_by_lua*, ssl_session_fetch_by_lua*, ssl_session_store_by_lua** +**context:** *init_worker_by_lua*, set_by_lua*, rewrite_by_lua*, access_by_lua*, content_by_lua*, header_filter_by_lua*, body_filter_by_lua*, log_by_lua*, ngx.timer.*, balancer_by_lua*, ssl_certificate_by_lua*, ssl_session_fetch_by_lua*, ssl_session_store_by_lua*, exit_worker_by_lua** Returns current date (in the format `yyyy-mm-dd`) from the Nginx cached time (no syscall involved unlike Lua's date library). @@ -5930,7 +5930,7 @@ ngx.time **syntax:** *secs = ngx.time()* -**context:** *init_worker_by_lua*, set_by_lua*, rewrite_by_lua*, access_by_lua*, content_by_lua*, header_filter_by_lua*, body_filter_by_lua*, log_by_lua*, ngx.timer.*, balancer_by_lua*, ssl_certificate_by_lua*, ssl_session_fetch_by_lua*, ssl_session_store_by_lua** +**context:** *init_worker_by_lua*, set_by_lua*, rewrite_by_lua*, access_by_lua*, content_by_lua*, header_filter_by_lua*, body_filter_by_lua*, log_by_lua*, ngx.timer.*, balancer_by_lua*, ssl_certificate_by_lua*, ssl_session_fetch_by_lua*, ssl_session_store_by_lua*, exit_worker_by_lua** Returns the elapsed seconds from the epoch for the current time stamp from the Nginx cached time (no syscall involved unlike Lua's date library). @@ -5943,7 +5943,7 @@ ngx.now **syntax:** *secs = ngx.now()* -**context:** *init_worker_by_lua*, set_by_lua*, rewrite_by_lua*, access_by_lua*, content_by_lua*, header_filter_by_lua*, body_filter_by_lua*, log_by_lua*, ngx.timer.*, balancer_by_lua*, ssl_certificate_by_lua*, ssl_session_fetch_by_lua*, ssl_session_store_by_lua** +**context:** *init_worker_by_lua*, set_by_lua*, rewrite_by_lua*, access_by_lua*, content_by_lua*, header_filter_by_lua*, body_filter_by_lua*, log_by_lua*, ngx.timer.*, balancer_by_lua*, ssl_certificate_by_lua*, ssl_session_fetch_by_lua*, ssl_session_store_by_lua*, exit_worker_by_lua** Returns a floating-point number for the elapsed time in seconds (including milliseconds as the decimal part) from the epoch for the current time stamp from the Nginx cached time (no syscall involved unlike Lua's date library). @@ -5958,7 +5958,7 @@ ngx.update_time **syntax:** *ngx.update_time()* -**context:** *init_worker_by_lua*, set_by_lua*, rewrite_by_lua*, access_by_lua*, content_by_lua*, header_filter_by_lua*, body_filter_by_lua*, log_by_lua*, ngx.timer.*, balancer_by_lua*, ssl_certificate_by_lua*, ssl_session_fetch_by_lua*, ssl_session_store_by_lua** +**context:** *init_worker_by_lua*, set_by_lua*, rewrite_by_lua*, access_by_lua*, content_by_lua*, header_filter_by_lua*, body_filter_by_lua*, log_by_lua*, ngx.timer.*, balancer_by_lua*, ssl_certificate_by_lua*, ssl_session_fetch_by_lua*, ssl_session_store_by_lua*, exit_worker_by_lua** Forcibly updates the Nginx current time cache. This call involves a syscall and thus has some overhead, so do not abuse it. @@ -5971,7 +5971,7 @@ ngx.localtime **syntax:** *str = ngx.localtime()* -**context:** *init_worker_by_lua*, set_by_lua*, rewrite_by_lua*, access_by_lua*, content_by_lua*, header_filter_by_lua*, body_filter_by_lua*, log_by_lua*, ngx.timer.*, balancer_by_lua*, ssl_certificate_by_lua*, ssl_session_fetch_by_lua*, ssl_session_store_by_lua** +**context:** *init_worker_by_lua*, set_by_lua*, rewrite_by_lua*, access_by_lua*, content_by_lua*, header_filter_by_lua*, body_filter_by_lua*, log_by_lua*, ngx.timer.*, balancer_by_lua*, ssl_certificate_by_lua*, ssl_session_fetch_by_lua*, ssl_session_store_by_lua*, exit_worker_by_lua** Returns the current time stamp (in the format `yyyy-mm-dd hh:mm:ss`) of the Nginx cached time (no syscall involved unlike Lua's [os.date](https://www.lua.org/manual/5.1/manual.html#pdf-os.date) function). @@ -5984,7 +5984,7 @@ ngx.utctime **syntax:** *str = ngx.utctime()* -**context:** *init_worker_by_lua*, set_by_lua*, rewrite_by_lua*, access_by_lua*, content_by_lua*, header_filter_by_lua*, body_filter_by_lua*, log_by_lua*, ngx.timer.*, balancer_by_lua*, ssl_certificate_by_lua*, ssl_session_fetch_by_lua*, ssl_session_store_by_lua** +**context:** *init_worker_by_lua*, set_by_lua*, rewrite_by_lua*, access_by_lua*, content_by_lua*, header_filter_by_lua*, body_filter_by_lua*, log_by_lua*, ngx.timer.*, balancer_by_lua*, ssl_certificate_by_lua*, ssl_session_fetch_by_lua*, ssl_session_store_by_lua*, exit_worker_by_lua** Returns the current time stamp (in the format `yyyy-mm-dd hh:mm:ss`) of the Nginx cached time (no syscall involved unlike Lua's [os.date](https://www.lua.org/manual/5.1/manual.html#pdf-os.date) function). @@ -5997,7 +5997,7 @@ ngx.cookie_time **syntax:** *str = ngx.cookie_time(sec)* -**context:** *init_worker_by_lua*, set_by_lua*, rewrite_by_lua*, access_by_lua*, content_by_lua*, header_filter_by_lua*, body_filter_by_lua*, log_by_lua*, ngx.timer.*, balancer_by_lua*, ssl_certificate_by_lua*, ssl_session_fetch_by_lua*, ssl_session_store_by_lua** +**context:** *init_worker_by_lua*, set_by_lua*, rewrite_by_lua*, access_by_lua*, content_by_lua*, header_filter_by_lua*, body_filter_by_lua*, log_by_lua*, ngx.timer.*, balancer_by_lua*, ssl_certificate_by_lua*, ssl_session_fetch_by_lua*, ssl_session_store_by_lua*, exit_worker_by_lua** Returns a formatted string can be used as the cookie expiration time. The parameter `sec` is the time stamp in seconds (like those returned from [ngx.time](#ngxtime)). @@ -6014,7 +6014,7 @@ ngx.http_time **syntax:** *str = ngx.http_time(sec)* -**context:** *init_worker_by_lua*, set_by_lua*, rewrite_by_lua*, access_by_lua*, content_by_lua*, header_filter_by_lua*, body_filter_by_lua*, log_by_lua*, ngx.timer.*, balancer_by_lua*, ssl_certificate_by_lua*, ssl_session_fetch_by_lua*, ssl_session_store_by_lua** +**context:** *init_worker_by_lua*, set_by_lua*, rewrite_by_lua*, access_by_lua*, content_by_lua*, header_filter_by_lua*, body_filter_by_lua*, log_by_lua*, ngx.timer.*, balancer_by_lua*, ssl_certificate_by_lua*, ssl_session_fetch_by_lua*, ssl_session_store_by_lua*, exit_worker_by_lua** Returns a formated string can be used as the http header time (for example, being used in `Last-Modified` header). The parameter `sec` is the time stamp in seconds (like those returned from [ngx.time](#ngxtime)). @@ -6031,7 +6031,7 @@ ngx.parse_http_time **syntax:** *sec = ngx.parse_http_time(str)* -**context:** *init_worker_by_lua*, set_by_lua*, rewrite_by_lua*, access_by_lua*, content_by_lua*, header_filter_by_lua*, body_filter_by_lua*, log_by_lua*, ngx.timer.*, balancer_by_lua*, ssl_certificate_by_lua*, ssl_session_fetch_by_lua*, ssl_session_store_by_lua** +**context:** *init_worker_by_lua*, set_by_lua*, rewrite_by_lua*, access_by_lua*, content_by_lua*, header_filter_by_lua*, body_filter_by_lua*, log_by_lua*, ngx.timer.*, balancer_by_lua*, ssl_certificate_by_lua*, ssl_session_fetch_by_lua*, ssl_session_store_by_lua*, exit_worker_by_lua** Parse the http time string (as returned by [ngx.http_time](#ngxhttp_time)) into seconds. Returns the seconds or `nil` if the input string is in bad forms. @@ -6061,7 +6061,7 @@ ngx.re.match **syntax:** *captures, err = ngx.re.match(subject, regex, options?, ctx?, res_table?)* -**context:** *init_worker_by_lua*, set_by_lua*, rewrite_by_lua*, access_by_lua*, content_by_lua*, header_filter_by_lua*, body_filter_by_lua*, log_by_lua*, ngx.timer.*, balancer_by_lua*, ssl_certificate_by_lua*, ssl_session_fetch_by_lua*, ssl_session_store_by_lua** +**context:** *init_worker_by_lua*, set_by_lua*, rewrite_by_lua*, access_by_lua*, content_by_lua*, header_filter_by_lua*, body_filter_by_lua*, log_by_lua*, ngx.timer.*, balancer_by_lua*, ssl_certificate_by_lua*, ssl_session_fetch_by_lua*, ssl_session_store_by_lua*, exit_worker_by_lua** Matches the `subject` string using the Perl compatible regular expression `regex` with the optional `options`. @@ -6220,7 +6220,7 @@ ngx.re.find **syntax:** *from, to, err = ngx.re.find(subject, regex, options?, ctx?, nth?)* -**context:** *init_worker_by_lua*, set_by_lua*, rewrite_by_lua*, access_by_lua*, content_by_lua*, header_filter_by_lua*, body_filter_by_lua*, log_by_lua*, ngx.timer.*, balancer_by_lua*, ssl_certificate_by_lua*, ssl_session_fetch_by_lua*, ssl_session_store_by_lua** +**context:** *init_worker_by_lua*, set_by_lua*, rewrite_by_lua*, access_by_lua*, content_by_lua*, header_filter_by_lua*, body_filter_by_lua*, log_by_lua*, ngx.timer.*, balancer_by_lua*, ssl_certificate_by_lua*, ssl_session_fetch_by_lua*, ssl_session_store_by_lua*, exit_worker_by_lua** Similar to [ngx.re.match](#ngxrematch) but only returns the beginning index (`from`) and end index (`to`) of the matched substring. The returned indexes are 1-based and can be fed directly into the [string.sub](https://www.lua.org/manual/5.1/manual.html#pdf-string.sub) API function to obtain the matched substring. @@ -6275,7 +6275,7 @@ ngx.re.gmatch **syntax:** *iterator, err = ngx.re.gmatch(subject, regex, options?)* -**context:** *init_worker_by_lua*, set_by_lua*, rewrite_by_lua*, access_by_lua*, content_by_lua*, header_filter_by_lua*, body_filter_by_lua*, log_by_lua*, ngx.timer.*, balancer_by_lua*, ssl_certificate_by_lua*, ssl_session_fetch_by_lua*, ssl_session_store_by_lua** +**context:** *init_worker_by_lua*, set_by_lua*, rewrite_by_lua*, access_by_lua*, content_by_lua*, header_filter_by_lua*, body_filter_by_lua*, log_by_lua*, ngx.timer.*, balancer_by_lua*, ssl_certificate_by_lua*, ssl_session_fetch_by_lua*, ssl_session_store_by_lua*, exit_worker_by_lua** Similar to [ngx.re.match](#ngxrematch), but returns a Lua iterator instead, so as to let the user programmer iterate all the matches over the `` string argument with the PCRE `regex`. @@ -6354,7 +6354,7 @@ ngx.re.sub **syntax:** *newstr, n, err = ngx.re.sub(subject, regex, replace, options?)* -**context:** *init_worker_by_lua*, set_by_lua*, rewrite_by_lua*, access_by_lua*, content_by_lua*, header_filter_by_lua*, body_filter_by_lua*, log_by_lua*, ngx.timer.*, balancer_by_lua*, ssl_certificate_by_lua*, ssl_session_fetch_by_lua*, ssl_session_store_by_lua** +**context:** *init_worker_by_lua*, set_by_lua*, rewrite_by_lua*, access_by_lua*, content_by_lua*, header_filter_by_lua*, body_filter_by_lua*, log_by_lua*, ngx.timer.*, balancer_by_lua*, ssl_certificate_by_lua*, ssl_session_fetch_by_lua*, ssl_session_store_by_lua*, exit_worker_by_lua** Substitutes the first match of the Perl compatible regular expression `regex` on the `subject` argument string with the string or function argument `replace`. The optional `options` argument has exactly the same meaning as in [ngx.re.match](#ngxrematch). @@ -6422,7 +6422,7 @@ ngx.re.gsub **syntax:** *newstr, n, err = ngx.re.gsub(subject, regex, replace, options?)* -**context:** *init_worker_by_lua*, set_by_lua*, rewrite_by_lua*, access_by_lua*, content_by_lua*, header_filter_by_lua*, body_filter_by_lua*, log_by_lua*, ngx.timer.*, balancer_by_lua*, ssl_certificate_by_lua*, ssl_session_fetch_by_lua*, ssl_session_store_by_lua** +**context:** *init_worker_by_lua*, set_by_lua*, rewrite_by_lua*, access_by_lua*, content_by_lua*, header_filter_by_lua*, body_filter_by_lua*, log_by_lua*, ngx.timer.*, balancer_by_lua*, ssl_certificate_by_lua*, ssl_session_fetch_by_lua*, ssl_session_store_by_lua*, exit_worker_by_lua** Just like [ngx.re.sub](#ngxresub), but does global substitution. @@ -6463,7 +6463,7 @@ ngx.shared.DICT **syntax:** *dict = ngx.shared\[name_var\]* -**context:** *init_by_lua*, init_worker_by_lua*, set_by_lua*, rewrite_by_lua*, access_by_lua*, content_by_lua*, header_filter_by_lua*, body_filter_by_lua*, log_by_lua*, ngx.timer.*, balancer_by_lua*, ssl_certificate_by_lua*, ssl_session_fetch_by_lua*, ssl_session_store_by_lua** +**context:** *init_by_lua*, init_worker_by_lua*, set_by_lua*, rewrite_by_lua*, access_by_lua*, content_by_lua*, header_filter_by_lua*, body_filter_by_lua*, log_by_lua*, ngx.timer.*, balancer_by_lua*, ssl_certificate_by_lua*, ssl_session_fetch_by_lua*, ssl_session_store_by_lua*, exit_worker_by_lua** Fetching the shm-based Lua dictionary object for the shared memory zone named `DICT` defined by the [lua_shared_dict](#lua_shared_dict) directive. @@ -7845,7 +7845,7 @@ ngx.get_phase **syntax:** *str = ngx.get_phase()* -**context:** *init_by_lua*, init_worker_by_lua*, set_by_lua*, rewrite_by_lua*, access_by_lua*, content_by_lua*, header_filter_by_lua*, body_filter_by_lua*, log_by_lua*, ngx.timer.*, balancer_by_lua*, ssl_certificate_by_lua*, ssl_session_fetch_by_lua*, ssl_session_store_by_lua** +**context:** *init_by_lua*, init_worker_by_lua*, set_by_lua*, rewrite_by_lua*, access_by_lua*, content_by_lua*, header_filter_by_lua*, body_filter_by_lua*, log_by_lua*, ngx.timer.*, balancer_by_lua*, ssl_certificate_by_lua*, ssl_session_fetch_by_lua*, ssl_session_store_by_lua*, exit_worker_by_lua** Retrieves the current running phase name. Possible return values are @@ -8188,7 +8188,7 @@ ngx.timer.at **syntax:** *hdl, err = ngx.timer.at(delay, callback, user_arg1, user_arg2, ...)* -**context:** *init_worker_by_lua*, set_by_lua*, rewrite_by_lua*, access_by_lua*, content_by_lua*, header_filter_by_lua*, body_filter_by_lua*, log_by_lua*, ngx.timer.*, balancer_by_lua*, ssl_certificate_by_lua*, ssl_session_fetch_by_lua*, ssl_session_store_by_lua** +**context:** *init_worker_by_lua*, set_by_lua*, rewrite_by_lua*, access_by_lua*, content_by_lua*, header_filter_by_lua*, body_filter_by_lua*, log_by_lua*, ngx.timer.*, balancer_by_lua*, ssl_certificate_by_lua*, ssl_session_fetch_by_lua*, ssl_session_store_by_lua*, exit_worker_by_lua** Creates an Nginx timer with a user callback function as well as optional user arguments. @@ -8324,7 +8324,7 @@ ngx.timer.every **syntax:** *hdl, err = ngx.timer.every(delay, callback, user_arg1, user_arg2, ...)* -**context:** *init_worker_by_lua*, set_by_lua*, rewrite_by_lua*, access_by_lua*, content_by_lua*, header_filter_by_lua*, body_filter_by_lua*, log_by_lua*, ngx.timer.*, balancer_by_lua*, ssl_certificate_by_lua*, ssl_session_fetch_by_lua*, ssl_session_store_by_lua** +**context:** *init_worker_by_lua*, set_by_lua*, rewrite_by_lua*, access_by_lua*, content_by_lua*, header_filter_by_lua*, body_filter_by_lua*, log_by_lua*, ngx.timer.*, balancer_by_lua*, ssl_certificate_by_lua*, ssl_session_fetch_by_lua*, ssl_session_store_by_lua*, exit_worker_by_lua** Similar to the [ngx.timer.at](#ngxtimerat) API function, but @@ -8347,7 +8347,7 @@ ngx.timer.running_count **syntax:** *count = ngx.timer.running_count()* -**context:** *init_worker_by_lua*, set_by_lua*, rewrite_by_lua*, access_by_lua*, content_by_lua*, header_filter_by_lua*, body_filter_by_lua*, log_by_lua*, ngx.timer.*, balancer_by_lua*, ssl_certificate_by_lua*, ssl_session_fetch_by_lua*, ssl_session_store_by_lua** +**context:** *init_worker_by_lua*, set_by_lua*, rewrite_by_lua*, access_by_lua*, content_by_lua*, header_filter_by_lua*, body_filter_by_lua*, log_by_lua*, ngx.timer.*, balancer_by_lua*, ssl_certificate_by_lua*, ssl_session_fetch_by_lua*, ssl_session_store_by_lua*, exit_worker_by_lua** Returns the number of timers currently running. @@ -8360,7 +8360,7 @@ ngx.timer.pending_count **syntax:** *count = ngx.timer.pending_count()* -**context:** *init_worker_by_lua*, set_by_lua*, rewrite_by_lua*, access_by_lua*, content_by_lua*, header_filter_by_lua*, body_filter_by_lua*, log_by_lua*, ngx.timer.*, balancer_by_lua*, ssl_certificate_by_lua*, ssl_session_fetch_by_lua*, ssl_session_store_by_lua** +**context:** *init_worker_by_lua*, set_by_lua*, rewrite_by_lua*, access_by_lua*, content_by_lua*, header_filter_by_lua*, body_filter_by_lua*, log_by_lua*, ngx.timer.*, balancer_by_lua*, ssl_certificate_by_lua*, ssl_session_fetch_by_lua*, ssl_session_store_by_lua*, exit_worker_by_lua** Returns the number of pending timers. @@ -8373,7 +8373,7 @@ ngx.config.subsystem **syntax:** *subsystem = ngx.config.subsystem* -**context:** *set_by_lua*, rewrite_by_lua*, access_by_lua*, content_by_lua*, header_filter_by_lua*, body_filter_by_lua*, log_by_lua*, ngx.timer.*, init_by_lua*, init_worker_by_lua** +**context:** *set_by_lua*, rewrite_by_lua*, access_by_lua*, content_by_lua*, header_filter_by_lua*, body_filter_by_lua*, log_by_lua*, ngx.timer.*, init_by_lua*, init_worker_by_lua*, exit_worker_by_lua** This string field indicates the Nginx subsystem the current Lua environment is based on. For this module, this field always takes the string value `"http"`. For [ngx_stream_lua_module](https://github.com/openresty/stream-lua-nginx-module#readme), however, this field takes the value `"stream"`. @@ -8387,7 +8387,7 @@ ngx.config.debug **syntax:** *debug = ngx.config.debug* -**context:** *set_by_lua*, rewrite_by_lua*, access_by_lua*, content_by_lua*, header_filter_by_lua*, body_filter_by_lua*, log_by_lua*, ngx.timer.*, init_by_lua*, init_worker_by_lua** +**context:** *set_by_lua*, rewrite_by_lua*, access_by_lua*, content_by_lua*, header_filter_by_lua*, body_filter_by_lua*, log_by_lua*, ngx.timer.*, init_by_lua*, init_worker_by_lua*, exit_worker_by_lua** This boolean field indicates whether the current Nginx is a debug build, i.e., being built by the `./configure` option `--with-debug`. @@ -8400,7 +8400,7 @@ ngx.config.prefix **syntax:** *prefix = ngx.config.prefix()* -**context:** *set_by_lua*, rewrite_by_lua*, access_by_lua*, content_by_lua*, header_filter_by_lua*, body_filter_by_lua*, log_by_lua*, ngx.timer.*, init_by_lua*, init_worker_by_lua** +**context:** *set_by_lua*, rewrite_by_lua*, access_by_lua*, content_by_lua*, header_filter_by_lua*, body_filter_by_lua*, log_by_lua*, ngx.timer.*, init_by_lua*, init_worker_by_lua*, exit_worker_by_lua** Returns the Nginx server "prefix" path, as determined by the `-p` command-line option when running the Nginx executable, or the path specified by the `--prefix` command-line option when building Nginx with the `./configure` script. @@ -8413,7 +8413,7 @@ ngx.config.nginx_version **syntax:** *ver = ngx.config.nginx_version* -**context:** *set_by_lua*, rewrite_by_lua*, access_by_lua*, content_by_lua*, header_filter_by_lua*, body_filter_by_lua*, log_by_lua*, ngx.timer.*, init_by_lua*, init_worker_by_lua** +**context:** *set_by_lua*, rewrite_by_lua*, access_by_lua*, content_by_lua*, header_filter_by_lua*, body_filter_by_lua*, log_by_lua*, ngx.timer.*, init_by_lua*, init_worker_by_lua*, exit_worker_by_lua** This field take an integral value indicating the version number of the current Nginx core being used. For example, the version number `1.4.3` results in the Lua number 1004003. @@ -8452,7 +8452,7 @@ ngx.worker.exiting **syntax:** *exiting = ngx.worker.exiting()* -**context:** *set_by_lua*, rewrite_by_lua*, access_by_lua*, content_by_lua*, header_filter_by_lua*, body_filter_by_lua*, log_by_lua*, ngx.timer.*, init_by_lua*, init_worker_by_lua** +**context:** *set_by_lua*, rewrite_by_lua*, access_by_lua*, content_by_lua*, header_filter_by_lua*, body_filter_by_lua*, log_by_lua*, ngx.timer.*, init_by_lua*, init_worker_by_lua*, exit_worker_by_lua** This function returns a boolean value indicating whether the current Nginx worker process already starts exiting. Nginx worker process exiting happens on Nginx server quit or configuration reload (aka HUP reload). @@ -8465,7 +8465,7 @@ ngx.worker.pid **syntax:** *pid = ngx.worker.pid()* -**context:** *set_by_lua*, rewrite_by_lua*, access_by_lua*, content_by_lua*, header_filter_by_lua*, body_filter_by_lua*, log_by_lua*, ngx.timer.*, init_by_lua*, init_worker_by_lua** +**context:** *set_by_lua*, rewrite_by_lua*, access_by_lua*, content_by_lua*, header_filter_by_lua*, body_filter_by_lua*, log_by_lua*, ngx.timer.*, init_by_lua*, init_worker_by_lua*, exit_worker_by_lua** This function returns a Lua number for the process ID (PID) of the current Nginx worker process. This API is more efficient than `ngx.var.pid` and can be used in contexts where the [ngx.var.VARIABLE](#ngxvarvariable) API cannot be used (like [init_worker_by_lua](#init_worker_by_lua)). @@ -8478,7 +8478,7 @@ ngx.worker.count **syntax:** *count = ngx.worker.count()* -**context:** *set_by_lua*, rewrite_by_lua*, access_by_lua*, content_by_lua*, header_filter_by_lua*, body_filter_by_lua*, log_by_lua*, ngx.timer.*, init_by_lua*, init_worker_by_lua** +**context:** *set_by_lua*, rewrite_by_lua*, access_by_lua*, content_by_lua*, header_filter_by_lua*, body_filter_by_lua*, log_by_lua*, ngx.timer.*, init_by_lua*, init_worker_by_lua*, exit_worker_by_lua** Returns the total number of the Nginx worker processes (i.e., the value configured by the [worker_processes](https://nginx.org/en/docs/ngx_core_module.html#worker_processes) @@ -8493,7 +8493,7 @@ ngx.worker.id **syntax:** *id = ngx.worker.id()* -**context:** *set_by_lua*, rewrite_by_lua*, access_by_lua*, content_by_lua*, header_filter_by_lua*, body_filter_by_lua*, log_by_lua*, ngx.timer.*, init_worker_by_lua** +**context:** *set_by_lua*, rewrite_by_lua*, access_by_lua*, content_by_lua*, header_filter_by_lua*, body_filter_by_lua*, log_by_lua*, ngx.timer.*, init_worker_by_lua*, exit_worker_by_lua** Returns the ordinal number of the current Nginx worker processes (starting from number 0). @@ -8598,7 +8598,7 @@ ndk.set_var.DIRECTIVE **syntax:** *res = ndk.set_var.DIRECTIVE_NAME* -**context:** *init_worker_by_lua*, set_by_lua*, rewrite_by_lua*, access_by_lua*, content_by_lua*, header_filter_by_lua*, body_filter_by_lua*, log_by_lua*, ngx.timer.*, balancer_by_lua*, ssl_certificate_by_lua*, ssl_session_fetch_by_lua*, ssl_session_store_by_lua** +**context:** *init_worker_by_lua*, set_by_lua*, rewrite_by_lua*, access_by_lua*, content_by_lua*, header_filter_by_lua*, body_filter_by_lua*, log_by_lua*, ngx.timer.*, balancer_by_lua*, ssl_certificate_by_lua*, ssl_session_fetch_by_lua*, ssl_session_store_by_lua*, exit_worker_by_lua** This mechanism allows calling other Nginx C modules' directives that are implemented by [Nginx Devel Kit](https://github.com/simplresty/ngx_devel_kit) (NDK)'s set_var submodule's `ndk_set_var_value`. diff --git a/doc/HttpLuaModule.wiki b/doc/HttpLuaModule.wiki index 4ce3494904..e9e30efa18 100644 --- a/doc/HttpLuaModule.wiki +++ b/doc/HttpLuaModule.wiki @@ -2943,7 +2943,7 @@ These constants are usually used in [[#ngx.location.capture|ngx.location.capture == Nginx log level constants == -'''context:''' ''init_by_lua*, init_worker_by_lua*, set_by_lua*, rewrite_by_lua*, access_by_lua*, content_by_lua*, header_filter_by_lua*, body_filter_by_lua*, log_by_lua*, ngx.timer.*, balancer_by_lua*, ssl_certificate_by_lua*, ssl_session_fetch_by_lua*, ssl_session_store_by_lua*'' +'''context:''' ''init_by_lua*, init_worker_by_lua*, set_by_lua*, rewrite_by_lua*, access_by_lua*, content_by_lua*, header_filter_by_lua*, body_filter_by_lua*, log_by_lua*, ngx.timer.*, balancer_by_lua*, ssl_certificate_by_lua*, ssl_session_fetch_by_lua*, ssl_session_store_by_lua*, exit_worker_by_lua*'' ngx.STDERR @@ -2963,7 +2963,7 @@ These constants are usually used by the [[#ngx.log|ngx.log]] method. '''syntax:''' ''print(...)'' -'''context:''' ''init_by_lua*, init_worker_by_lua*, set_by_lua*, rewrite_by_lua*, access_by_lua*, content_by_lua*, header_filter_by_lua*, body_filter_by_lua*, log_by_lua*, ngx.timer.*, balancer_by_lua*, ssl_certificate_by_lua*, ssl_session_fetch_by_lua*, ssl_session_store_by_lua*'' +'''context:''' ''init_by_lua*, init_worker_by_lua*, set_by_lua*, rewrite_by_lua*, access_by_lua*, content_by_lua*, header_filter_by_lua*, body_filter_by_lua*, log_by_lua*, ngx.timer.*, balancer_by_lua*, ssl_certificate_by_lua*, ssl_session_fetch_by_lua*, ssl_session_store_by_lua*, exit_worker_by_lua*'' Writes argument values into the Nginx error.log file with the ngx.NOTICE log level. @@ -2979,7 +2979,7 @@ There is a hard coded 2048 byte limitation on error message lengths == ngx.ctx == -'''context:''' ''init_worker_by_lua*, set_by_lua*, rewrite_by_lua*, access_by_lua*, content_by_lua*, header_filter_by_lua*, body_filter_by_lua*, log_by_lua*, ngx.timer.*, balancer_by_lua*'' +'''context:''' ''init_worker_by_lua*, set_by_lua*, rewrite_by_lua*, access_by_lua*, content_by_lua*, header_filter_by_lua*, body_filter_by_lua*, log_by_lua*, ngx.timer.*, balancer_by_lua*, exit_worker_by_lua*'' This table can be used to store per-request Lua context data and has a life time identical to the current request (as with the Nginx variables). @@ -4603,7 +4603,7 @@ Just as [[#ngx.print|ngx.print]] but also emit a trailing newline. '''syntax:''' ''ngx.log(log_level, ...)'' -'''context:''' ''init_by_lua*, init_worker_by_lua*, set_by_lua*, rewrite_by_lua*, access_by_lua*, content_by_lua*, header_filter_by_lua*, body_filter_by_lua*, log_by_lua*, ngx.timer.*, balancer_by_lua*, ssl_certificate_by_lua*, ssl_session_fetch_by_lua*, ssl_session_store_by_lua*'' +'''context:''' ''init_by_lua*, init_worker_by_lua*, set_by_lua*, rewrite_by_lua*, access_by_lua*, content_by_lua*, header_filter_by_lua*, body_filter_by_lua*, log_by_lua*, ngx.timer.*, balancer_by_lua*, ssl_certificate_by_lua*, ssl_session_fetch_by_lua*, ssl_session_store_by_lua*, exit_worker_by_lua*'' Log arguments concatenated to error.log with the given logging level. @@ -4730,7 +4730,7 @@ This method was introduced in the 0.5.0rc30 release. '''syntax:''' ''newstr = ngx.escape_uri(str, type?)'' -'''context:''' ''init_by_lua*, init_worker_by_lua*, set_by_lua*, rewrite_by_lua*, access_by_lua*, content_by_lua*, header_filter_by_lua*, body_filter_by_lua*, log_by_lua*, ngx.timer.*, balancer_by_lua*, ssl_certificate_by_lua*, ssl_session_fetch_by_lua*, ssl_session_store_by_lua*'' +'''context:''' ''init_by_lua*, init_worker_by_lua*, set_by_lua*, rewrite_by_lua*, access_by_lua*, content_by_lua*, header_filter_by_lua*, body_filter_by_lua*, log_by_lua*, ngx.timer.*, balancer_by_lua*, ssl_certificate_by_lua*, ssl_session_fetch_by_lua*, ssl_session_store_by_lua*, exit_worker_by_lua*'' Since `v0.10.16`, this function accepts an optional type argument. It accepts the following values (defaults to `2`): @@ -4746,7 +4746,7 @@ alphabetic characters, digits, -, ., _, '''syntax:''' ''newstr = ngx.unescape_uri(str)'' -'''context:''' ''init_by_lua*, init_worker_by_lua*, set_by_lua*, rewrite_by_lua*, access_by_lua*, content_by_lua*, header_filter_by_lua*, body_filter_by_lua*, log_by_lua*, ngx.timer.*, balancer_by_lua*, ssl_certificate_by_lua*'' +'''context:''' ''init_by_lua*, init_worker_by_lua*, set_by_lua*, rewrite_by_lua*, access_by_lua*, content_by_lua*, header_filter_by_lua*, body_filter_by_lua*, log_by_lua*, ngx.timer.*, balancer_by_lua*, ssl_certificate_by_lua*, exit_worker_by_lua*'' Unescape str as an escaped URI component. @@ -4967,7 +4967,7 @@ Returns a quoted SQL string literal according to the MySQL quoting rules. '''syntax:''' ''str = ngx.today()'' -'''context:''' ''init_worker_by_lua*, set_by_lua*, rewrite_by_lua*, access_by_lua*, content_by_lua*, header_filter_by_lua*, body_filter_by_lua*, log_by_lua*, ngx.timer.*, balancer_by_lua*, ssl_certificate_by_lua*, ssl_session_fetch_by_lua*, ssl_session_store_by_lua*'' +'''context:''' ''init_worker_by_lua*, set_by_lua*, rewrite_by_lua*, access_by_lua*, content_by_lua*, header_filter_by_lua*, body_filter_by_lua*, log_by_lua*, ngx.timer.*, balancer_by_lua*, ssl_certificate_by_lua*, ssl_session_fetch_by_lua*, ssl_session_store_by_lua*, exit_worker_by_lua*'' Returns current date (in the format yyyy-mm-dd) from the Nginx cached time (no syscall involved unlike Lua's date library). @@ -4977,7 +4977,7 @@ This is the local time. '''syntax:''' ''secs = ngx.time()'' -'''context:''' ''init_worker_by_lua*, set_by_lua*, rewrite_by_lua*, access_by_lua*, content_by_lua*, header_filter_by_lua*, body_filter_by_lua*, log_by_lua*, ngx.timer.*, balancer_by_lua*, ssl_certificate_by_lua*, ssl_session_fetch_by_lua*, ssl_session_store_by_lua*'' +'''context:''' ''init_worker_by_lua*, set_by_lua*, rewrite_by_lua*, access_by_lua*, content_by_lua*, header_filter_by_lua*, body_filter_by_lua*, log_by_lua*, ngx.timer.*, balancer_by_lua*, ssl_certificate_by_lua*, ssl_session_fetch_by_lua*, ssl_session_store_by_lua*, exit_worker_by_lua*'' Returns the elapsed seconds from the epoch for the current time stamp from the Nginx cached time (no syscall involved unlike Lua's date library). @@ -4987,7 +4987,7 @@ Updates of the Nginx time cache can be forced by calling [[#ngx.update_time|ngx. '''syntax:''' ''secs = ngx.now()'' -'''context:''' ''init_worker_by_lua*, set_by_lua*, rewrite_by_lua*, access_by_lua*, content_by_lua*, header_filter_by_lua*, body_filter_by_lua*, log_by_lua*, ngx.timer.*, balancer_by_lua*, ssl_certificate_by_lua*, ssl_session_fetch_by_lua*, ssl_session_store_by_lua*'' +'''context:''' ''init_worker_by_lua*, set_by_lua*, rewrite_by_lua*, access_by_lua*, content_by_lua*, header_filter_by_lua*, body_filter_by_lua*, log_by_lua*, ngx.timer.*, balancer_by_lua*, ssl_certificate_by_lua*, ssl_session_fetch_by_lua*, ssl_session_store_by_lua*, exit_worker_by_lua*'' Returns a floating-point number for the elapsed time in seconds (including milliseconds as the decimal part) from the epoch for the current time stamp from the Nginx cached time (no syscall involved unlike Lua's date library). @@ -4999,7 +4999,7 @@ This API was first introduced in v0.3.1rc32. '''syntax:''' ''ngx.update_time()'' -'''context:''' ''init_worker_by_lua*, set_by_lua*, rewrite_by_lua*, access_by_lua*, content_by_lua*, header_filter_by_lua*, body_filter_by_lua*, log_by_lua*, ngx.timer.*, balancer_by_lua*, ssl_certificate_by_lua*, ssl_session_fetch_by_lua*, ssl_session_store_by_lua*'' +'''context:''' ''init_worker_by_lua*, set_by_lua*, rewrite_by_lua*, access_by_lua*, content_by_lua*, header_filter_by_lua*, body_filter_by_lua*, log_by_lua*, ngx.timer.*, balancer_by_lua*, ssl_certificate_by_lua*, ssl_session_fetch_by_lua*, ssl_session_store_by_lua*, exit_worker_by_lua*'' Forcibly updates the Nginx current time cache. This call involves a syscall and thus has some overhead, so do not abuse it. @@ -5009,7 +5009,7 @@ This API was first introduced in v0.3.1rc32. '''syntax:''' ''str = ngx.localtime()'' -'''context:''' ''init_worker_by_lua*, set_by_lua*, rewrite_by_lua*, access_by_lua*, content_by_lua*, header_filter_by_lua*, body_filter_by_lua*, log_by_lua*, ngx.timer.*, balancer_by_lua*, ssl_certificate_by_lua*, ssl_session_fetch_by_lua*, ssl_session_store_by_lua*'' +'''context:''' ''init_worker_by_lua*, set_by_lua*, rewrite_by_lua*, access_by_lua*, content_by_lua*, header_filter_by_lua*, body_filter_by_lua*, log_by_lua*, ngx.timer.*, balancer_by_lua*, ssl_certificate_by_lua*, ssl_session_fetch_by_lua*, ssl_session_store_by_lua*, exit_worker_by_lua*'' Returns the current time stamp (in the format yyyy-mm-dd hh:mm:ss) of the Nginx cached time (no syscall involved unlike Lua's [https://www.lua.org/manual/5.1/manual.html#pdf-os.date os.date] function). @@ -5019,7 +5019,7 @@ This is the local time. '''syntax:''' ''str = ngx.utctime()'' -'''context:''' ''init_worker_by_lua*, set_by_lua*, rewrite_by_lua*, access_by_lua*, content_by_lua*, header_filter_by_lua*, body_filter_by_lua*, log_by_lua*, ngx.timer.*, balancer_by_lua*, ssl_certificate_by_lua*, ssl_session_fetch_by_lua*, ssl_session_store_by_lua*'' +'''context:''' ''init_worker_by_lua*, set_by_lua*, rewrite_by_lua*, access_by_lua*, content_by_lua*, header_filter_by_lua*, body_filter_by_lua*, log_by_lua*, ngx.timer.*, balancer_by_lua*, ssl_certificate_by_lua*, ssl_session_fetch_by_lua*, ssl_session_store_by_lua*, exit_worker_by_lua*'' Returns the current time stamp (in the format yyyy-mm-dd hh:mm:ss) of the Nginx cached time (no syscall involved unlike Lua's [https://www.lua.org/manual/5.1/manual.html#pdf-os.date os.date] function). @@ -5029,7 +5029,7 @@ This is the UTC time. '''syntax:''' ''str = ngx.cookie_time(sec)'' -'''context:''' ''init_worker_by_lua*, set_by_lua*, rewrite_by_lua*, access_by_lua*, content_by_lua*, header_filter_by_lua*, body_filter_by_lua*, log_by_lua*, ngx.timer.*, balancer_by_lua*, ssl_certificate_by_lua*, ssl_session_fetch_by_lua*, ssl_session_store_by_lua*'' +'''context:''' ''init_worker_by_lua*, set_by_lua*, rewrite_by_lua*, access_by_lua*, content_by_lua*, header_filter_by_lua*, body_filter_by_lua*, log_by_lua*, ngx.timer.*, balancer_by_lua*, ssl_certificate_by_lua*, ssl_session_fetch_by_lua*, ssl_session_store_by_lua*, exit_worker_by_lua*'' Returns a formatted string can be used as the cookie expiration time. The parameter sec is the time stamp in seconds (like those returned from [[#ngx.time|ngx.time]]). @@ -5042,7 +5042,7 @@ Returns a formatted string can be used as the cookie expiration time. The parame '''syntax:''' ''str = ngx.http_time(sec)'' -'''context:''' ''init_worker_by_lua*, set_by_lua*, rewrite_by_lua*, access_by_lua*, content_by_lua*, header_filter_by_lua*, body_filter_by_lua*, log_by_lua*, ngx.timer.*, balancer_by_lua*, ssl_certificate_by_lua*, ssl_session_fetch_by_lua*, ssl_session_store_by_lua*'' +'''context:''' ''init_worker_by_lua*, set_by_lua*, rewrite_by_lua*, access_by_lua*, content_by_lua*, header_filter_by_lua*, body_filter_by_lua*, log_by_lua*, ngx.timer.*, balancer_by_lua*, ssl_certificate_by_lua*, ssl_session_fetch_by_lua*, ssl_session_store_by_lua*, exit_worker_by_lua*'' Returns a formated string can be used as the http header time (for example, being used in Last-Modified header). The parameter sec is the time stamp in seconds (like those returned from [[#ngx.time|ngx.time]]). @@ -5055,7 +5055,7 @@ Returns a formated string can be used as the http header time (for example, bein '''syntax:''' ''sec = ngx.parse_http_time(str)'' -'''context:''' ''init_worker_by_lua*, set_by_lua*, rewrite_by_lua*, access_by_lua*, content_by_lua*, header_filter_by_lua*, body_filter_by_lua*, log_by_lua*, ngx.timer.*, balancer_by_lua*, ssl_certificate_by_lua*, ssl_session_fetch_by_lua*, ssl_session_store_by_lua*'' +'''context:''' ''init_worker_by_lua*, set_by_lua*, rewrite_by_lua*, access_by_lua*, content_by_lua*, header_filter_by_lua*, body_filter_by_lua*, log_by_lua*, ngx.timer.*, balancer_by_lua*, ssl_certificate_by_lua*, ssl_session_fetch_by_lua*, ssl_session_store_by_lua*, exit_worker_by_lua*'' Parse the http time string (as returned by [[#ngx.http_time|ngx.http_time]]) into seconds. Returns the seconds or nil if the input string is in bad forms. @@ -5078,7 +5078,7 @@ Returns true if the current request is an Nginx subrequest, or subject string using the Perl compatible regular expression regex with the optional options. @@ -5226,7 +5226,7 @@ This feature was introduced in the v0.2.1rc11 release. '''syntax:''' ''from, to, err = ngx.re.find(subject, regex, options?, ctx?, nth?)'' -'''context:''' ''init_worker_by_lua*, set_by_lua*, rewrite_by_lua*, access_by_lua*, content_by_lua*, header_filter_by_lua*, body_filter_by_lua*, log_by_lua*, ngx.timer.*, balancer_by_lua*, ssl_certificate_by_lua*, ssl_session_fetch_by_lua*, ssl_session_store_by_lua*'' +'''context:''' ''init_worker_by_lua*, set_by_lua*, rewrite_by_lua*, access_by_lua*, content_by_lua*, header_filter_by_lua*, body_filter_by_lua*, log_by_lua*, ngx.timer.*, balancer_by_lua*, ssl_certificate_by_lua*, ssl_session_fetch_by_lua*, ssl_session_store_by_lua*, exit_worker_by_lua*'' Similar to [[#ngx.re.match|ngx.re.match]] but only returns the beginning index (from) and end index (to) of the matched substring. The returned indexes are 1-based and can be fed directly into the [https://www.lua.org/manual/5.1/manual.html#pdf-string.sub string.sub] API function to obtain the matched substring. @@ -5276,7 +5276,7 @@ This API function was first introduced in the v0.9.2 release. '''syntax:''' ''iterator, err = ngx.re.gmatch(subject, regex, options?)'' -'''context:''' ''init_worker_by_lua*, set_by_lua*, rewrite_by_lua*, access_by_lua*, content_by_lua*, header_filter_by_lua*, body_filter_by_lua*, log_by_lua*, ngx.timer.*, balancer_by_lua*, ssl_certificate_by_lua*, ssl_session_fetch_by_lua*, ssl_session_store_by_lua*'' +'''context:''' ''init_worker_by_lua*, set_by_lua*, rewrite_by_lua*, access_by_lua*, content_by_lua*, header_filter_by_lua*, body_filter_by_lua*, log_by_lua*, ngx.timer.*, balancer_by_lua*, ssl_certificate_by_lua*, ssl_session_fetch_by_lua*, ssl_session_store_by_lua*, exit_worker_by_lua*'' Similar to [[#ngx.re.match|ngx.re.match]], but returns a Lua iterator instead, so as to let the user programmer iterate all the matches over the string argument with the PCRE regex. @@ -5350,7 +5350,7 @@ This feature was first introduced in the v0.2.1rc12 release. '''syntax:''' ''newstr, n, err = ngx.re.sub(subject, regex, replace, options?)'' -'''context:''' ''init_worker_by_lua*, set_by_lua*, rewrite_by_lua*, access_by_lua*, content_by_lua*, header_filter_by_lua*, body_filter_by_lua*, log_by_lua*, ngx.timer.*, balancer_by_lua*, ssl_certificate_by_lua*, ssl_session_fetch_by_lua*, ssl_session_store_by_lua*'' +'''context:''' ''init_worker_by_lua*, set_by_lua*, rewrite_by_lua*, access_by_lua*, content_by_lua*, header_filter_by_lua*, body_filter_by_lua*, log_by_lua*, ngx.timer.*, balancer_by_lua*, ssl_certificate_by_lua*, ssl_session_fetch_by_lua*, ssl_session_store_by_lua*, exit_worker_by_lua*'' Substitutes the first match of the Perl compatible regular expression regex on the subject argument string with the string or function argument replace. The optional options argument has exactly the same meaning as in [[#ngx.re.match|ngx.re.match]]. @@ -5411,7 +5411,7 @@ This feature was first introduced in the v0.2.1rc13 release. '''syntax:''' ''newstr, n, err = ngx.re.gsub(subject, regex, replace, options?)'' -'''context:''' ''init_worker_by_lua*, set_by_lua*, rewrite_by_lua*, access_by_lua*, content_by_lua*, header_filter_by_lua*, body_filter_by_lua*, log_by_lua*, ngx.timer.*, balancer_by_lua*, ssl_certificate_by_lua*, ssl_session_fetch_by_lua*, ssl_session_store_by_lua*'' +'''context:''' ''init_worker_by_lua*, set_by_lua*, rewrite_by_lua*, access_by_lua*, content_by_lua*, header_filter_by_lua*, body_filter_by_lua*, log_by_lua*, ngx.timer.*, balancer_by_lua*, ssl_certificate_by_lua*, ssl_session_fetch_by_lua*, ssl_session_store_by_lua*, exit_worker_by_lua*'' Just like [[#ngx.re.sub|ngx.re.sub]], but does global substitution. @@ -5447,7 +5447,7 @@ This feature was first introduced in the v0.2.1rc15 release. '''syntax:''' ''dict = ngx.shared[name_var]'' -'''context:''' ''init_by_lua*, init_worker_by_lua*, set_by_lua*, rewrite_by_lua*, access_by_lua*, content_by_lua*, header_filter_by_lua*, body_filter_by_lua*, log_by_lua*, ngx.timer.*, balancer_by_lua*, ssl_certificate_by_lua*, ssl_session_fetch_by_lua*, ssl_session_store_by_lua*'' +'''context:''' ''init_by_lua*, init_worker_by_lua*, set_by_lua*, rewrite_by_lua*, access_by_lua*, content_by_lua*, header_filter_by_lua*, body_filter_by_lua*, log_by_lua*, ngx.timer.*, balancer_by_lua*, ssl_certificate_by_lua*, ssl_session_fetch_by_lua*, ssl_session_store_by_lua*, exit_worker_by_lua*'' Fetching the shm-based Lua dictionary object for the shared memory zone named DICT defined by the [[#lua_shared_dict|lua_shared_dict]] directive. @@ -6673,7 +6673,7 @@ This feature was first introduced in the v0.5.0rc1 release. '''syntax:''' ''str = ngx.get_phase()'' -'''context:''' ''init_by_lua*, init_worker_by_lua*, set_by_lua*, rewrite_by_lua*, access_by_lua*, content_by_lua*, header_filter_by_lua*, body_filter_by_lua*, log_by_lua*, ngx.timer.*, balancer_by_lua*, ssl_certificate_by_lua*, ssl_session_fetch_by_lua*, ssl_session_store_by_lua*'' +'''context:''' ''init_by_lua*, init_worker_by_lua*, set_by_lua*, rewrite_by_lua*, access_by_lua*, content_by_lua*, header_filter_by_lua*, body_filter_by_lua*, log_by_lua*, ngx.timer.*, balancer_by_lua*, ssl_certificate_by_lua*, ssl_session_fetch_by_lua*, ssl_session_store_by_lua*, exit_worker_by_lua*'' Retrieves the current running phase name. Possible return values are @@ -6996,7 +6996,7 @@ See also [[#lua_check_client_abort|lua_check_client_abort]]. '''syntax:''' ''hdl, err = ngx.timer.at(delay, callback, user_arg1, user_arg2, ...)'' -'''context:''' ''init_worker_by_lua*, set_by_lua*, rewrite_by_lua*, access_by_lua*, content_by_lua*, header_filter_by_lua*, body_filter_by_lua*, log_by_lua*, ngx.timer.*, balancer_by_lua*, ssl_certificate_by_lua*, ssl_session_fetch_by_lua*, ssl_session_store_by_lua*'' +'''context:''' ''init_worker_by_lua*, set_by_lua*, rewrite_by_lua*, access_by_lua*, content_by_lua*, header_filter_by_lua*, body_filter_by_lua*, log_by_lua*, ngx.timer.*, balancer_by_lua*, ssl_certificate_by_lua*, ssl_session_fetch_by_lua*, ssl_session_store_by_lua*, exit_worker_by_lua*'' Creates an Nginx timer with a user callback function as well as optional user arguments. @@ -7127,7 +7127,7 @@ This API was first introduced in the v0.8.0 release. '''syntax:''' ''hdl, err = ngx.timer.every(delay, callback, user_arg1, user_arg2, ...)'' -'''context:''' ''init_worker_by_lua*, set_by_lua*, rewrite_by_lua*, access_by_lua*, content_by_lua*, header_filter_by_lua*, body_filter_by_lua*, log_by_lua*, ngx.timer.*, balancer_by_lua*, ssl_certificate_by_lua*, ssl_session_fetch_by_lua*, ssl_session_store_by_lua*'' +'''context:''' ''init_worker_by_lua*, set_by_lua*, rewrite_by_lua*, access_by_lua*, content_by_lua*, header_filter_by_lua*, body_filter_by_lua*, log_by_lua*, ngx.timer.*, balancer_by_lua*, ssl_certificate_by_lua*, ssl_session_fetch_by_lua*, ssl_session_store_by_lua*, exit_worker_by_lua*'' Similar to the [[#ngx.timer.at|ngx.timer.at]] API function, but @@ -7147,7 +7147,7 @@ This API was first introduced in the v0.10.9 release. '''syntax:''' ''count = ngx.timer.running_count()'' -'''context:''' ''init_worker_by_lua*, set_by_lua*, rewrite_by_lua*, access_by_lua*, content_by_lua*, header_filter_by_lua*, body_filter_by_lua*, log_by_lua*, ngx.timer.*, balancer_by_lua*, ssl_certificate_by_lua*, ssl_session_fetch_by_lua*, ssl_session_store_by_lua*'' +'''context:''' ''init_worker_by_lua*, set_by_lua*, rewrite_by_lua*, access_by_lua*, content_by_lua*, header_filter_by_lua*, body_filter_by_lua*, log_by_lua*, ngx.timer.*, balancer_by_lua*, ssl_certificate_by_lua*, ssl_session_fetch_by_lua*, ssl_session_store_by_lua*, exit_worker_by_lua*'' Returns the number of timers currently running. @@ -7157,7 +7157,7 @@ This directive was first introduced in the v0.9.20 release. '''syntax:''' ''count = ngx.timer.pending_count()'' -'''context:''' ''init_worker_by_lua*, set_by_lua*, rewrite_by_lua*, access_by_lua*, content_by_lua*, header_filter_by_lua*, body_filter_by_lua*, log_by_lua*, ngx.timer.*, balancer_by_lua*, ssl_certificate_by_lua*, ssl_session_fetch_by_lua*, ssl_session_store_by_lua*'' +'''context:''' ''init_worker_by_lua*, set_by_lua*, rewrite_by_lua*, access_by_lua*, content_by_lua*, header_filter_by_lua*, body_filter_by_lua*, log_by_lua*, ngx.timer.*, balancer_by_lua*, ssl_certificate_by_lua*, ssl_session_fetch_by_lua*, ssl_session_store_by_lua*, exit_worker_by_lua*'' Returns the number of pending timers. @@ -7167,7 +7167,7 @@ This directive was first introduced in the v0.9.20 release. '''syntax:''' ''subsystem = ngx.config.subsystem'' -'''context:''' ''set_by_lua*, rewrite_by_lua*, access_by_lua*, content_by_lua*, header_filter_by_lua*, body_filter_by_lua*, log_by_lua*, ngx.timer.*, init_by_lua*, init_worker_by_lua*'' +'''context:''' ''set_by_lua*, rewrite_by_lua*, access_by_lua*, content_by_lua*, header_filter_by_lua*, body_filter_by_lua*, log_by_lua*, ngx.timer.*, init_by_lua*, init_worker_by_lua*, exit_worker_by_lua*'' This string field indicates the Nginx subsystem the current Lua environment is based on. For this module, this field always takes the string value "http". For [https://github.com/openresty/stream-lua-nginx-module#readme ngx_stream_lua_module], however, this field takes the value "stream". @@ -7178,7 +7178,7 @@ This field was first introduced in the 0.10.1. '''syntax:''' ''debug = ngx.config.debug'' -'''context:''' ''set_by_lua*, rewrite_by_lua*, access_by_lua*, content_by_lua*, header_filter_by_lua*, body_filter_by_lua*, log_by_lua*, ngx.timer.*, init_by_lua*, init_worker_by_lua*'' +'''context:''' ''set_by_lua*, rewrite_by_lua*, access_by_lua*, content_by_lua*, header_filter_by_lua*, body_filter_by_lua*, log_by_lua*, ngx.timer.*, init_by_lua*, init_worker_by_lua*, exit_worker_by_lua*'' This boolean field indicates whether the current Nginx is a debug build, i.e., being built by the ./configure option --with-debug. @@ -7188,7 +7188,7 @@ This field was first introduced in the 0.8.7. '''syntax:''' ''prefix = ngx.config.prefix()'' -'''context:''' ''set_by_lua*, rewrite_by_lua*, access_by_lua*, content_by_lua*, header_filter_by_lua*, body_filter_by_lua*, log_by_lua*, ngx.timer.*, init_by_lua*, init_worker_by_lua*'' +'''context:''' ''set_by_lua*, rewrite_by_lua*, access_by_lua*, content_by_lua*, header_filter_by_lua*, body_filter_by_lua*, log_by_lua*, ngx.timer.*, init_by_lua*, init_worker_by_lua*, exit_worker_by_lua*'' Returns the Nginx server "prefix" path, as determined by the -p command-line option when running the Nginx executable, or the path specified by the --prefix command-line option when building Nginx with the ./configure script. @@ -7198,7 +7198,7 @@ This function was first introduced in the 0.9.2. '''syntax:''' ''ver = ngx.config.nginx_version'' -'''context:''' ''set_by_lua*, rewrite_by_lua*, access_by_lua*, content_by_lua*, header_filter_by_lua*, body_filter_by_lua*, log_by_lua*, ngx.timer.*, init_by_lua*, init_worker_by_lua*'' +'''context:''' ''set_by_lua*, rewrite_by_lua*, access_by_lua*, content_by_lua*, header_filter_by_lua*, body_filter_by_lua*, log_by_lua*, ngx.timer.*, init_by_lua*, init_worker_by_lua*, exit_worker_by_lua*'' This field take an integral value indicating the version number of the current Nginx core being used. For example, the version number 1.4.3 results in the Lua number 1004003. @@ -7228,7 +7228,7 @@ This API was first introduced in the 0.9.3 release. '''syntax:''' ''exiting = ngx.worker.exiting()'' -'''context:''' ''set_by_lua*, rewrite_by_lua*, access_by_lua*, content_by_lua*, header_filter_by_lua*, body_filter_by_lua*, log_by_lua*, ngx.timer.*, init_by_lua*, init_worker_by_lua*'' +'''context:''' ''set_by_lua*, rewrite_by_lua*, access_by_lua*, content_by_lua*, header_filter_by_lua*, body_filter_by_lua*, log_by_lua*, ngx.timer.*, init_by_lua*, init_worker_by_lua*, exit_worker_by_lua*'' This function returns a boolean value indicating whether the current Nginx worker process already starts exiting. Nginx worker process exiting happens on Nginx server quit or configuration reload (aka HUP reload). @@ -7238,7 +7238,7 @@ This API was first introduced in the 0.9.3 release. '''syntax:''' ''pid = ngx.worker.pid()'' -'''context:''' ''set_by_lua*, rewrite_by_lua*, access_by_lua*, content_by_lua*, header_filter_by_lua*, body_filter_by_lua*, log_by_lua*, ngx.timer.*, init_by_lua*, init_worker_by_lua*'' +'''context:''' ''set_by_lua*, rewrite_by_lua*, access_by_lua*, content_by_lua*, header_filter_by_lua*, body_filter_by_lua*, log_by_lua*, ngx.timer.*, init_by_lua*, init_worker_by_lua*, exit_worker_by_lua*'' This function returns a Lua number for the process ID (PID) of the current Nginx worker process. This API is more efficient than ngx.var.pid and can be used in contexts where the [[#ngx.var.VARIABLE|ngx.var.VARIABLE]] API cannot be used (like [[#init_worker_by_lua|init_worker_by_lua]]). @@ -7248,7 +7248,7 @@ This API was first introduced in the 0.9.5 release. '''syntax:''' ''count = ngx.worker.count()'' -'''context:''' ''set_by_lua*, rewrite_by_lua*, access_by_lua*, content_by_lua*, header_filter_by_lua*, body_filter_by_lua*, log_by_lua*, ngx.timer.*, init_by_lua*, init_worker_by_lua*'' +'''context:''' ''set_by_lua*, rewrite_by_lua*, access_by_lua*, content_by_lua*, header_filter_by_lua*, body_filter_by_lua*, log_by_lua*, ngx.timer.*, init_by_lua*, init_worker_by_lua*, exit_worker_by_lua*'' Returns the total number of the Nginx worker processes (i.e., the value configured by the [https://nginx.org/en/docs/ngx_core_module.html#worker_processes worker_processes] @@ -7260,7 +7260,7 @@ This API was first introduced in the 0.9.20 release. '''syntax:''' ''id = ngx.worker.id()'' -'''context:''' ''set_by_lua*, rewrite_by_lua*, access_by_lua*, content_by_lua*, header_filter_by_lua*, body_filter_by_lua*, log_by_lua*, ngx.timer.*, init_worker_by_lua*'' +'''context:''' ''set_by_lua*, rewrite_by_lua*, access_by_lua*, content_by_lua*, header_filter_by_lua*, body_filter_by_lua*, log_by_lua*, ngx.timer.*, init_worker_by_lua*, exit_worker_by_lua*'' Returns the ordinal number of the current Nginx worker processes (starting from number 0). @@ -7350,7 +7350,7 @@ This feature requires at least ngx_lua v0.10.0. '''syntax:''' ''res = ndk.set_var.DIRECTIVE_NAME'' -'''context:''' ''init_worker_by_lua*, set_by_lua*, rewrite_by_lua*, access_by_lua*, content_by_lua*, header_filter_by_lua*, body_filter_by_lua*, log_by_lua*, ngx.timer.*, balancer_by_lua*, ssl_certificate_by_lua*, ssl_session_fetch_by_lua*, ssl_session_store_by_lua*'' +'''context:''' ''init_worker_by_lua*, set_by_lua*, rewrite_by_lua*, access_by_lua*, content_by_lua*, header_filter_by_lua*, body_filter_by_lua*, log_by_lua*, ngx.timer.*, balancer_by_lua*, ssl_certificate_by_lua*, ssl_session_fetch_by_lua*, ssl_session_store_by_lua*, exit_worker_by_lua*'' This mechanism allows calling other Nginx C modules' directives that are implemented by [https://github.com/simplresty/ngx_devel_kit Nginx Devel Kit] (NDK)'s set_var submodule's ndk_set_var_value. From b9396dc6334b36ae9321b9ff8dc4794e2d7076fb Mon Sep 17 00:00:00 2001 From: rainingmaster <312841925@qq.com> Date: Tue, 21 Jul 2020 15:05:22 +0800 Subject: [PATCH 22/22] chore: add introduce for first version of this feature --- README.markdown | 6 +++++- doc/HttpLuaModule.wiki | 6 +++++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/README.markdown b/README.markdown index e37bda90fc..bd29909b53 100644 --- a/README.markdown +++ b/README.markdown @@ -1602,12 +1602,14 @@ For example, } ``` +This directive was first introduced in the `v0.10.18` release. + [Back to TOC](#directives) exit_worker_by_lua_file ----------------------- -**syntax:** *exit_worker_by_lua_file { lua-file-path }* +**syntax:** *exit_worker_by_lua_file <path-to-lua-script-file>* **context:** *http* @@ -1615,6 +1617,8 @@ exit_worker_by_lua_file Similar to [exit_worker_by_lua_block](#exit_worker_by_lua_block), but accepts the file path to a Lua source file or Lua bytecode file. +This directive was first introduced in the `v0.10.18` release. + [Back to TOC](#directives) set_by_lua diff --git a/doc/HttpLuaModule.wiki b/doc/HttpLuaModule.wiki index e9e30efa18..04a2694972 100644 --- a/doc/HttpLuaModule.wiki +++ b/doc/HttpLuaModule.wiki @@ -1301,9 +1301,11 @@ For example, } +This directive was first introduced in the v0.10.18 release. + == exit_worker_by_lua_file == -'''syntax:''' ''exit_worker_by_lua_file { lua-file-path }'' +'''syntax:''' ''exit_worker_by_lua_file '' '''context:''' ''http'' @@ -1311,6 +1313,8 @@ For example, Similar to [[#exit_worker_by_lua_block|exit_worker_by_lua_block]], but accepts the file path to a Lua source file or Lua bytecode file. +This directive was first introduced in the v0.10.18 release. + == set_by_lua == '''syntax:''' ''set_by_lua $res [$arg1 $arg2 ...]''