2017-06-29 580 views
0

我想用nginx來獲取post請求內容,數據enctype是multipart/form-data。我用下面的配置,nginx的配置:如何用nginx獲取post請求內容,數據enctype是multipart/form-data

upstream syy{ 
    server 201.23.xx.xx; 
    } 


server { 
    listen  443; 
    server_name localhost; 



lua_need_request_body on; 

    location /test { 
     root html; 
     index index.html index.htm; 
     proxy_pass http://syy; 
    proxy_buffering off; 
    lua_code_cache off; 
    content_by_lua_file /usr/local/openresty/luasrc/att.lua; 
    } 

LUA文件:

function writefile(filename,info) 
local file = assert(io.open(filename,"ab")) 
file:write(info) 
file:close() 
end 

ngx.req.read_body() 
local value = "/home/tmp/"..os.date("%Y%m%d%H")..".att" 
local data = ngx.req.get_post_args() 
if data ~= nil then 
    if type(data) == "table" then 
     writefile(value,table.concat(data,",").."\n") 
    else 
     writefile(value,data.."\n") 
    end 
end 

但是,當我訪問測試網站,我只得到了一個空文件,像這樣的login.jsp,這是一個空的文件。瀏覽器會提示我下載login.jsp。

THX非常感謝!

回答

0

所有「proxy_pass」和「content_by_lua_file」首先不應該一起使用。您可以使用proxy_pass或content_by_lua。這就是瀏覽器試圖下載文件而不是實際執行文件的原因。

不完全確定你要達到的目標,但是你可能想使用「rewrite_by_lua_file」而不是「content_by_lua_file」。在此階段,您可以進行一些操作,然後再反向代理您的請求。您也可以使用「access_by_lua *」作爲您使用的代碼片段,但這一切取決於您要使用的nginx功能。

rewrite_by_lua_file /usr/local/openresty/luasrc/att.lua; 
+0

thx非常多,「access_by_lua」是工作。我想記錄附件數據流,用戶上傳到Web應用程序並使用apached commons-fileupload組件。 –

相關問題