【发布时间】:2020-09-30 04:47:21
【问题描述】:
我在我的 nginx 配置中使用access_by_lua_block 来添加/修改自定义请求标头(比如说ngx.req.set_header("foo", "bar"))。在向客户端返回任何响应之前,我正在以ngx.var["http_foo"] 的形式访问header_filter_by_lua_block 中的这些标头。
如果将请求传递到上游,这可以正常工作,但是在重定向的情况下它不起作用。
所以基本上,
这有效(无重定向)。
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在内容阶段之前有一个执行优先级(link)
【问题讨论】:
标签: nginx lua nginx-reverse-proxy nginx-location openresty