Nginx Series | (rpm) Nginx upload files: client_max_body_size, client_body_buffer_size

Original: http://php-note.com/article/detail/488

client_max_body_size

client_max_body_size default 1M, client requests the server represents the maximum allowed size specified in the header "Content-Length" request. If the body is greater than the requested data client_max_body_size, HTTP protocol will be given 413 Request Entity Too Large. That is, if the request is greater than the body client_max_body_size, must be a failure. If you need to upload large files, be sure to modify the value.

client_body_buffer_size

Buffer Nginx allocated data size of the request, if the requested data is less than client_body_buffer_size data previously stored in the memory directly. If the requested value is greater than client_body_buffer_size less than client_max_body_size, it will be the first data stored in a temporary file, in which the temporary file it?

client_body_temp specified path, the default value is the path / tmp /.

So configured client_body_temp address, will allow groups of users to perform Nginx has read and write permissions. Otherwise, when data transmission is greater than client_body_buffer_size, written into the temporary file failed error.

The problems we encountered.

20648 open() "/usr/local/openresty-1.9.7.5/nginx/client_body_temp/0000000019" failed (13: Permission denied)

/usr/local/openresty-1.9.7.5/nginx/client_body_temp/ this folder permissions changed to perform Nginx user group can be resolved.

On this issue and on related languages, and if you are using PHP, PHP will own the temporary file will read out the requested data to be placed inside, this is no problem, developers do not need to care about. Certainly complete data.

If openresty lua-use development, then we need to develop themselves read out so that the subsequent use of logic.

function getFile(file_name)
    local f = assert(io.open(file_name, 'r'))
    local string = f:read("*all")
    f:close()
    return string
end
 
ngx.req.read_body()
local data = ngx.req.get_body_data()
if nil == data then
    local file_name = ngx.req.get_body_file()
    ngx.say(">> temp file: ", file_name)
    if file_name then
        data = getFile(file_name)
    end
end
 
ngx.say("hello ", data)

to sum up

  • Data transmission is greater than client_max_body_size, it must pass unsuccessful.
  • Less than client_body_buffer_size efficiently stored directly in memory.
  • 如果大于 client_body_buffer_size 小于 client_max_body_size 会存储临时文件,临时文件一定要有权限。
  • 如果追求效率,就设置 client_max_body_size 和 client_body_buffer_size 相同的值,这样就不会存储临时文件,直接存储在内存了。

Guess you like

Origin www.cnblogs.com/tinywan/p/11468397.html