Skip to content
Open
Show file tree
Hide file tree
Changes from 7 commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
90b3c13
adds support of variables into upstream module
undying Apr 28, 2025
9def4fb
move local upper from if
undying Apr 29, 2025
cfea8d8
linter fixes
undying May 15, 2025
3b90fe2
Merge branch 'master' into upstreams_variable_support
undying May 26, 2025
ac76dd7
bump build
undying May 27, 2025
2160c51
new line
undying May 28, 2025
e8e086c
Merge branch 'master' into upstreams_variable_support
undying May 29, 2025
5a907fa
removes is_nginx_variable
undying Jun 2, 2025
ab7bc5d
Merge branch 'upstreams_variable_support' of github.com:undying/apisi…
undying Jun 2, 2025
3a623bc
new line
undying Jun 3, 2025
5b457df
up
undying Jun 3, 2025
b661517
check for empty string in service name
undying Jun 3, 2025
5f19d54
newline
undying Jun 4, 2025
7548cf8
use resolve_var in error message
undying Jun 4, 2025
b669e78
removes function documentation
undying Jun 9, 2025
e8b18c1
adds documentation for service_name as variable
undying Jun 9, 2025
7d411d3
adds tests for service_name as variable
undying Jun 9, 2025
8a14a9e
adds nginx configuration
undying Jun 10, 2025
9c58805
returns the previous comment
undying Jun 10, 2025
dbb795a
fix tests lint
undying Jun 11, 2025
54d12c9
escape variables
undying Jun 11, 2025
3b5782d
move map into http_config
undying Jun 12, 2025
c6da5d1
fix nil value for $backend
undying Jun 13, 2025
100fa55
move set backend directly into the test
undying Jun 16, 2025
b688b39
move variable into http section
undying Jul 17, 2025
2c3d085
register services
undying Jul 18, 2025
6fdd1ec
add upstreams for tests
undying Jul 21, 2025
641fe78
fix newline and description
undying Jul 22, 2025
e54929a
adds details into the Chinese documentation
undying Jul 22, 2025
6f21de0
Merge branch 'master' into upstreams_variable_support
undying Jul 23, 2025
94920a4
Merge branch 'master' into upstreams_variable_support
undying Jul 25, 2025
e7f886c
adds service_name logging
undying Aug 1, 2025
fdcd511
Merge branch 'master' into upstreams_variable_support
undying Aug 4, 2025
3cd47e5
Merge branch 'master' into upstreams_variable_support
undying Aug 27, 2025
a7ca200
Merge branch 'master' into upstreams_variable_support
undying Sep 8, 2025
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
30 changes: 30 additions & 0 deletions apisix/core/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -461,5 +461,35 @@ function _M.check_tls_bool(fields, conf, plugin_name)
end
end

---
-- Checks if a string is an nginx variable.
-- An nginx variable starts with the '$' character.
--
-- @function core.utils.is_nginx_variable
-- @tparam string str The string to check
-- @treturn boolean true if the string starts with '$', false otherwise
-- @usage
-- local utils = require("apisix.core.utils")
--
-- -- Usage examples:
-- local is_var = utils.is_nginx_variable("$host") -- true
-- local is_var = utils.is_nginx_variable("host") -- false
-- local is_var = utils.is_nginx_variable("${host}") -- true
-- local is_var = utils.is_nginx_variable("\\$host") -- false
--
-- -- Usage in APISIX context:
-- if utils.is_nginx_variable(up_conf.service_name) then
-- -- Handle as nginx variable
-- else
-- -- Handle as regular service name
-- end
function _M.is_nginx_variable(str)
if not str or type(str) ~= "string" then
return false
end

-- Check if the string starts with '$' and it's not escaped
return str:sub(1, 1) == "$" and (str:sub(1, 2) ~= "\\$")
end

return _M
7 changes: 6 additions & 1 deletion apisix/upstream.lua
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,12 @@ function _M.set_by_route(route, api_ctx)
return 503, err
end

local new_nodes, err = dis.nodes(up_conf.service_name, up_conf.discovery_args)
local service_name = up_conf.service_name
if core.utils.is_nginx_variable(up_conf.service_name) then
service_name = core.utils.resolve_var(up_conf.service_name, api_ctx.var)
end

local new_nodes, err = dis.nodes(service_name, up_conf.discovery_args)
if not new_nodes then
return HTTP_CODE_UPSTREAM_UNAVAILABLE, "no valid upstream node: " .. (err or "nil")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please add service_name to this error log, since service_name is no longer a fixed value, we need to know the specific service_name when there is a no valid upstream node.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added logging service_name. This format is ok?

end
Expand Down
30 changes: 30 additions & 0 deletions t/core/utils.t
Original file line number Diff line number Diff line change
Expand Up @@ -393,3 +393,33 @@ res:nil
res:5
res:12
res:7



=== TEST X: is_nginx_variable
--- config
location /t {
content_by_lua_block {
local is_nginx_variable = require("apisix.core.utils").is_nginx_variable

local cases = {
{str = "$host", expected = true},
{str = "host", expected = false},
{str = "${host}", expected = true},
{str = "\\$host", expected = false},
{str = nil, expected = false},
{str = 123, expected = false},
{str = "", expected = false},
}

for _, case in ipairs(cases) do
local res = is_nginx_variable(case.str)
assert(res == case.expected,
string.format("case %s failed: got %s, expected %s",
case.str, res, case.expected))
end
}
}
--- request
GET /t
--- response_body
Loading