Skip to content
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

fix(core/balancer): fix a bug that the host_header not set correctly #13135

Merged
merged 6 commits into from
Jun 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions changelog/unreleased/host_header.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
message: fix a bug that the `host_header` attribute of upstream entity can not be set correctly in requests to upstream as Host header when retries to upstream happen.
scope: Core
type: bugfix
1 change: 1 addition & 0 deletions kong/runloop/balancer/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,7 @@ local function execute(balancer_data, ctx)
if dns_cache_only then
-- retry, so balancer is already set if there was one
balancer = balancer_data.balancer
upstream = balancer_data.upstream

else
-- first try, so try and find a matching balancer/upstream object
Expand Down
74 changes: 74 additions & 0 deletions spec/02-integration/05-proxy/03-upstream_headers_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1217,4 +1217,78 @@ for _, strategy in helpers.each_strategy() do
end)
end)
end)

describe("host_header should be set correctly", function()
lazy_setup(function()
local bp = helpers.get_db_utils(strategy, {
"routes",
"services",
})

local upstream = assert(bp.upstreams:insert {
name = "foo",
host_header = "foo.com",
})

assert(bp.targets:insert {
target = "127.0.0.1:62351",
upstream = upstream,
weight = 50,
})

local service = bp.services:insert {
name = "retry_service",
host = "foo",
retries = 5,
}

bp.routes:insert {
service = service,
paths = { "/hello" },
strip_path = false,
}

local fixtures = {
http_mock = {}
}

fixtures.http_mock.my_server_block = [[
server {
listen 0.0.0.0:62351;
location /hello {
content_by_lua_block {
local shd = ngx.shared.request_counter
local request_counter = shd:incr("counter", 1, 0)
if request_counter % 2 ~= 0 then
ngx.exit(ngx.HTTP_CLOSE)
else
ngx.say(ngx.var.host)
end
}
}
}
]]

assert(helpers.start_kong({
database = strategy,
nginx_conf = "spec/fixtures/custom_nginx.template",
nginx_http_lua_shared_dict = "request_counter 1m",
}, nil, nil, fixtures))
end)

lazy_teardown(function()
assert(helpers.stop_kong())
end)

it("when retries to upstream happen", function()
local proxy_client = helpers.proxy_client()
local res = assert(proxy_client:send {
method = "GET",
path = "/hello",
})

local body = assert.res_status(200, res)
assert.equal("foo.com", body)
end)
end)
end
Loading