如何从 openresty header_filter_by_lua_block 返回404

我有一个上游服务器,我无法更改任何内容。

在我的Openresty(带有Lua的nginx)中,我想检查proxy_pass,如果响应中存在某个标头,则继续,但如果不存在,则返回404。

这是我的配置:

header_filter_by_lua_block {
                local h, err = ngx.resp.get_headers()
                local found = 0
                for key, _ in pairs(h) do
                    if key == "have-to-be-header" then
                        found = 1
                    end
                    local start = string.sub(key, 1, 7)
                    if start == "ToClean" or start == "toClean" then
                        ngx.header[key] = nul
                    end
                end
                if found==0 then
                    ngx.status = 404
                    return ngx.exit(ngx.HTTP_NOT_FOUND)
                end
            }

现在的行为是第一次请求有效,第二次及之后卡住。

能否有人帮助我实现这个问题的正确方式?

原文链接 https://stackoverflow.com/questions/70486682

点赞