ngx_lua 如何删除http请求头

Som*_*Lee 3 nginx openresty

http 请求标头中有一个名为“RedirectURL”的参数。我想在 ngx_lua 中删除它,然后将此请求发送到 RedirectURL,这是 nginx.conf 中的一些片段

location /apiproxytest {

           set_by_lua $redirectURL '
                    return ngx.req.get_headers()["RedirectURL"]
            ';

            header_filter_by_lua '
                     ngx.header["RedirectURL"] = nil;
            ';

            echo "1:" $redirectURL;

            set_by_lua $redirectURL2 '
                    return ngx.req.get_headers()["RedirectURL"]
            ';
            echo "2:" $redirectURL2;

            proxy_pass $redirectURL;
}   
Run Code Online (Sandbox Code Playgroud)

当我测试它时使用

curl --header "RedirectURL:www.google.com" xx.xx.xx.xx:xx/apiproxytest
Run Code Online (Sandbox Code Playgroud)

我得到结果:

1: www.google.com
2: www.google.com
Run Code Online (Sandbox Code Playgroud)

我不知道错在哪里。谁能帮我弄清楚?谢谢你的帮助!

fan*_*ard 6

ngx.req.clear_header

location /apiproxytest {
    set_by_lua $redirectURL '
        local url = ngx.req.get_headers()["RedirectURL"]
        ngx.req.clear_header("RedirectURL")

        return url
    ';

    proxy_pass $redirectURL;
}
Run Code Online (Sandbox Code Playgroud)