Skip to content

Commit 057b916

Browse files
committed
bugfix: inlined Lua code snippets in nginx.conf failed to use the Lua source checksum as part of the Lua code cache key. thanks Oleg A. Mamontov for the report in #1428.
Fix #1428. optimize: now we exclude the current nginx config file info from the cache keys for inlined Lua code snippets in nginx config files.
1 parent f627f4c commit 057b916

File tree

3 files changed

+144
-18
lines changed

3 files changed

+144
-18
lines changed

src/ngx_http_lua_cache.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,8 @@ ngx_http_lua_cache_loadbuffer(ngx_log_t *log, lua_State *L,
156156

157157
n = lua_gettop(L);
158158

159-
dd("XXX cache key: [%s]", cache_key);
159+
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, log, 0,
160+
"looking up Lua code cache with key '%s'", cache_key);
160161

161162
rc = ngx_http_lua_cache_load_code(log, L, (char *) cache_key);
162163
if (rc == NGX_OK) {
@@ -240,7 +241,8 @@ ngx_http_lua_cache_loadfile(ngx_log_t *log, lua_State *L,
240241
dd("CACHE file key already pre-calculated");
241242
}
242243

243-
dd("XXX cache key for file: [%s]", cache_key);
244+
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, log, 0,
245+
"looking up Lua code cache with key '%s'", cache_key);
244246

