3

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!

3 Answers 3

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;
}
Sign up to request clarification or add additional context in comments.

Comments

3

It is usual mistake. You think that nginx directives are executed in the same order as in config file. There are a lot phases of processing of nginx configuration files. Good, but long explanation: Nginx directive execution order

In their execution order the phases are post-read, server-rewrite, find-config, rewrite, post-rewrite, preaccess, access, post-access, try-files, content, and finally log.

set_by_lua - phase: rewrite
echo - phase: content
proxy_pass - phase: content
header_filter_by_lua - phase: output-header-filter

You may see unlisted phase - output-header-filter. Also I know about output-body-filter. These are special phases and implemented in different way then rewrite, contect etc phases. I cannot find a documentation for these phases. Any help are welcome! But obviously they cannot be early then content phase.

Your config's directives are executed in next order:

set_by_lua directives
echo and proxy_pass directives
header_filter_by_lua directives

So ngx.header["RedirectURL"] = nil really executed later and has no influence to your echo directives.

Comments

1

You can use the API ngx.req.clear_header(header) or ngx.req.set_header(header, nil), which is

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.