NGINX LUA 内容长度 +1 字节丢失

问题描述

我遇到了一个有趣的错误方法问题
Lua 提到 js_content 变量的长度为 80 个字节。
但是当我不使用 "Content-Length" 标头时,firefox 提到传输了 81 字节的数据。
我不知道 +1 字节的多余来自哪里 如果您能提供帮助,我会很高兴,当我注意到 "Content-Length" 标头是 80 字节时,我用 VBNet 编写的应用程序在解析来自远程服务器的 json 数据时出现错误,但是当我添加 +1 时它工作正常.

local ref_array = {1,2,3}

local sArray = {}
sArray["1"] = "One"
sArray["2"] = "Two"
sArray["3"] = "Tree"

local ctable = {}

for index,data in ipairs(ref_array) do

    if sArray[tostring(data)] ~= nil then
        local cinfo = {}
        cinfo["X"] = tostring(data)
        cinfo["Y"] = sArray[tostring(data)]
        cinfo["Z"] = 0
        table.insert(ctable,cinfo)
    end 
end     

local js_content = cjson.encode(ctable)

ngx.header['Content-Type'] = 'application/json'
ngx.header['Content-Length'] = #js_content -- 80 byte

ngx.say(js_content)

ngx.exit(200)

解决方法

0a

我猜问题是末尾的换行符字符。

ngx.say 总是添加换行符
ngx.print 只是输出

问题已解决

Linefeed Character