我使用的是 access_by_lua_block
在我的nginx配置中添加modify自定义请求头(比如说 ngx.req.set_header("foo", "bar")
). 我正在访问这些头文件,在 header_filter_by_lua_block
作为 ngx.var["http_foo"]
这在将请求传递给上游的情况下可以正常工作,但是在重定向的情况下就不行了,所以基本上。
这工作(无重定向)。
location /abc {
proxy_pass some_upstream;
access_by_lua_block {
ngx.req.set_header("foo", "bar")
}
header_filter_by_lua_block {
ngx.header["foo2"] = ngx.var["http_foo"] # this is correctly getting the value of "foo" header set above
}
}
这不工作(有重定向)
location /abc {
access_by_lua_block {
ngx.req.set_header("foo", "bar")
}
header_filter_by_lua_block {
ngx.header["foo2"] = ngx.var["http_foo"] # this is not getting the value of "foo" header set above
}
return 301 xyz.com;
}
在只有重定向的情况下,access_by_lua_block没有被执行(返回301语句)。我不明白为什么会这样?因为access_by_lua_block的执行优先级高于内容阶段(联系)
据我所知,执行 return
指令发生在重写阶段,而在这种情况下,访问阶段根本不执行。你可以尝试改变 access_by_lua_block
到 rewrite_by_lua_block
看看会发生什么。
第一次尝试解决这个问题时,什么都没有得到。事实上,正如lua-ngx-module文档中的 rewrite_by_lua
州:
请注意,该处理程序始终运行 之后 标准的ngx_http_rewrite_module。
你还可以尝试在这个模块中做重定向。rewrite_by_lua_block
:
location /abc {
rewrite_by_lua_block {
ngx.req.set_header("foo", "bar")
ngx.redirect("xyz.com", 301)
}
header_filter_by_lua_block {
ngx.header["foo2"] = ngx.var["http_foo"] # this is not getting the value of "foo" header set above
}
}