ngx-lua POST request body limit?

645 views
Skip to first unread message

Colin MacRae

unread,
May 30, 2018, 10:11:01 AM5/30/18
to openresty-en
Quick summary: I have a webpage with a single text area form input and submit button. When a user hits submit, the form is sent as a POST, with data in the request body. This string is then parsed, and submitted as a key-value pair to the redis cache backend. This is to store from-to URL redirects. This works fine with 50 lines, (100 URLs), but anything more than 100 lines (200 URLs) there is no data sent, and I am seeing a nil value.

The numbers I've stated I'm sure are arbitrary, but is this a known limit? I don't see anything in the logs from nginx regarding body_size too large, and I have tried various ways of increasing this apparent limit without any change in behaviour. The strannge thing is I can't find any errors.

I'm using this:

ngx.req.read_body()
local data = ngx.req.get_body_data()
if data ~= nil then
 
-- do something
here
else
   ngx.say("Body too large...for some reason.")
end
And seeing the "Body too large..." issue.

Any insight into this would be greatly appreciated.

Rgds,
Colin

Jim Robinson

unread,
May 30, 2018, 11:05:05 AM5/30/18
to openresty-en
Check if it's in the body file, which is used when nginx decided it will be more efficient to use a temp file. As an example:

-- if the body is in memory record it as request.body,
-- otherwise if it is stored in a file record it as
-- request.body_file.
ngx.req.read_body()
request.body = ngx.req.get_body_data()
if not request.body then
        local file = ngx.req.get_body_file()
        if file then
                request.body_file = file
        end
end

...

-- if a body_file exists, ready it into memory
if request.body_file then
        local fh, err = io.open(request.body_file, "rb")
        if err then
                ngx.status = ngx.HTTP_INTERNAL_SERVER_ERROR
                ngx.log(ngx.ERR, "error reading request.body_file:", err)
                return
        end

        request.body = fh:read("*all")
        fh:close()
end

Colin MacRae

unread,
May 30, 2018, 12:00:06 PM5/30/18
to openresty-en
I had no idea nginx did that! Jim, you are my hero! Thanks!

I had been scratching my head over this for some time

Colin
Reply all
Reply to author
Forward
0 new messages