holinhot
V2EX  ›  问与答

nginx lua 能不能返回变量到 nginx

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

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

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

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