nginxluaopenresty

body_file doesnt exist even tho get_body_file returns a filename


I want to make nginx handle large request by dumping them into a file and instead set a header so my backend app can read the file instead. However, whatever I try, the file is gone before I can do anything with it:

        location ~ ^/upload $ {
            client_body_in_file_only on;

            content_by_lua_block {
                ngx.req.read_body()
                local body_file = ngx.req.get_body_file()
                ngx.req.set_header("X-File-Path", body_file)
                ngx.req.set_body_data("")
                return ngx.exec("@proxy_target")
            }
        }
        location @proxy_target {
            proxy_pass http://127.0.0.1:8000$request_uri;
        }

The code is pretty straight forward and body_file is defined. However, the file itself doesnt exist. When I dont read the file via read_body it does exist but then I cant get the name of the file because it returns nil. If i DO read the file, I can get its name but its gone. Grrr

I first tried using nginx without lua. However, it is impossible to remove the request body without getting malformed request errors in my backend app. If someone knows a solution via nginx alone that obviously would be much preferred

Any pointers are welcome!

// EDIT: Opened a bug report since this seems inconsistent behavior: https://github.com/openresty/lua-nginx-module/issues/2405


Solution

  • The description of ngx.req.set_body_data in the reference says:

    When the current request's request body has been read into memory or buffered into a disk file, then the old request body's memory will be freed or the disk file will be cleaned up immediately, respectively.

    I think this is the reason why the body file is gone.