diff --git a/apisix/balancer.lua b/apisix/balancer.lua index 50488cd225cb..cd9edcaac5f5 100644 --- a/apisix/balancer.lua +++ b/apisix/balancer.lua @@ -61,7 +61,7 @@ local function fetch_health_nodes(upstream, checker) return new_nodes end - local host = upstream.checks and upstream.checks.host + local host = upstream.checks and upstream.checks.active and upstream.checks.active.host local up_nodes = core.table.new(0, #nodes) for _, node in ipairs(nodes) do local ok = checker:get_target_status(node.host, node.port, host) @@ -91,8 +91,10 @@ local function create_checker(upstream, healthcheck_parent) shm_name = "upstream-healthcheck", checks = upstream.checks, }) + + local host = upstream.checks and upstream.checks.active and upstream.checks.active.host for _, node in ipairs(upstream.nodes) do - local ok, err = checker:add_target(node.host, node.port, upstream.checks.host) + local ok, err = checker:add_target(node.host, node.port, host) if not ok then core.log.error("failed to add new health check target: ", node.host, ":", node.port, " err: ", err) @@ -192,18 +194,15 @@ local function pick_server(route, ctx) ctx.balancer_try_count = (ctx.balancer_try_count or 0) + 1 if checker and ctx.balancer_try_count > 1 then local state, code = get_last_failure() + local host = up_conf.checks and up_conf.checks.active and up_conf.checks.active.host if state == "failed" then if code == 504 then - checker:report_timeout(ctx.balancer_ip, ctx.balancer_port, - up_conf.checks.host) + checker:report_timeout(ctx.balancer_ip, ctx.balancer_port, host) else - checker:report_tcp_failure(ctx.balancer_ip, - ctx.balancer_port, up_conf.checks.host) + checker:report_tcp_failure(ctx.balancer_ip, ctx.balancer_port, host) end - else - checker:report_http_status(ctx.balancer_ip, ctx.balancer_port, - up_conf.checks.host, code) + checker:report_http_status(ctx.balancer_ip, ctx.balancer_port, host, code) end end diff --git a/t/node/healthcheck.t b/t/node/healthcheck.t index 96ff4f48217c..d2cca60cba70 100644 --- a/t/node/healthcheck.t +++ b/t/node/healthcheck.t @@ -548,3 +548,76 @@ code: 200 body: passed delete code: 200 --- no_error_log [error] + + + +=== TEST 12: add route (test health check config `host` valid) +--- config + location /t { + content_by_lua_block { + local t = require("lib.test_admin").test + local code, body = t('/apisix/admin/routes/1', + ngx.HTTP_PUT, + [[{ + "uri": "/server_port", + "upstream": { + "type": "roundrobin", + "nodes": { + "127.0.0.1:1980": 1, + "127.0.0.1:1988": 1 + }, + "checks": { + "active": { + "http_path": "/status", + "host": "foo.com", + "healthy": { + "interval": 1, + "successes": 1 + }, + "unhealthy": { + "interval": 1, + "http_failures": 2 + } + } + } + } + }]] + ) + + if code >= 300 then + ngx.status = code + end + ngx.say(body) + } + } +--- request +GET /t +--- response_body +passed + + + +=== TEST 13: test health check config `host` valid +--- config + location /t { + content_by_lua_block { + local http = require "resty.http" + local uri = "http://127.0.0.1:" .. ngx.var.server_port + .. "/server_port" + + local httpc = http.new() + local res, err = httpc:request_uri(uri, {method = "GET", keepalive = false}) + + ngx.sleep(2) + + ngx.say(res.status) + } + } +--- request +GET /t +--- response_body +200 +--- grep_error_log eval +qr/^.*?\[warn\].*/ +--- grep_error_log_out eval +qr/unhealthy TCP increment.*foo.com/