Hi James,
The code is below. I don't have access to the backend server logs, but the same request works when I send it directly to the backend server (and update the target IP and Host header). Also note that some POST requests work while others do not and result in the timeout error despite extremely large values set in httpc:set_timeout().
-- This is a generic function that grabs the request args (for both GET and POST requests) and returns them in a string
function GetParams()
local args, err = ngx.req.get_uri_args()
local pargs, err = ngx.req.get_post_args()
for k,v in pairs(pargs) do args[k] = v end
local parms
if not args then
ngx.log(ngx.ERR, "failed to get post args: ", err)
return
end
for key, val in pairs(args) do
if type(val) == "table" then
ngx.log(ngx.ERR, key, ": ", table.concat(val, ", "))
if parms == nil then
parms = key .. "=" .. val
else
parms = parms .. "&" .. key .. "=" .. val
end
else
ngx.log(ngx.ERR, key, ": ", val)
if parms == nil then
parms = key .. "=" .. val
else
parms = parms .. "&" .. key .. "=" .. val
end
end
end
return parms
end
-- This function is called when proxying POST requests
function ProxyPOSTRequest(Host, URI, Params, Headers)
local http = require "resty.http"
local httpc =
http.new()
httpc:set_timeout(5000)
local ok, err = httpc:connect(Host, 80)
local res, err = httpc:request{
method = "POST",
headers = Headers,
path = URI,
body = Params,
}
for k,v in pairs(res.headers) do
ngx.header[k] = v
--ngx.say(k, ": ", v)
ngx.log(ngx.ERR, "Headers: ", k, " ", v)
end
local body = res:read_body()
ngx.say(body)
local ok, err = httpc:set_keepalive()
if not ok then
ngx.say("failed to set keepalive: ", err)
return
end
end
thanks!
- D