Skip to content

Commit 86e1a64

Browse files
authored
feature: content_by_lua_file return 503 if read err and file is existed openresty#1992 (openresty#1995)
1 parent 653d6a3 commit 86e1a64

File tree

3 files changed

+21
-1
lines changed

3 files changed

+21
-1
lines changed

README.markdown

+2
Original file line numberDiff line numberDiff line change
@@ -1886,6 +1886,8 @@ content_by_lua_file
18861886

18871887
Equivalent to [content_by_lua_block](#content_by_lua_block), except that the file specified by `<path-to-lua-script-file>` contains the Lua code, or, as from the `v0.5.0rc32` release, the [LuaJIT bytecode](#luajit-bytecode-support) to be executed.
18881888

1889+
If the file is not found, a `404 Not Found` status code will be returned, and a `503 Service Temporarily Unavailable` status code will be returned in case of errors in reading other files.
1890+
18891891
Nginx variables can be used in the `<path-to-lua-script-file>` string to provide flexibility. This however carries some risks and is not ordinarily recommended.
18901892

18911893
When a relative path like `foo/bar.lua` is given, they will be turned into the absolute path relative to the `server prefix` path determined by the `-p PATH` command-line option while starting the Nginx server.

src/ngx_http_lua_cache.c

+7-1
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,13 @@ ngx_http_lua_cache_loadfile(ngx_log_t *log, lua_State *L,
307307
break;
308308

309309
case LUA_ERRFILE:
310-
errcode = NGX_HTTP_NOT_FOUND;
310+
if (errno == ENOENT) {
311+
errcode = NGX_HTTP_NOT_FOUND;
312+
313+
} else {
314+
errcode = NGX_HTTP_SERVICE_UNAVAILABLE;
315+
}
316+
311317
/* fall through */
312318

313319
default:

t/002-content.t

+12
Original file line numberDiff line numberDiff line change
@@ -1086,3 +1086,15 @@ GET /lua
10861086
--- error_code: 500
10871087
--- error_log
10881088
failed to load inlined Lua code: content_by_lua(...45678901234567890123456789012345.conf:14)
1089+
1090+
1091+
1092+
=== TEST 51: Lua file permission denied
1093+
--- config
1094+
location /lua {
1095+
content_by_lua_file /etc/shadow;
1096+
}
1097+
--- request
1098+
GET /lua
1099+
--- response_body_like: 503 Service Temporarily Unavailable
1100+
--- error_code: 503

0 commit comments

Comments
 (0)