holinhot
V2EX  ›  问与答

nginx lua 能不能返回变量到 nginx

  •  
  •   holinhot · Nov 6, 2017 · 2414 views
    This topic created in 3134 days ago, the information mentioned may be changed or developed.
    例如 lua 计算结果为$key=abc1233
    然后 nginx 配置文件里设置 rewrite ^ /$key;

    这样行不行,不在 lua 里返回重定向
    holinhot
        2
    holinhot  
    OP
       Nov 6, 2017
    找到了,等下测试一下

    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  
       Nov 6, 2017
    LZ 的做法是不行的,因为 content phase 跑在 rewrite phase 之后,你在 content_by_lua_block 改 my_var 对 rewrite 工作是没有任何影响的。

    正确的做法应该是用 set_by_lua,或者更好的是直接用 ngx.exec() 来做内部跳转,还省了变量的开销。
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   990 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 23:03 · PVG 07:03 · LAX 16:03 · JFK 19:03
    ♥ Do have faith in what you're doing.