1

I am trying to proxy requests using lua-resty-http module. I am currently passing headers, body, request method, and url for http request which works fine. But when a file upload post request comes it is unable to do so, obviously i haven't configured it to do so. So I want proxy that type of request also. Here is a snap of my so far code, what changes should i make so that it extract file upload data and sends it using lua-resty-http module ->

    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
    })

mikasa9002
  • 11
  • 1

1 Answers1

0

Read the docs!

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

POST request may contain big body and nginx may write it to a disk file.

If the request body has been read into disk files, try calling the ngx.req.get_body_file function instead.

PS: IMO approach to proxy HTTP request by Lua is not optimal, because it is the full buffered way. For me it make sense only if we need to issue a subrequest(s). For the main path with most requests processed I recommend to use proxy_pass.

Alexander Altshuler
  • 2,930
  • 1
  • 17
  • 27