貌似找到了问题,问题出在随机数的生成上面:
我原先的随机数是这样的:
local function get_strong_random_string(str_len)
local env = require 'env'
local random = require 'resty.random'
local str = require 'resty.string'
local strong_random
str_len = str_len or env.default_random_str_len
strong_random = random.bytes(str_len, true)
while strong_random == nil do
strong_random = random.bytes(str_len, true)
end
return str.to_hex(strong_random)
end
后来把随机数的产生方法改成:
http://lua-users.org/wiki/RandomStrings
local function get_random_string(str_len)
local Chars = {}
for Loop = 0, 255 do
Chars[Loop+1] = string.char(Loop)
end
local String = table.concat(Chars)
local Built = {['.'] = Chars}
local AddLookup = function(CharSet)
local Substitute = string.gsub(String, '[^'..CharSet..']', '')
local Lookup = {}
for Loop = 1, string.len(Substitute) do
Lookup[Loop] = string.sub(Substitute, Loop, Loop)
end
Built[CharSet] = Lookup
return Lookup
end
--local CharSet = CharSet or '.'
local CharSet = CharSet or '%l%d'
if CharSet == '' then
return ''
else
local Result = {}
local Lookup = Built[CharSet] or AddLookup(CharSet)
local Range = table.getn(Lookup)
for Loop = 1, str_len do
Result[Loop] = Lookup[math.random(1, Range)]
end
return table.concat(Result)
end
end
就不会出现curl: (52) Empty reply from server这个东东了。
我后来写了两个测试,只要在请求中使用resty.random,不断在http://127.0.0.1:8090:/t和http://127.0.0.1:8090:/t2间切换,在使用了strong random的请求上面就会出现empty reply的错误,不知道这是不是resty.random的bug,请春哥查证。
> 总是会伴随一些空的响应,但对同一个location, 并不是每次都是empty reply
>
从你提供的调试日志看,貌似都很正常,nginx 都有往下游写数据。
为进一步确定问题,我有如下建议:
1. 建议你在自己的关键 Lua 代码路径上多加一些输出语句(比如使用 print 或者 ngx.log 输出到 nginx 错误日志)来进行调试。
2. 如果你有使用自己的 Lua module 文件的话,建议总是使用 lua-releng 工具检查是否误用了 Lua 全局变量而导致请求之间串数据。
3. 在调用 curl 时使用 -vv 选项可以在客户端得到更多信息。
4. 可以使用 tcpdump 或者 wireshark 工具在 TCP 层面上抓包进行分析。
5. 如果你实在无法定位问题,请尝试提供一个最小化的完整示例,以便让我能在我本地复现你看到的问题。