ngx_lua how to remove http request header

SomnusLee picture SomnusLee · May 21, 2015 · Viewed 8.6k times · Source

There is a param named "RedirectURL" in http request header. I want to remove it in ngx_lua and then send this request to RedirectURL, here is some snippet in 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;
}   

When I test it use

curl --header "RedirectURL:www.google.com" xx.xx.xx.xx:xx/apiproxytest

I get result:

1: www.google.com
2: www.google.com

I don't know where the wrong is. Who can help me figure it out? Thanks for any help!

Answer

fannheyward picture fannheyward · May 25, 2015

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;
}