【问题标题】:Proxy an incoming upload file post request using lua-resty-http request module使用 lua-resty-http 请求模块代理传入的上传文件发布请求
【发布时间】:2021-06-06 06:47:23
【问题描述】:

我正在尝试使用 lua-resty-http 模块代理请求。我目前正在为 http 请求传递标头、正文、请求方法和 url,这些都可以正常工作。但是当文件上传发布请求来时它无法这样做,显然我还没有配置它这样做。所以我也想要代理这种类型的请求。这是我到目前为止的代码的快照,我应该进行哪些更改以便提取文件上传数据并使用 lua-resty-http 模块发送它->

    local http = require "resty.http"
    local cjson = require "cjson"
    local httpc = http.new()
    local path = ngx.var.request_uri

    local passHeader = {["cookie"]=ngx.req.get_headers()["cookie"]}
    passHeader["content-type"] = ngx.req.get_headers()["content-type"]

    ngx.req.read_body();
    body = ngx.req.get_body_data();
    
    local original_req_uri = "https://"  .. "fakehost.com" .. path
    
    local req_method = ngx.req.get_method()

    local res, err = httpc:request_uri(original_req_uri, {
      method = req_method,
      ssl_verify = false,
      keepalive_timeout = 60000,
      headers = passHeader,
      keepalive_pool = 10,
      body = body
    })

【问题讨论】:

    标签: file-upload lua proxy http-post openresty


    【解决方案1】:

    阅读文档!

    https://github.com/openresty/lua-nginx-module#ngxreqget_body_data

    POST 请求可能包含大的正文,nginx 可能会将其写入磁盘文件。

    如果请求正文已读入磁盘文件,请尝试调用 ngx.req.get_body_file 函数。

    PS:IMO 通过 Lua 代理 HTTP 请求的方法不是最佳的,因为它是完全缓冲的方式。对我来说,只有当我们需要发出子请求时才有意义。对于处理请求最多的主路径,我建议使用 proxy_pass。

    【讨论】:

      猜你喜欢
      • 2015-09-28
      • 1970-01-01
      • 2013-03-15
      • 2018-07-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-20
      • 1970-01-01
      相关资源
      最近更新 更多