On Tuesday, September 30, 2014 5:56:35 PM UTC+3, James Hurst wrote:
Hi,
On 30 September 2014 15:49,
<david...@gmail.com> wrote:
When using lua-resty-http (
https://github.com/pintsized/lua-resty-http) and sending POST requests I get a timeout no matter how long I set the timeout in the lua client (e.g httpc:set_timeout(10000)). The POST request is pretty small (Content-Length: 150), any idea what could be causing the timeout? When I send the same request directly to the backend server everything works normally. When I go through the reverse proxy I get the following in the logs:
[error] 27253#0: *139 lua tcp socket read timed out, client
How are you sending the request body, as a string or using the client body reader? Can you post a gist of your code? Do you have any error on the backend server?
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