NGINX cjson lua 解析 json

我正在尝试运行 手册 中找到的 cjson 解析示例:

但似乎无法工作。

以下是相关的配置:

 location / {
            content_by_lua '
                    local cjson = require("cjson")
                    json_text = '[ true, { "foo": "bar" } ]'
                    value = cjson.decode(json_text)
                    ngx.say(value)
            ';

            resolver 8.8.8.8;
    }

我得到了以下错误:

sudo nginx -t
nginx: [emerg] unexpected "[" in /etc/nginx/nginx.conf:29
nginx: configuration file /etc/nginx/nginx.conf test failed

如果我删除方括号,则会得到以下内容:

sudo nginx -t
nginx: [emerg] directive "content_by_lua" is not terminated by ";" in  /etc/nginx/nginx.conf:28
nginx: configuration file /etc/nginx/nginx.conf test failed

任何人有任何想法我在做什么错了吗?似乎 cjson 应该是在 NGINX 中解析 json 的一种相当简单的方法,我知道人们正在这么做……只是无法弄清楚为什么即使示例代码也无法为我工作。

点赞