-
Notifications
You must be signed in to change notification settings - Fork 2.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: proxy-rewrite support config add set and remove header #8336
Conversation
| headers | object | False | | | New Upstream headers. Headers are overwritten if they are already present otherwise, they are added to the present headers. To remove a header, set the header value to an empty string. The values in the header can contain Nginx variables like `$remote_addr` and `$client_addr`. | | ||
| headers | object | False | | | | | ||
| headers.add | object | false | | | Append the new headers. The format is `{"name: value",...}`. The values in the header can contain Nginx variables like $remote_addr and $balancer_ip. | | ||
| headers.set | object | false | | | Rewriting the headers. The format is `{"name": "value", ...}`. The values in the header can contain Nginx variables like $remote_addr and $balancer_ip. | |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think use headers.rewrite
is more clear.
What happens if headers.set
is set, but the actual request does not have a header name that needs to be rewritten?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This headers.set
is the same as the response-rewrite plugin. When the header does not exist will add it.
apisix/plugins/proxy-rewrite.lua
Outdated
end | ||
|
||
local function addHeader(ctx, header_name, header_value) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
local function addHeader(ctx, header_name, header_value) | |
local function add_header(ctx, header_name, header_value) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
apisix/plugins/proxy-rewrite.lua
Outdated
end | ||
|
||
local function addHeader(ctx, header_name, header_value) | ||
local oldHeader = core.request.header(ctx, header_name) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
local oldHeader = core.request.header(ctx, header_name) | |
local old_header = core.request.header(ctx, header_name) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
test: sssss | ||
test: bbb | ||
--- response_headers | ||
test: sssss, bbb, 123 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why it is
test: sssss
test: bbb
test: 123
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is for test header combination. First combine two existing headers test: sssss
and test: bbb
. And then add a new header test: 123
from proxy_rewrite. Finally, it will combine all headers in one.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it necessary to merge header with the same name? I'm not sure. cc @spacewander
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We need to use https://github.com/openresty/lua-resty-core/blob/master/lib/ngx/req.md#add_header. Merging multiple request headers with ,
doesn't obey the standard.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done @spacewander
apisix/plugins/proxy-rewrite.lua
Outdated
minItems = 1, | ||
items = { | ||
type = "string", | ||
-- "Set-Cookie" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Set-Cookie is a response header. Better to use another example.
test: sssss | ||
test: bbb | ||
--- response_headers | ||
test: sssss, bbb, 123 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We need to use https://github.com/openresty/lua-resty-core/blob/master/lib/ngx/req.md#add_header. Merging multiple request headers with ,
doesn't obey the standard.
t/plugin/proxy-rewrite3.t
Outdated
} | ||
} | ||
--- request | ||
GET /t |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please remove duplicate sections which are set in
apisix/t/plugin/proxy-rewrite3.t
Line 28 in ae400b9
if (!$block->request) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
t/plugin/proxy-rewrite3.t
Outdated
GET /t | ||
--- response_body | ||
passed | ||
--- no_error_log |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ditto
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
t/plugin/proxy-rewrite3.t
Outdated
--- response_headers | ||
X-Forwarded-Host: test.com | ||
test: sssss, bbb, 123 | ||
--- no_error_log |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Need to deal with #8336 (comment) in other test
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
t/plugin/proxy-rewrite3.t
Outdated
|
||
|
||
|
||
=== TEST 13: rewrite X-Forwarded-Host |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This test isn't relative to this feature?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nope, but this change is to remove duplicate sections from your advice. So I changed some test code.
apisix/core/request.lua
Outdated
error(err) | ||
end | ||
|
||
req_add_header(header_name,header_value) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please ensure a space is after the ','. Let's deal with similar places in this PR.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
Co-authored-by: 罗泽轩 <spacewanderlzx@gmail.com>
apisix/core/request.lua
Outdated
@@ -39,6 +44,7 @@ local req_get_uri_args = ngx.req.get_uri_args | |||
local req_set_uri_args = ngx.req.set_uri_args | |||
|
|||
|
|||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
delete this line
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
Description
Fixes #8239
Checklist