lua-resty-http
lua-resty-http copied to clipboard
keepalive send wrong http method after many requests in concurrency scenario
local http = require "resty.http"
local qrbody = {
method = ngx.var.request_method,
query = ngx.req.get_uri_args(),
body = nil,
path = ngx.var.uri,
headers = ngx.req.get_headers(),
keepalive_timeout = 60000,
keepalive_pool = 20,
keepalive = true
}
if qrbody.method == "POST" then
ngx.req.read_body()
qrbody.body = ngx.req.get_body_data()
end
local httpc = http:new()
httpc:set_timeouts(30000, 180000, 300000)
local res, err = httpc:request_uri(passUrl, qrbody)
and backend receive a wrong method: "ET/OST"
web.HttpRequestMethodNotSupportedException: Request method 'ET' not supported
...
web.HttpRequestMethodNotSupportedException: Request method 'OST' not supported
If you call http.debug(true)
before running the request, you will see the formatted request in your log file. First confirm that this looks correct (i.e. the method appears correct). If it is truncated, perhaps your nginx variable has been changed elsewhere? Consider ngx.req.get_method()
instead?
Also note from your snippet, qrbody
is a global variable (meaning you will likely see confusing and inconsistent results with concurrent requests) and you are missing a then
keyword. That is, if you need help with an issue, please post a working example config that others can use to replicate your issue.
If you call
http.debug(true)
before running the request, you will see the formatted request in your log file. First confirm that this looks correct (i.e. the method appears correct). If it is truncated, perhaps your nginx variable has been changed elsewhere? Considerngx.req.get_method()
instead?Also note from your snippet,
qrbody
is a global variable (meaning you will likely see confusing and inconsistent results with concurrent requests) and you are missing athen
keyword. That is, if you need help with an issue, please post a working example config that others can use to replicate your issue.
Thanks for the reply.
sorry about that, qcbody
is local variable, just writing too fast to miss it.
I will try http.debug(true)
and ngx.req.get_method()
first.
in addition, same script, if i disable keepalive, there will be no such issue, as below
local qrbody = {
method = ngx.var.request_method,
query = ngx.req.get_uri_args(),
body = nil,
path = ngx.var.uri,
headers = ngx.req.get_headers(),
--keepalive_timeout = 60000,
--keepalive_pool = 20,
keepalive = false
}
Ok, interesting, but again - a working config file with steps to reproduce the error are needed here. The behaviour of the library is very dependent on the actual run time request / response data, and so without examples we are left guessing.
If I am forced to guess, this sounds like there is a discrepancy between the headers you are sending and the request body (e.g. bad Content-Length
), meaning there are bytes left on the wire and then the connection is reused, resulting in bad parsing of the next request. Or something like that. Usually I would expect to see errors logged regarding this however.
Ok, interesting, but again - a working config file with steps to reproduce the error are needed here. The behaviour of the library is very dependent on the actual run time request / response data, and so without examples we are left guessing.
If I am forced to guess, this sounds like there is a discrepancy between the headers you are sending and the request body (e.g. bad
Content-Length
), meaning there are bytes left on the wire and then the connection is reused, resulting in bad parsing of the next request. Or something like that. Usually I would expect to see errors logged regarding this however.
turn on http.debug(true), debug log as below
2021/03/24 18:46:13 [debug] 26993#26993: *53083 [lua] http.lua:716: send_request():
GET /context/v2/comment/list?offset=&id=6827 HTTP/1.1
host: myhost.com
authorization: Apple token
accept-encoding: br;q=1.0, gzip;q=0.9, deflate;q=0.8
user-agent: SSSSS/2.4.0 (iPhone; CPU iPhone OS 13.4.1 like Mac OS X) (com.myproduct; build:1; iOS 13.4.1) Alamofire/5.3.0
accept: application/json
accept-language: zh-CN
seems no error or exception appeared
by the way, use ngx.req.get_method() instead of ngx.var.request_method doesn't work.
I'm closing this because I don't see that this could be a bug in lua-resty-http
.
I am certain that the method is being overridden elsewhere in the configuration, somehow. For example, with ngx.req.set_method
or similar. Or perhaps an accidental Lua global is in play since you mention high concurrency is required to trigger the error.
From the point of view of lua-resty-http
, we simply pass whatever method string is set, and this is definitely never truncated.