使用lua-resty-http请求模块代理传入的上传文件请求

我正在尝试使用lua-resty-http模块来代理请求。目前,我已经传递了http请求的标头、主体、请求方法和url,它可以正常工作。但是,当上传文件的post请求出现时,它无法这样做,很明显我还没有配置它来这样做。因此,我想代理那种类型的请求。这是我的代码片段,我应该做出什么改变,以便它提取文件上传数据,并使用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
})
点赞
用户2060502
用户2060502

阅读文档!

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

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

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

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

2021-03-09 06:38:04