You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@apisix.apache.org by sp...@apache.org on 2021/10/09 09:38:38 UTC

[apisix] branch master updated: fix: pass correct host header to health checker target nodes (#5175)

This is an automated email from the ASF dual-hosted git repository.

spacewander pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/apisix.git


The following commit(s) were added to refs/heads/master by this push:
     new 621c7b8  fix: pass correct host header to health checker target nodes (#5175)
621c7b8 is described below

commit 621c7b819951749aecd08093f6301ca4f7f93eac
Author: 罗泽轩 <sp...@gmail.com>
AuthorDate: Sat Oct 9 17:37:51 2021 +0800

    fix: pass correct host header to health checker target nodes (#5175)
---
 apisix/upstream.lua   |   6 ++-
 t/lib/server.lua      |   1 +
 t/node/healthcheck2.t | 129 ++++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 135 insertions(+), 1 deletion(-)

diff --git a/apisix/upstream.lua b/apisix/upstream.lua
index 5be6d64..8c4919a 100644
--- a/apisix/upstream.lua
+++ b/apisix/upstream.lua
@@ -111,8 +111,12 @@ local function create_checker(upstream)
 
     local host = upstream.checks and upstream.checks.active and upstream.checks.active.host
     local port = upstream.checks and upstream.checks.active and upstream.checks.active.port
+    local up_hdr = upstream.pass_host == "rewrite" and upstream.upstream_host
+    local use_node_hdr = upstream.pass_host == "node"
     for _, node in ipairs(upstream.nodes) do
-        local ok, err = checker:add_target(node.host, port or node.port, host)
+        local host_hdr = up_hdr or (use_node_hdr and node.domain)
+        local ok, err = checker:add_target(node.host, port or node.port, host,
+                                           true, host_hdr)
         if not ok then
             core.log.error("failed to add new health check target: ", node.host, ":",
                     port or node.port, " err: ", err)
diff --git a/t/lib/server.lua b/t/lib/server.lua
index 9f18496..d72a308 100644
--- a/t/lib/server.lua
+++ b/t/lib/server.lua
@@ -109,6 +109,7 @@ end
 
 
 function _M.status()
+    ngx.log(ngx.WARN, "client request host: ", ngx.var.http_host)
     ngx.say("ok")
 end
 
diff --git a/t/node/healthcheck2.t b/t/node/healthcheck2.t
index 1a083af..2939175 100644
--- a/t/node/healthcheck2.t
+++ b/t/node/healthcheck2.t
@@ -231,3 +231,132 @@ qr/\([^)]+\) unhealthy .* for '.*'/
 (upstream#/routes/arr_1) unhealthy TCP increment (1/2) for 'foo.com(127.0.0.1:1970)'
 (upstream#/routes/arr_1) unhealthy TCP increment (2/2) for 'foo.com(127.0.0.1:1970)'
 --- timeout: 10
+
+
+
+=== TEST 4: pass the configured host (pass_host == "pass")
+--- apisix_yaml
+routes:
+    - id: 1
+      uri: /server_port
+      upstream:
+          type: roundrobin
+          nodes:
+              "localhost:1980": 1
+              "127.0.0.1:1981": 1
+          checks:
+              active:
+                  http_path: /status
+                  healthy:
+                      interval: 1
+                      successes: 1
+                  unhealthy:
+                      interval: 1
+                      http_failures: 2
+#END
+--- 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"
+
+            do
+                local httpc = http.new()
+                local res, err = httpc:request_uri(uri, {method = "GET", keepalive = false})
+            end
+
+            ngx.sleep(1)
+        }
+    }
+--- no_error_log
+client request host: localhost
+--- error_log
+client request host: 127.0.0.1
+
+
+
+=== TEST 5: pass the configured host (pass_host == "node")
+--- apisix_yaml
+routes:
+    - id: 1
+      uri: /server_port
+      upstream:
+          type: roundrobin
+          pass_host: node
+          nodes:
+              "localhost:1980": 1
+              "127.0.0.1:1981": 1
+          checks:
+              active:
+                  http_path: /status
+                  healthy:
+                      interval: 1
+                      successes: 1
+                  unhealthy:
+                      interval: 1
+                      http_failures: 2
+#END
+--- 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"
+
+            do
+                local httpc = http.new()
+                local res, err = httpc:request_uri(uri, {method = "GET", keepalive = false})
+            end
+
+            ngx.sleep(1)
+        }
+    }
+--- error_log
+client request host: localhost
+client request host: 127.0.0.1
+
+
+
+=== TEST 6: pass the configured host (pass_host == "rewrite")
+--- apisix_yaml
+routes:
+    - id: 1
+      uri: /server_port
+      upstream:
+          type: roundrobin
+          pass_host: rewrite
+          upstream_host: foo.com
+          nodes:
+              "localhost:1980": 1
+              "127.0.0.1:1981": 1
+          checks:
+              active:
+                  http_path: /status
+                  healthy:
+                      interval: 1
+                      successes: 1
+                  unhealthy:
+                      interval: 1
+                      http_failures: 2
+#END
+--- 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"
+
+            do
+                local httpc = http.new()
+                local res, err = httpc:request_uri(uri, {method = "GET", keepalive = false})
+            end
+
+            ngx.sleep(1)
+        }
+    }
+--- no_error_log
+client request host: localhost
+client request host: 127.0.0.1
+--- error_log
+client request host: foo.com