如何使用 Nginx 获取 POST 请求内容,数据的 enctype 是 multipart/form-data

我想使用 Nginx 获取 POST 请求的内容,数据的 enctype 是 multipart/form-data。 我使用了以下配置,Nginx 配置:

 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。

非常感谢!

点赞
用户4163842
用户4163842

首先,"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;
2017-07-03 22:33:26