diff --git a/apisix/plugins/traffic-split.lua b/apisix/plugins/traffic-split.lua index 1767c4a9f697..da44d3e85cf5 100644 --- a/apisix/plugins/traffic-split.lua +++ b/apisix/plugins/traffic-split.lua @@ -148,27 +148,18 @@ end local function set_pass_host(ctx, upstream_info, host) - -- Currently only supports a single upstream of the domain name. - -- When the upstream is `IP`, do not do any `pass_host` operation. - if not core.utils.parse_ipv4(host) - and not core.utils.parse_ipv6(host) - then - local pass_host = upstream_info.pass_host or "pass" - if pass_host == "pass" then - ctx.var.upstream_host = ctx.var.host - return - end - - if pass_host == "rewrite" then - ctx.var.upstream_host = upstream_info.upstream_host - return - end + local pass_host = upstream_info.pass_host or "pass" + if pass_host == "pass" then + return + end - ctx.var.upstream_host = host + if pass_host == "rewrite" then + ctx.var.upstream_host = upstream_info.upstream_host return end - return + -- only support single node for `node` mode currently + ctx.var.upstream_host = host end diff --git a/t/plugin/traffic-split2.t b/t/plugin/traffic-split2.t index 655885a4f04a..071853abdd65 100644 --- a/t/plugin/traffic-split2.t +++ b/t/plugin/traffic-split2.t @@ -103,3 +103,212 @@ GET /server_port?name=jack&age=17 GET /server_port?name=jack&age=18 --- response_body chomp 1980 + + + +=== TEST 4: the upstream node is IP and pass_host is `pass` +--- 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": "/uri", + "plugins": { + "traffic-split": { + "rules": [ + { + "match": [ + { + "vars": [["arg_name", "==", "jack"]] + } + ], + "weighted_upstreams": [ + { + "upstream": { + "type": "roundrobin", + "pass_host": "pass", + "nodes": { + "127.0.0.1:1981":1 + } + } + } + ] + } + ] + } + }, + "upstream": { + "type": "roundrobin", + "nodes": { + "127.0.0.1:1980": 1 + } + } + }]=] + ) + if code >= 300 then + ngx.status = code + end + ngx.say(body) + } + } +--- request +GET /t +--- response_body +passed +--- no_error_log +[error] + + + +=== TEST 5: upstream_host is `127.0.0.1` +--- request +GET /uri?name=jack +--- more_headers +host: 127.0.0.1 +--- response_body +uri: /uri +host: 127.0.0.1 +x-real-ip: 127.0.0.1 +--- no_error_log +[error] + + + +=== TEST 6: the upstream node is IP and pass_host is `rewrite` +--- config + location /t { + content_by_lua_block { + local t = require("lib.test_admin").test + local code, body = t('/apisix/admin/routes/1', + ngx.HTTP_PATCH, + [=[{ + "uri": "/uri", + "plugins": { + "traffic-split": { + "rules": [ + { + "match": [ + { + "vars": [["arg_name", "==", "jack"]] + } + ], + "weighted_upstreams": [ + { + "upstream": { + "type": "roundrobin", + "pass_host": "rewrite", + "upstream_host": "test.com", + "nodes": { + "127.0.0.1:1981":1 + } + } + } + ] + } + ] + } + }, + "upstream": { + "type": "roundrobin", + "nodes": { + "127.0.0.1:1980": 1 + } + } + }]=] + ) + if code >= 300 then + ngx.status = code + end + ngx.say(body) + } + } +--- request +GET /t +--- response_body +passed +--- no_error_log +[error] + + + +=== TEST 7: upstream_host is test.com +--- request +GET /uri?name=jack +--- response_body +uri: /uri +host: test.com +x-real-ip: 127.0.0.1 +--- no_error_log +[error] + + + +=== TEST 8: the upstream node is IP and pass_host is `node` +--- config + location /t { + content_by_lua_block { + local t = require("lib.test_admin").test + local code, body = t('/apisix/admin/routes/1', + ngx.HTTP_PATCH, + [=[{ + "uri": "/uri", + "plugins": { + "traffic-split": { + "rules": [ + { + "match": [ + { + "vars": [["arg_name", "==", "jack"]] + } + ], + "weighted_upstreams": [ + { + "upstream": { + "type": "roundrobin", + "pass_host": "node", + "nodes": { + "localhost:1981":1 + } + } + } + ] + } + ] + } + }, + "upstream": { + "type": "roundrobin", + "nodes": { + "127.0.0.1:1980": 1 + } + } + }]=] + ) + if code >= 300 then + ngx.status = code + end + ngx.say(body) + } + } +--- request +GET /t +--- response_body +passed +--- no_error_log +[error] + + + +=== TEST 9: upstream_host is localhost +--- request +GET /uri?name=jack +--- more_headers +host: 127.0.0.1 +--- response_body +uri: /uri +host: localhost +x-real-ip: 127.0.0.1 +--- no_error_log +[error]