245247
rc = ngx_http_lua_cache_load_code(log, L, (char *) cache_key);
246248
if (rc == NGX_OK) {

src/ngx_http_lua_directive.c

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -497,15 +497,13 @@ ngx_http_lua_rewrite_by_lua(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
497497

498498
llcf->rewrite_src.value = value[1];
499499

500-
p = ngx_palloc(cf->pool,
501-
chunkname_len + NGX_HTTP_LUA_INLINE_KEY_LEN + 1);
500+
p = ngx_palloc(cf->pool, NGX_HTTP_LUA_INLINE_KEY_LEN + 1);
502501
if (p == NULL) {
503502
return NGX_CONF_ERROR;
504503
}
505504

506505
llcf->rewrite_src_key = p;
507506

508-
p = ngx_copy(p, chunkname, chunkname_len);
509507
p = ngx_copy(p, NGX_HTTP_LUA_INLINE_TAG, NGX_HTTP_LUA_INLINE_TAG_LEN);
510508
p = ngx_http_lua_digest_hex(p, value[1].data, value[1].len);
511509
*p = '\0';
@@ -611,15 +609,13 @@ ngx_http_lua_access_by_lua(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
611609

612610
llcf->access_src.value = value[1];
613611

614-
p = ngx_palloc(cf->pool,
615-
chunkname_len + NGX_HTTP_LUA_INLINE_KEY_LEN + 1);
612+
p = ngx_palloc(cf->pool, NGX_HTTP_LUA_INLINE_KEY_LEN + 1);
616613
if (p == NULL) {
617614
return NGX_CONF_ERROR;
618615
}
619616

620617
llcf->access_src_key = p;
621618

622-
p = ngx_copy(p, chunkname, chunkname_len);
623619
p = ngx_copy(p, NGX_HTTP_LUA_INLINE_TAG, NGX_HTTP_LUA_INLINE_TAG_LEN);
624620
p = ngx_http_lua_digest_hex(p, value[1].data, value[1].len);
625621
*p = '\0';
@@ -725,25 +721,25 @@ ngx_http_lua_content_by_lua(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
725721

726722
llcf->content_chunkname = chunkname;
727723

728-
dd("chunkname: %s", chunkname);
724+
dd("chunkname: %s (len=%d)", chunkname, (int) chunkname_len);
729725

730726
/* Don't eval nginx variables for inline lua code */
731727

732728
llcf->content_src.value = value[1];
733729

734-
p = ngx_palloc(cf->pool,
735-
chunkname_len + NGX_HTTP_LUA_INLINE_KEY_LEN + 1);
730+
p = ngx_palloc(cf->pool, NGX_HTTP_LUA_INLINE_KEY_LEN + 1);
736731
if (p == NULL) {
737732
return NGX_CONF_ERROR;
738733
}
739734

740735
llcf->content_src_key = p;
741736

742-
p = ngx_copy(p, chunkname, chunkname_len);
743737
p = ngx_copy(p, NGX_HTTP_LUA_INLINE_TAG, NGX_HTTP_LUA_INLINE_TAG_LEN);
744738
p = ngx_http_lua_digest_hex(p, value[1].data, value[1].len);
745739
*p = '\0';
746740

741+
dd("src key: %s\n", llcf->content_src_key);
742+
747743
} else {
748744
ngx_memzero(&ccv, sizeof(ngx_http_compile_complex_value_t));
749745
ccv.cf = cf;
@@ -852,15 +848,13 @@ ngx_http_lua_log_by_lua(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
852848

853849
llcf->log_src.value = value[1];
854850

855-
p = ngx_palloc(cf->pool,
856-
chunkname_len + NGX_HTTP_LUA_INLINE_KEY_LEN + 1);
851+
p = ngx_palloc(cf->pool, NGX_HTTP_LUA_INLINE_KEY_LEN + 1);
857852
if (p == NULL) {
858853
return NGX_CONF_ERROR;
859854
}
860855

861856
llcf->log_src_key = p;
862857

863-
p = ngx_copy(p, chunkname, chunkname_len);
864858
p = ngx_copy(p, NGX_HTTP_LUA_INLINE_TAG, NGX_HTTP_LUA_INLINE_TAG_LEN);
865859
p = ngx_http_lua_digest_hex(p, value[1].data, value[1].len);
866860
*p = '\0';
@@ -1304,11 +1298,11 @@ ngx_http_lua_gen_chunk_name(ngx_conf_t *cf, const char *tag, size_t tag_len,
13041298

13051299
found:
13061300

1307-
ngx_snprintf(out, len, "=%*s(%*s:%d)%Z",
1301+
p = ngx_snprintf(out, len, "=%*s(%*s:%d)%Z",
13081302
tag_len, tag, cf->conf_file->file.name.data
13091303
+ cf->conf_file->file.name.len - p,
13101304
p, cf->conf_file->line);
1311-
*chunkname_len = len;
1305+
*chunkname_len = p - out - 1; /* exclude the trailing '\0' byte */
13121306

13131307
return out;
13141308
}

t/025-codecache.t

Lines changed: 131 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use Test::Nginx::Socket::Lua;
44

55
repeat_each(2);
66

7-
plan tests => repeat_each() * 155;
7+
plan tests => repeat_each() * 163;
88

99
#$ENV{LUA_PATH} = $ENV{HOME} . '/work/JSON4Lua-0.9.30/json/?.lua';
1010

@@ -1244,3 +1244,133 @@ qr/\[alert\] \S+ lua_code_cache is off; this will hurt performance/,
12441244
"decrementing the reference count for Lua VM: 1",
12451245
"lua close the global Lua VM",
12461246
]
1247+
1248+
1249+
1250+
=== TEST 32: make sure inline code keys are correct
1251+
GitHub issue #1428
1252+
--- config
1253+
include ../html/a/proxy.conf;
1254+
include ../html/b/proxy.conf;
1255+
include ../html/c/proxy.conf;
1256+
1257+
location /t {
1258+
echo_location /a/;
1259+
echo_location /b/;
1260+
echo_location /a/;
1261+
echo_location /c/;
1262+
}
1263+
1264+
--- user_files
1265+
>>> a/proxy.conf
1266+
location /a/ {
1267+
content_by_lua_block { ngx.say("/a/ is called") }
1268+
}
1269+
1270+
>>> b/proxy.conf
1271+
location /b/ {
1272+
content_by_lua_block { ngx.say("/b/ is called") }
1273+
}
1274+
1275+
>>> c/proxy.conf
1276+
location /c/ {
1277+
content_by_lua_block { ngx.say("/b/ is called") }
1278+
}
1279+
1280+
--- request
1281+
GET /t
1282+
--- response_body
1283+
/a/ is called
1284+
/b/ is called
1285+
/a/ is called
1286+
/b/ is called
1287+
--- grep_error_log eval: qr/looking up Lua code cache with key '.*?'/
1288+
--- grep_error_log_out eval
1289+
[
1290+
"looking up Lua code cache with key 'nhli_3c7137b8371d10bc148c8f8bb3042ee6'
1291+
looking up Lua code cache with key 'nhli_1dfe09105792ef65c8d576cc486d5e04'
1292+
looking up Lua code cache with key 'nhli_3c7137b8371d10bc148c8f8bb3042ee6'
1293+
looking up Lua code cache with key 'nhli_1dfe09105792ef65c8d576cc486d5e04'
1294+
",
1295+
"looking up Lua code cache with key 'nhli_3c7137b8371d10bc148c8f8bb3042ee6'
1296+
looking up Lua code cache with key 'nhli_1dfe09105792ef65c8d576cc486d5e04'
1297+
looking up Lua code cache with key 'nhli_3c7137b8371d10bc148c8f8bb3042ee6'
1298+
looking up Lua code cache with key 'nhli_1dfe09105792ef65c8d576cc486d5e04'
1299+
looking up Lua code cache with key 'nhli_3c7137b8371d10bc148c8f8bb3042ee6'
1300+
looking up Lua code cache with key 'nhli_1dfe09105792ef65c8d576cc486d5e04'
1301+
looking up Lua code cache with key 'nhli_3c7137b8371d10bc148c8f8bb3042ee6'
1302+
looking up Lua code cache with key 'nhli_1dfe09105792ef65c8d576cc486d5e04'
1303+
"]
1304+
--- log_level: debug
1305+
--- no_error_log
1306+
[error]
1307+
1308+
1309+
1310+
=== TEST 33: make sure Lua code file keys are correct
1311+
GitHub issue #1428
1312+
--- config
1313+
include ../html/a/proxy.conf;
1314+
include ../html/b/proxy.conf;
1315+
include ../html/c/proxy.conf;
1316+
1317+
location /t {
1318+
echo_location /a/;
1319+
echo_location /b/;
1320+
echo_location /a/;
1321+
echo_location /c/;
1322+
}
1323+
1324+
--- user_files
1325+
>>> a.lua
1326+
ngx.say("/a/ is called")
1327+
1328+
>>> b.lua
1329+
ngx.say("/b/ is called")
1330+
1331+
>>> c.lua
1332+
ngx.say("/b/ is called")
1333+
1334+
>>> a/proxy.conf
1335+
location /a/ {
1336+
content_by_lua_file html/a.lua;
1337+
}
1338+
1339+
>>> b/proxy.conf
1340+
location /b/ {
1341+
content_by_lua_file html/b.lua;
1342+
}
1343+
1344+
>>> c/proxy.conf
1345+
location /c/ {
1346+
content_by_lua_file html/c.lua;
1347+
}
1348+
1349+
--- request
1350+
GET /t
1351+
--- response_body
1352+
/a/ is called
1353+
/b/ is called
1354+
/a/ is called
1355+
/b/ is called
1356+
--- grep_error_log eval: qr/looking up Lua code cache with key '.*?'/
1357+
--- grep_error_log_out eval
1358+
[
1359+
"looking up Lua code cache with key 'nhlf_48a9a7def61143c003a7de1644e026e4'
1360+
looking up Lua code cache with key 'nhlf_68f5f4e946c3efd1cc206452b807e8b6'
1361+
looking up Lua code cache with key 'nhlf_48a9a7def61143c003a7de1644e026e4'
1362+
looking up Lua code cache with key 'nhlf_042c9b3a136fbacbbd0e4b9ad10896b7'
1363+
",
1364+
"looking up Lua code cache with key 'nhlf_48a9a7def61143c003a7de1644e026e4'
1365+
looking up Lua code cache with key 'nhlf_68f5f4e946c3efd1cc206452b807e8b6'
1366+
looking up Lua code cache with key 'nhlf_48a9a7def61143c003a7de1644e026e4'
1367+
looking up Lua code cache with key 'nhlf_042c9b3a136fbacbbd0e4b9ad10896b7'
1368+
looking up Lua code cache with key 'nhlf_48a9a7def61143c003a7de1644e026e4'
1369+
looking up Lua code cache with key 'nhlf_68f5f4e946c3efd1cc206452b807e8b6'
1370+
looking up Lua code cache with key 'nhlf_48a9a7def61143c003a7de1644e026e4'
1371+
looking up Lua code cache with key 'nhlf_042c9b3a136fbacbbd0e4b9ad10896b7'
1372+
"
1373+
]
1374+
--- log_level: debug
1375+
--- no_error_log
1376+
[error]

0 commit comments

Comments
 (0)