request help: no host allowed while connecting to upstream
Issue description
从nacos上发现服务.
- nacos上的服务ip为主机名
uaa-service, 一个为ip - 当负载均衡运行时报
failed to set server peer [uaa-service:20101] err: no host allowed while connecting to upstream - 造成当负载到为主机名的时候返回网关错误. 负载到ip的时候则正常.
Environment
- apisix version (cmd:
apisix version): - OS (cmd:
uname -a): - OpenResty / Nginx version (cmd:
nginx -Voropenresty -V): - etcd version, if have (cmd: run
curl http://127.0.0.1:9090/v1/server_infoto get the info from server-info API): - apisix-dashboard version, if have:
- the plugin runner version, if the issue is about a plugin runner (cmd: depended on the kind of runner):
- luarocks version, if the issue is about installation (cmd:
luarocks --version):
Issue description
从nacos上发现服务.
- nacos上的服务ip为主机名
uaa-service, 一个为ip- 当负载均衡运行时报
failed to set server peer [uaa-service:20101] err: no host allowed while connecting to upstreamEnvironment
- apisix version (cmd:
apisix version):- OS (cmd:
uname -a):- OpenResty / Nginx version (cmd:
nginx -Voropenresty -V):- etcd version, if have (cmd: run
curl http://127.0.0.1:9090/v1/server_infoto get the info from server-info API):- apisix-dashboard version, if have:
- the plugin runner version, if the issue is about a plugin runner (cmd: depended on the kind of runner):
- luarocks version, if the issue is about installation (cmd:
luarocks --version):
如果使用上游类型是node, 那么主机名是没有问题的.
Whether the hostname of uaa-service has been configured with DNS resolution on the APISIX node, you can test it on the APISIX node ping uaa-service
配置了host了. 能ping通的
localhost:~ xxx$ ping uaa-service
PING uaa-service (127.0.0.1): 56 data bytes
64 bytes from 127.0.0.1: icmp_seq=0 ttl=64 time=0.084 ms
64 bytes from 127.0.0.1: icmp_seq=1 ttl=64 time=0.293 ms
64 bytes from 127.0.0.1: icmp_seq=2 ttl=64 time=0.128 ms
^C
--- uaa-service ping statistics ---
3 packets transmitted, 3 packets received, 0.0% packet loss
round-trip min/avg/max/stddev = 0.084/0.168/0.293/0.090 ms
use dig uaa-service and show the result, and proveide the environment info
use
dig uaa-serviceand show the result, and proveide the environment info
apisix version (cmd: apisix version): 2.10.0
OS (cmd: uname -a): macos
OpenResty / Nginx version (cmd: nginx -V or openresty -V):
etcd version, if have (cmd: run curl http://127.0.0.1:9090/v1/server_info to get the info from server-info API):
{ "version": "2.10.0", "hostname": "localhost", "etcd_version": "3.5.0", "up_time": 48, "id": "c1141ba7-70c4-41a9-8d9d-c54c70ac2912", "boot_time": 1634215727, "last_report_time": 1634215727 }
apisix-dashboard version, if have:
the plugin runner version, if the issue is about a plugin runner (cmd: depended on the kind of runner):
luarocks version, if the issue is about installation (cmd: luarocks --version):
复现方式
- discovery config
discovery: # service discovery center
# dns:
# servers:
# - "127.0.0.1:8600" # use the real address of your dns server
nacos:
host: # it's possible to define multiple eureka hosts addresses of the same eureka cluster.
- "http://nacos:[email protected]:8848"
prefix: "/nacos/v1/"
fetch_interval: 3 # default 30 sec
weight: 10 # default 100
timeout:
connect: 2000 # default 2000 ms
send: 2000 # default 2000 ms
read: 5000 # default 5000 ms
- nacos注册服务为

- host 配置
localhost:apisix-2.10.0 xxx$ cat /etc/hosts
127.0.0.1 uaa-service uaa-service-host
- 路由配置
{
"uri": "/uaa/*",
"name": "uaa服务",
"methods": [
"GET",
"POST",
"PUT",
"DELETE",
"PATCH",
"HEAD",
"OPTIONS",
"CONNECT",
"TRACE"
],
"upstream_id": "376959049259811901",
"status": 1
}
- 上游服务
{
"timeout": {
"connect": 6,
"send": 6,
"read": 6
},
"type": "roundrobin",
"scheme": "http",
"discovery_type": "nacos",
"pass_host": "pass",
"name": "uaa2",
"service_name": "uaa-service",
"keepalive_pool": {
"idle_timeout": 60,
"requests": 1000,
"size": 320
}
}
-
调用服务

-
测试
localhost:apisix-2.10.0 xielongwang$ ping uaa-service-host
PING uaa-service (127.0.0.1): 56 data bytes
64 bytes from 127.0.0.1: icmp_seq=0 ttl=64 time=0.147 ms
64 bytes from 127.0.0.1: icmp_seq=1 ttl=64 time=0.249 ms
64 bytes from 127.0.0.1: icmp_seq=2 ttl=64 time=0.269 ms
64 bytes from 127.0.0.1: icmp_seq=3 ttl=64 time=0.109 ms
64 bytes from 127.0.0.1: icmp_seq=4 ttl=64 time=0.232 ms
64 bytes from 127.0.0.1: icmp_seq=5 ttl=64 time=0.258 ms
64 bytes from 127.0.0.1: icmp_seq=6 ttl=64 time=0.371 ms
64 bytes from 127.0.0.1: icmp_seq=7 ttl=64 time=0.239 ms
^C
--- uaa-service ping statistics ---
8 packets transmitted, 8 packets received, 0.0% packet loss
round-trip min/avg/max/stddev = 0.109/0.234/0.371/0.074 ms
localhost:apisix-2.10.0 xielongwang$ dig uaa-service-host
; <<>> DiG 9.10.6 <<>> uaa-service-host
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 55283
;; flags: qr rd ra ad; QUERY: 1, ANSWER: 0, AUTHORITY: 0, ADDITIONAL: 0
;; QUESTION SECTION:
;uaa-service-host. IN A
;; Query time: 86 msec
;; SERVER: 192.168.31.1#53(192.168.31.1)
;; WHEN: Thu Oct 14 20:54:55 CST 2021
;; MSG SIZE rcvd: 34
localhost:apisix-2.10.0 xielongwang$
- 日志打印
2021/10/14 20:49:04 [error] 71097#853898: *2184 [lua] balancer.lua:353: run(): failed to set server peer [uaa-service-host:30101] err: no host allowed while connecting to upstream, client: 127.0.0.1, server: _, request: "GET /uaa/test/hello HTTP/1.1", host: "127.0.0.1:9080"
- 相关 issues
- https://github.com/openresty/lua-resty-core/issues/45
- https://github.com/Kong/kong/issues/4786
At present, the APISIX nacos plug-in does not perform dns resolution on the back-end host it gets (code location), so if the domain name is configured in nacos, it will be directly used as the parameter of balancer.set_current_peer, which will lead to no host allowed While connecting to upstream error.
The configuration node on the Dashboard page will first do dns query and then give balancer.set_current_peer, so it can work. code location
So the error you encountered is in line with expectations, we can further consider whether service discovery should support configuration in the form of domain names.
mark,same problem, register domain as nacos ip.
i have resolved this issue related to eureka. how can i contributor?
i have resolved this issue related to eureka. how can i contributor?
Can you start with a brief chat about how it was solved?
i have resolved this issue related to eureka. how can i contributor?
Can you start with a brief chat about how it was solved?
I parse the file '/etc/hosts' with RegExp in the initial phase,So I can get 'hosts to ip' mapping . then I modify eureka code that get ip (the code is line 132 in /apisix/discovery/eureka/init.lua ), use mapping relations to get ip in high priority , if not, I use the eureka service ip oneself .
I parse the file '/etc/hosts' with RegExp in the initial phase
Did you write the host and ip mappings in /etc/hosts beforehand?
So I can get 'hosts to ip' mapping . then I modify eureka code that get ip (the code is line 132 in /apisix/discovery/eureka/init.lua ), use mapping relations to get ip in high priority , if not, I use the eureka service ip oneself .
I have a suggestion for enhancing nacos/eureka
- determine whether the host is a domain or an ip
- if it's a domain, resolve the domain to an ip, overriding the host
ref: https://github.com/apache/apisix/blob/719c5388143d73cc1424d0a03ec51584a60e243e/apisix/init.lua#L166-L188
Did you write the host and ip mappings in /etc/hosts beforehand?
yeah, i think this is General Situation
I have a suggestion for enhancing nacos/eureka determine whether the host is a domain or an ip
- determine whether the host is a domain or an ip
- if it's a domain, resolve the domain to an ip, overriding the host
nice~ so. i modify the code resolver .parse_domain that can get ip from ' host to ip ' mapping in high priority then use dns_parse ?
Did you write the host and ip mappings in /etc/hosts beforehand?
yeah, i think this is General Situation
I have a suggestion for enhancing nacos/eureka determine whether the host is a domain or an ip
- determine whether the host is a domain or an ip
- if it's a domain, resolve the domain to an ip, overriding the host
nice~ so. i modify the code resolver .parse_domain that can get ip from ' host to ip ' mapping in high priority then use dns_parse ?
It seems that this is a more generic demands that we can use the /etc/hosts to change the DNS resolving.
It seems that this is a more generic demands that we can use the
/etc/hoststo change the DNS resolving.
Our dns resolution process also takes resolution information from /etc/hosts. So the key point of my suggestion is to use the existing dns resolution method, not just /etc/hosts, to resolve the domain name.
nice~ so. i modify the code resolver .parse_domain that can get ip from ' host to ip ' mapping in high priority then use dns_parse ?
just try
I get same error in custom plugin when rewrite upstream. Upstream data:
{
"id": "ups-sys1-abcd",
"create_time": 1643165083,
"update_time": 1643190928,
"nodes": [
{
"host": "example.com",
"port": 80,
"weight": 1
}
],
"type": "roundrobin",
"hash_on": "vars",
"scheme": "http",
"pass_host": "pass",
"name": "ups-sys1-abcd"
}
Route:
{
"id": "1",
"create_time": 1643186095,
"update_time": 1643187397,
"uri": "/get",
"name": "",
"methods": [
"GET"
],
"host": "sys1.com",
"plugins": {
"custom-plugin": {}
},
"upstream": {
"nodes": [
{
"host": "httpbin.org",
"port": 80,
"weight": 1
}
],
"type": "roundrobin",
"hash_on": "vars",
"scheme": "http",
"pass_host": "pass"
},
"status": 1
}
Custom plugin code:
function _M.rewrite(conf, ctx)
-- rewrite by request header
-- get upstream info
local upstreams = core.config.fetch_created_obj("/upstreams")
if not upstreams then
return core.response.exit(502)
end
-- new upstream id by system name and tag
local upstream_id = "ups-sys1-abcd"
local ups_info = upstreams:get(tostring(upstream_id))
if not ups_info then
return
end
ups_info = ups_info.value
upstream.set(ctx, ups_info.name, ctx.conf_version, ups_info)
end
error log:
apisix | 2022/01/26 09:11:48 [error] 48#48: *456684 [lua] balancer.lua:353: run(): failed to set server peer [example.com:80] err: no host allowed while connecting to upstream, client: 172.18.0.1, server: _, request: "GET /get HTTP/1.1", host: "sys1.com"
hi @drrrMikado, the cause and the solution of this problem are provided above. Since it is your own plugin, there is nothing I can do about it.
hi @drrrMikado, the cause and the solution of this problem are provided above. Since it is your own plugin, there is nothing I can do about it.
Ok, Thanks.
2.99.0上也遇到这个问题,目前有修复计划吗
is discovery to get the service address should be judged when it is not the ip, otherwise then resolve the dns/hosts ?