lua 内存线程中止:尝试连接 'cookie_sessionid' 字段(为 nil 值)

/etc/nginx/sites-enabled/default:

location /pine {
    add_header Access-Control-Allow-Origin http://localhost:6285;
    add_header Access-Control-Allow-Credentials true;
    access_by_lua_file /home/akuznetsov/auth.lua;
    proxy_pass http://localhost:9100;
}

auth.lua (版本 1,正常工作):

ngx.req.set_header('x-user-id', ngx.var.cookie_sessionid)

auth.lua (版本 2,不工作):

ngx.req.set_header('x-user-id', 'session:' .. ngx.var.cookie_sessionid)

/var/log/nginx/error.log 中出现了以下错误:

2016/04/06 16:13:10 [error] 14183#0: *1 lua entry thread aborted: runtime error: /home/akuznetsov/auth.lua:2: attempt to concatenate field 'cookie_sessionid' (a nil value)
stack traceback:
coroutine 0:
/home/akuznetsov/auth.lua:2: in function </home/akuznetsov/auth.lua:1>, client: 127.0.0.1, server: localhost, request: "OPTIONS /pine HTTP/1.1", host: "localhost", referrer: "http://localhost:6285/chart/kjckCBcG/?pine=http://localhost/pine"

连接出了什么问题?

点赞
用户936986
用户936986

ngx.var.cookie_sessionid 的值是 nil,正如提示消息所指出的一样,你不能进行连接操作(即使用 ..)。提供一个 if 条件语句来处理这种情况,或者如果你可以接受使用空字符串作为默认值,可以使用 ngx.req.set_header('x-user-id', 'session:' .. ngx.var.cookie_sessionid or "")

2016-04-06 13:37:56