Reputation: 431
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!
Upvotes: 3
Views: 11484
Reputation: 61
You can use the API ngx.req.clear_header(header)
or ngx.req.set_header(header, nil)
, which is
Upvotes: 1
Reputation: 19297
location /apiproxytest {
set_by_lua $redirectURL '
local url = ngx.req.get_headers()["RedirectURL"]
ngx.req.clear_header("RedirectURL")
return url
';
proxy_pass $redirectURL;
}
Upvotes: 6
Reputation: 3064
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.
Upvotes: 3