【问题标题】:Using nginx upload module and nginx lua together一起使用nginx上传模块和nginx lua
【发布时间】:2019-01-06 17:13:58
【问题描述】:

我正在使用 nginx 上传模块将文件上传到服务器。但我希望 nginx 上传到不同的路径,而不是 upload_store 中提到的路径。 所以我正在使用 nginx-lua 模块来更改每个请求的 upload_store 值,如下所示。

location /umtest {
            upload_pass /nginx_response;
            set $upload_store '';
            rewrite_by_lua '
                local header = ngx.req.raw_header()
                ngx.say("type header",header)
                dst_path_dir = ngx.req.get_headers()["Dst-Dir"]
                ngx.say("dst_path_dir",dst_path_dir)
                ngx.var.upload_store = dst_path_dir
                ngx.say("upload store path" ,ngx.var.upload_store)    
             ';
                upload_set_form_field $upload_field_name.name 
               "$upload_file_name";
                upload_set_form_field $upload_field_name.content_type 
                "$upload_content_type";
                upload_set_form_field $upload_field_name.path 
                "$upload_tmp_path"
                upload_cleanup 400 404 499 500-505;
               }

现在,当我 POST 到“/umtest”时,它会更改 upload_store 的值,但不会执行 nginx 上传指令(即不会发生上传)。当我评论 rewrite_by_lua 指令时,就会发生上传。 我的问题是我们不能同时使用两者来达到目的吗?

【问题讨论】:

  • 你试过删除所有ngx.say 行吗?

标签: nginx file-upload lua upload


【解决方案1】:

由于您已经在使用 Lua,一个明显的解决方法是使用它来读取请求正文并将其保存为文件:

-- handle_download.lua
ngx.req.read_body()
local body = ngx.req.get_body_data()
local file = io.open("some_file", 'wb')
file:write(body)
file:close()
location /umtest {
   content_by_lua_file 'handle_download.lua';
}

这只是将整个请求正文转储到一个文件中。如果要上传的文件作为 HTML 表单的一部分发送,您也可以 ngx.req.get_post_args(),或者您甚至可以使用 ngx.req.socket() 并自己将数据作为流读取(对于非常大的文件很有用)

【讨论】:

    猜你喜欢
    • 2015-11-07
    • 2017-05-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-31
    • 1970-01-01
    • 1970-01-01
    • 2018-05-02
    • 1970-01-01
    相关资源
    最近更新 更多