Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ngx_lua how to remove http request header

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!

like image 808
SomnusLee Avatar asked Dec 06 '25 04:12

SomnusLee


1 Answers

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;
}
like image 70
fannheyward Avatar answered Dec 07 '25 21:12

fannheyward



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!