V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
holinhot
V2EX  ›  问与答

nginx lua 能不能返回变量到 nginx

  •  
  •   holinhot · 2017-11-06 12:41:35 +08:00 · 1859 次点击
    这是一个创建于 2334 天前的主题,其中的信息可能已经有所发展或是发生改变。
    例如 lua 计算结果为$key=abc1233
    然后 nginx 配置文件里设置 rewrite ^ /$key;

    这样行不行,不在 lua 里返回重定向
    holinhot
        2
    holinhot  
    OP
       2017-11-06 12:55:07 +08:00
    找到了,等下测试一下

    location /foo {
    set $my_var ''; # this line is required to create $my_var at config time
    content_by_lua_block {
    ngx.var.my_var = 123;
    ...
    }
    }
    dndx
        3
    dndx  
       2017-11-06 13:20:44 +08:00
    LZ 的做法是不行的,因为 content phase 跑在 rewrite phase 之后,你在 content_by_lua_block 改 my_var 对 rewrite 工作是没有任何影响的。

    正确的做法应该是用 set_by_lua,或者更好的是直接用 ngx.exec() 来做内部跳转,还省了变量的开销。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   973 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 20:44 · PVG 04:44 · LAX 13:44 · JFK 16:44
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.