test_read_body.lua:
ngx.req.read_body()
local req_body = ngx.req.get_body_data()
if req_body then
ngx.say("get " .. #req_body .. " bytes request body from memory")
else
local path = ngx.req.get_body_file()
if path ~= nil then
local f = assert(io.open(path, "rb"))
req_body = f:read("*all")
ngx.say("get " .. #req_body .. " bytes request body from file " .. path)
else
ngx.say("get no request body")
end
end
nginx.conf:
location /test_read_body {
client_max_body_size 100m;
content_by_lua_file "conf/test_read_body.lua";
}
当body不超过client_body_buffer_size的时候,是正常的。
当body超过client_body_buffer_size,未超过client_max_body_size时,会将body放到临时文件,ngx.req.get_body_file()返回路径,但从这个路径里读时,返回错误,找不到文件
curl -H "Expect: " -X PUT --data-binary "@some_large_file" "http://127.0.0.1/test_read_body"
2017/01/05 11:31:55 [error] 19686#0: *2 lua entry thread aborted: runtime error: /usr/local/nginx/conf/test_read_body.lua:9: /usr/local/nginx/client_body_temp/0000000002: No such file or directory
stack traceback:
coroutine 0:
[C]: in function 'assert'
/usr/local/nginx/conf/test_read_body.lua:9: in function </usr/local/nginx/conf/test_read_body.lua:1>, client: 127.0.0.1, server: localhost, request: "PUT /test_read_body HTTP/1.1", host: "127.0.0.1"
content_by_lua*和access_by_lua*都不行。
是什么原因呢?