diff --git a/src/ngx_http_lua_misc.c b/src/ngx_http_lua_misc.c index b12405f364..4990e26f71 100644 --- a/src/ngx_http_lua_misc.c +++ b/src/ngx_http_lua_misc.c @@ -44,12 +44,14 @@ ngx_http_lua_ngx_get(lua_State *L) r = ngx_http_lua_get_req(L); if (r == NULL) { - return luaL_error(L, "no request object found"); + lua_pushnil(L); + return 1; } ctx = ngx_http_get_module_ctx(r, ngx_http_lua_module); if (ctx == NULL) { - return luaL_error(L, "no request ctx found"); + lua_pushnil(L); + return 1; } p = (u_char *) luaL_checklstring(L, -1, &len); diff --git a/t/086-init-by.t b/t/086-init-by.t index c77f7f9496..f9ed12cb28 100644 --- a/t/086-init-by.t +++ b/t/086-init-by.t @@ -9,7 +9,7 @@ use Test::Nginx::Socket::Lua; repeat_each(2); -plan tests => repeat_each() * (blocks() * 3 + 2); +plan tests => repeat_each() * (blocks() * 3 + 3); #no_diff(); #no_long_string(); @@ -278,3 +278,30 @@ baz = 78 [error] Failed to resume our co: + + +=== TEST 11: access a field in the ngx. table +--- http_config + init_by_lua ' + print("INIT 1: foo = ", ngx.foo) + ngx.foo = 3 + print("INIT 2: foo = ", ngx.foo) + '; +--- config + location /t { + echo ok; + } +--- request +GET /t +--- response_body +ok +--- no_error_log +[error] +--- grep_error_log eval: qr/INIT \d+: foo = \S+/ +--- grep_error_log_out eval +[ +"INIT 1: foo = nil +INIT 2: foo = 3 +", +"", +]