post請求體過大導致ngx.req.get_post_args()取不到參數體的問題


http://nginx.org/en/docs/http/ngx_http_core_module.html#client_body_buffer_size

該地址對於client_body_buffer_size配置做了說明

syntax: client_body_buffer_size size

default: client_body_buffer_size 8k|16k

context: http,server,location

Sets buffer size for reading client request body. In case the request body is larger than the buffer, the whole body or only its part is written to a temporary file. By default, buffer size is equal to two memory pages. This is 8K on x86, other 32-bit platforms, and x86-64. It is usually 16K on other 64-bit platforms.

為讀取客戶端請求體設置緩存區大小。為了防止請求體超過緩存區大小,將整個請求體或其中一分部寫入一個臨時文件。默認緩存區大小為two memory pages。在x86、32位機器、x86-64上是8k,其它64位機器上是16k。

解決方案有兩種:

1、調大client_body_buffer_size值

client_body_buffer_size 128k

client_max_body_size 100m

這兩個參數配合使用,如果總body_size超過這個值,請求會被拒絕

2、去臨時文件讀取請求體

需要用到ngx.req.get_body_file,從臨時文件讀取數據

--Nginx服務器中使用lua獲取get或post參數
local request_method = ngx.var.request_method
local args = nil
local param = nil
local param2 = nil
--獲取參數的值
if "GET" == request_method then
    args = ngx.req.get_uri_args()
elseif "POST" == request_method then
    ngx.req.read_body()
    args = ngx.req.get_post_args()
end
param = args["param"]
param2 = args["param2"]
--升級版(能處理content-type=multipart/form-data的表單):
local function explode ( _str,seperator )
    local pos, arr = 0, {}
        for st, sp in function() return string.find( _str, seperator, pos, true ) end do
            table.insert( arr, string.sub( _str, pos, st-1 ) )
            pos = sp + 1
        end
    table.insert( arr, string.sub( _str, pos ) )
    return arr
end
local args = {}
local file_args = {}
local is_have_file_param = false
local function init_form_args()
    local receive_headers = ngx.req.get_headers()
    local request_method = ngx.var.request_method
    if "GET" == request_method then
        args = ngx.req.get_uri_args()
    elseif "POST" == request_method then
        ngx.req.read_body()
            --判斷是否是multipart/form-data類型的表單
        if string.sub(receive_headers["content-type"],1,20) == "multipart/form-data;" then   
            is_have_file_param = true
            content_type = receive_headers["content-type"]
            --body_data可是符合http協議的請求體,不是普通的字符串
            body_data = ngx.req.get_body_data()
            --請求體的size大於nginx配置里的client_body_buffer_size,則會導致請求體被緩沖到磁盤臨時文件里,client_body_buffer_size默認是8k或者16k
            if not body_data then
                local datafile = ngx.req.get_body_file()
                if not datafile then
                    error_code = 1
                    error_msg = "no request body found"
                else
                    local fh, err = io.open(datafile, "r")
                    if not fh then
                        error_code = 2
                        error_msg = "failed to open " .. tostring(datafile) .. "for reading: " .. tostring(err)
                    else
                        fh:seek("set")
                        body_data = fh:read("*a")
                        fh:close()
                        if body_data == "" then
                            error_code = 3
                            error_msg = "request body is empty"
                        end
                    end
                end
            end
            local new_body_data = {}
            --確保取到請求體的數據
            if not error_code then
                local boundary = "--" .. string.sub(receive_headers["content-type"],31)
                local body_data_table = explode(tostring(body_data),boundary)
                local first_string = table.remove(body_data_table,1)
                local last_string = table.remove(body_data_table)
                for i,v in ipairs(body_data_table) do
                    local start_pos,end_pos,capture,capture2 = string.find(v,'Content%-Disposition: form%-data; name="(.+)"; filename="(.*)"')
                    --普通參數
                    if not start_pos then
                        local t = explode(v,"rnrn")
                        local temp_param_name = string.sub(t[1],41,-2)
                        local temp_param_value = string.sub(t[2],1,-3)
                        args[temp_param_name] = temp_param_value
                    else
                    --文件類型的參數,capture是參數名稱,capture2是文件名                            
                        file_args[capture] = capture2
                        table.insert(new_body_data,v)
                    end
                end
                table.insert(new_body_data,1,first_string)
                table.insert(new_body_data,last_string)
                --去掉app_key,app_secret等幾個參數,把業務級別的參數傳給內部的API
                body_data = table.concat(new_body_data,boundary)--body_data可是符合http協議的請求體,不是普通的字符串
            end
        else
            args = ngx.req.get_post_args()
        end
    end
end

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM