您的位置:首页 > 运维架构 > Nginx

【???】nginx_lua问题记录

2015-03-10 13:42 323 查看
问题1:

当用http://localhost/test 访问时,结果为何迥异?

eg1 :

location /test {
                set $test '';
                content_by_lua '
                        ngx.var.test = 123;
                        ngx.say(ngx.var.test);   
                ';
                echo $test;     #
        }


结果为空,说明执行的是HttpEchoModule的echo指令,没有执行httpLuaModule的content_by_lua指令

eg2:

location /test {
                set $test '';
                echo "yyy";   #
                echo "zzz";   #
                content_by_lua '
                        ngx.var.test = 123;
                        ngx.say(ngx.var.test);
                ';
        }
输出123 ,说明执行的是httpLuaModule的content_by_lua指令,没有执行HttpEchoModule的echo指令

eg3:

location /test1 {
    set $a 123;
    content_by_lua ' ngx.say(ngx.var.a) ';
    echo $a;
    set $a 456;
    echo $a;
}


输出:

456

456

说明执行的是HttpEchoModule的echo指令,没有执行httpLuaModule的content_by_lua指令

eg4:

location /test1 {
    set $a 123;
    echo $a;
    set $a 456;
    echo $a;
    content_by_lua ' ngx.say(ngx.var.a) ';
}


输出:456
说明执行的是httpLuaModule的content_by_lua指令,没有执行HttpEchoModule的echo指令

综上四个例子的结论:

nginx的content_phases只能注册一个第三方的module。HttpEchoModule和httpLuaModule都注册在这个phases,当一个location同时又两个第三方的module注册content phase时,只能有一个注册成功,在这里HttpEchoModule和httpLuaModule 只能有一个注册成功,至于是谁注册成功,这是不确定的。

问题2:

access_by_lua rewrite_by_lua content_by_lua 三个块交换顺序有什么影响吗?主要想了解nginx执行的不同phase

location /test {
        rewrite_by_lua '
            ngx.say("foo = ", ngx.ctx.foo)
            ngx.ctx.foo = 76
        ';
        access_by_lua '
            ngx.ctx.foo = ngx.ctx.foo + 3
        ';
        content_by_lua '
            ngx.say(ngx.ctx.foo)
        ';
    }


问题3:

[root@de73cd1ca978 nginx]# sbin/nginx -s reload
[root@de73cd1ca978 nginx]# curl -d "uid=2646095867" http://localhost/statuses/friends_timeline    
1
110.210.130.21:807710.210.130.21:8078
path2
[root@de73cd1ca978 nginx]# curl -d "uid=2646095867" http://localhost/statuses/friends_timeline [root@de73cd1ca978 nginx]#
reload nginx配置以后,第一遍请求有内容,为何第二遍请求没有内容了?

解决方法:是关闭nginx.conf中的 lua_code_cache 开关,改为off

但是生产环境下的时候这个开关必须为on,怎么办?

参见这里:

https://www.fullautocapitalism.com/openresty%20lua-nginx-module%20%E9%83%A8%E5%88%86%E4%B8%AD%E6%96%87%E6%96%87%E6%A1%A3(lua_code_cache)

https://github.com/openresty/lua-nginx-module#lua_code_cache

https://groups.google.com/forum/#!topic/openresty/W0dvD5Wm0Bw

https://groups.google.com/forum/#!msg/openresty/qy3wDk6z038/crKmpmRwyOsJ

https://groups.google.com/forum/#!topic/openresty/kkr10J-__U0

https://github.com/openresty/lua-nginx-module/issues/272
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: