You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@apisix.apache.org by "jagerzhang (via GitHub)" <gi...@apache.org> on 2024/07/22 09:17:22 UTC
[I] bug: Route matching priority that does not meet expectations [apisix]
jagerzhang opened a new issue, #11421:
URL: https://github.com/apache/apisix/issues/11421
### Current Behavior
## reproduce steps
### config.yml
```yaml
apisix:
node_listen: 9080 # APISIX listening port
enable_ipv6: false
allow_admin: # http://nginx.org/en/docs/http/ngx_http_access_module.html#allow
- 0.0.0.0/0 # We need to restrict ip access rules for security. 0.0.0.0/0 is for test.
router:
http: radixtree_host_uri
admin_key:
- name: "admin"
key: edd1c9f034335f136f87ad84b625c8f1
role: admin # admin: manage all configuration data
# viewer: only can view configuration data
- name: "viewer"
key: 4054f7cf07e344346cd3f287985e76a2
role: viewer
enable_control: true
control:
ip: "0.0.0.0"
port: 9092
nginx_config:
user: root
error_log: /dev/stdout
error_log_level: error
enable_cpu_affinity: false
http:
enable_access_log: false # enable access log or not, default true
etcd:
host: # it's possible to define multiple etcd hosts addresses of the same etcd cluster.
- "http://127.0.0.1:2379" # multiple etcd address
prefix: "/apisix" # apisix configurations prefix
timeout: 30 # 30 seconds
plugin_attr:
prometheus:
export_addr:
ip: "0.0.0.0"
port: 9091
```
### create upstream
```shell
curl -X PUT http://127.0.0.1:9080/apisix/admin/upstreams/test -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -d '{
"nodes": [
{
"host": "127.0.0.1",
"port": 8000,
"weight": 100
}
],
"timeout": {
"connect": 6,
"send": 60,
"read": 60
},
"type": "roundrobin",
"scheme": "http",
"pass_host": "pass"
}'
```
### create service
```shell
curl -X PUT http://127.0.0.1:9080/apisix/admin/services/test -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -d'{
"upstream_id": "test",
"hosts": ["test1.com", "test2.com"]
}'
```
### create route1 without hosts
```shell
curl -X PUT http://127.0.0.1:9080/apisix/admin/routes/test1 -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -d '{
"uris": [
"/test*"
],
"name": "test1",
"methods": [
"GET"
],
"plugins": {
"echo": {
"body": "test1\n"
}
},
"service_id": "test",
"status": 1
}'
```
### create route2 with hosts
```shell
curl -X PUT http://127.0.0.1:9080/apisix/admin/routes/test2 -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -d '{
"uris": [
"/test*"
],
"name": "test2",
"methods": [
"GET"
],
"plugins": {
"echo": {
"body": "test2\n"
}
},
"hosts": ["test2.com"],
"service_id": "test",
"status": 1
}'
```
### request test
```shell
[root@centos test]# curl http://127.0.0.1:9080/test -H "host: test1.com"
test1
[root@centos test]# curl http://127.0.0.1:9080/test -H "host: test2.com"
test1
```
## In `radixtree_host_uri` mode, why `test2.com` match the route `test1` rather than the route test2 ?
### Expected Behavior
`test2.com` is bound to `route2` and should be prioritized for matching to `route2`
### Error Logs
_No response_
### Steps to Reproduce
Run With apache/apisix:2.15.0-centos Docker image.
### Environment
- APISIX version (run `apisix version`): 2.15 Docker centos
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [I] bug: Route matching priority that does not meet expectations [apisix]
Posted by "zhoujiexiong (via GitHub)" <gi...@apache.org>.
zhoujiexiong commented on issue #11421:
URL: https://github.com/apache/apisix/issues/11421#issuecomment-2268076059
@jagerzhang
Welcome :D
> @zhoujiexiong thanks a lot
>
> It seems I have misunderstood. I have always thought that the domain name in the service has a lower priority than the domain name in the route, and only serves as a default. Now it seems that the domain name in the service will eventually be merged into all routes for match calculation.
>
> What's strange is that this issue only appears now. We have always thought this way before, and it also meets our expectations. However, the usage situation in our production environment is indeed quite complex, so we won't delve into it.
@jagerzhang
Welcome :D
`Now it seems that the domain name in the service will eventually be merged into all routes for match calculation.'
Not merge, but prioritize to route.host[s].
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [I] bug: Route matching priority that does not meet expectations [apisix]
Posted by "zhoujiexiong (via GitHub)" <gi...@apache.org>.
zhoujiexiong commented on issue #11421:
URL: https://github.com/apache/apisix/issues/11421#issuecomment-2268080509
@jagerzhang
So, our conclusion is `If multiple routes are confused, adjust the priorities of them.` ?
And shall we close this issue?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [I] bug: Route matching priority that does not meet expectations [apisix]
Posted by "jagerzhang (via GitHub)" <gi...@apache.org>.
jagerzhang closed issue #11421: bug: Route matching priority that does not meet expectations
URL: https://github.com/apache/apisix/issues/11421
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [I] bug: Route matching priority that does not meet expectations [apisix]
Posted by "jagerzhang (via GitHub)" <gi...@apache.org>.
jagerzhang commented on issue #11421:
URL: https://github.com/apache/apisix/issues/11421#issuecomment-2264937575
@zhoujiexiong
Thank you for your response! I noticed that your previous replies all involve routes directly bound to a host. However, our practice has always been to use a service to bind the host. In certain situations, to distinguish requests, we pick a host from within the service to bind to a specific route.

Our expectation is that a request to this host+URI should hit the route that's bound to the host, not a route that isn't bound to any host. Historically, the behavior of our gateway has been in line with this expectation. When a request is made to a specific host+URI, it prioritizes the route that's bound to that host.
The problem you've described only started appearing recently. We haven't made any version updates in the meantime, which leaves us puzzled as to why this discrepancy has suddenly arisen.
Based on my past experience, a host bound on the service is considered a default setting. If a route binds to a host, then a request to that host should prioritize hitting that route. I would greatly appreciate any insights or clarifications from the experts on this issue.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [I] bug: Route matching priority that does not meet expectations [apisix]
Posted by "jagerzhang (via GitHub)" <gi...@apache.org>.
jagerzhang commented on issue #11421:
URL: https://github.com/apache/apisix/issues/11421#issuecomment-2268039539
@zhoujiexiong thanks a lot
It seems I have misunderstood. I have always thought that the domain name in the service has a lower priority than the domain name in the route, and only serves as a default. Now it seems that the domain name in the service will eventually be merged into all routes for match calculation.
What's strange is that this issue only appears now. We have always thought this way before, and it also meets our expectations. However, the usage situation in our production environment is indeed quite complex, so we won't delve into it.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [I] bug: Route matching priority that does not meet expectations [apisix]
Posted by "zhoujiexiong (via GitHub)" <gi...@apache.org>.
zhoujiexiong commented on issue #11421:
URL: https://github.com/apache/apisix/issues/11421#issuecomment-2264689320
@jagerzhang
This dynamic way is also behaving like the static.
You could adjust the priority of the routes to satisfy.
```
use t::APISIX 'no_plan';
repeat_each(1);
no_long_string();
no_shuffle();
no_root_location();
run_tests;
__DATA__
=== TEST 1: route_test1 set 1st, pri_test1 > pri_test2; route_test2(set pri = 100), pri_test2 > pri_test1
--- config
location /t {
content_by_lua_block {
local t = require("lib.test_admin").test
local code, body = t('/apisix/admin/upstreams/test',
ngx.HTTP_PUT,
[[{
"nodes": {
"localhost:1980": 1
},
"type": "roundrobin"
}]]
)
assert(body == "passed")
local code, body = t('/apisix/admin/services/test',
ngx.HTTP_PUT,
[[{
"upstream_id": "test",
"hosts": ["test1.com", "test2.com"]
}]]
)
assert(body == "passed")
local code, body = t('/apisix/admin/routes/test1',
ngx.HTTP_PUT,
[[{
"uris": ["/111*"],
"plugins": {
"echo": {
"body": "test1"
}
},
"service_id": "test"
}]]
)
assert(body == "passed")
local code, body = t('/apisix/admin/routes/test2',
ngx.HTTP_PUT,
[[{
"uris": ["/111*"],
"hosts": ["test2.com"],
"plugins": {
"echo": {
"body": "test2"
}
},
"upstream_id": "test"
}]]
)
assert(body == "passed")
local code, body = t('/apisix/admin/routes/test3',
ngx.HTTP_PUT,
[[{
"plugins": {
"serverless-pre-function": {
"phase": "rewrite",
"functions": [
"return function (conf, ctx) ngx.say(\"test3\"); ngx.exit(200) end"
]
}
},
"uri": "/111*"
}]]
)
assert(body == "passed")
ngx.sleep(3)
local http = require "resty.http"
local httpc = http.new()
local uri1 = "http://127.0.0.1:" .. ngx.var.server_port .. "/111"
local res, err = httpc:request_uri(uri1, { method = "GET", headers = { Host = "test2.com" }})
if err then
ngx.log(ngx.ERR, err)
ngx.status = res.status
return
end
ngx.say(res.body)
local code, body = t('/apisix/admin/routes/test2',
ngx.HTTP_PUT,
[[{
"uris": ["/111*"],
"hosts": ["test2.com"],
"plugins": {
"echo": {
"body": "test2"
}
},
"priority": 100,
"upstream_id": "test"
}]]
)
assert(body == "passed")
ngx.sleep(3)
local http = require "resty.http"
local httpc = http.new()
local uri1 = "http://127.0.0.1:" .. ngx.var.server_port .. "/111"
local res, err = httpc:request_uri(uri1, { method = "GET", headers = { Host = "test2.com" }})
if err then
ngx.log(ngx.ERR, err)
ngx.status = res.status
return
end
ngx.say(res.body)
}
}
--- request
GET /t
--- response_body
test1
test2
--- timeout: 30
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [I] bug: Route matching priority that does not meet expectations [apisix]
Posted by "zhoujiexiong (via GitHub)" <gi...@apache.org>.
zhoujiexiong commented on issue #11421:
URL: https://github.com/apache/apisix/issues/11421#issuecomment-2265001262
> @zhoujiexiong Thanks for your reply! I noticed that your previous replies all involve routes directly bound to a host. However, our practice has always been to use a service to bind the host. In certain situations, to distinguish requests, we pick a host from within the service to bind to a specific route.
>
> 
>
> According to experience, more specific conditions in route should be matched first, but it'snt.
>
> Our expectation is that a request to this host+URI should hit the route that's bound to the host, not a route that isn't bound to any host. Historically, the behavior of our gateway has been in line with this expectation. When a request is made to a specific host+URI, it prioritizes the route that's bound to that host.
>
> The problem only started appearing recently. We haven't made any version updates in the meantime, which leaves us puzzled as to why this discrepancy has suddenly arisen.
>
> Based on my past experience, a host bound on the service is considered a default setting. If a route binds to a host, then a request to that host should prioritize hitting that route.
>
> I would greatly appreciate any insights or clarifications from the experts on this issue.
@jagerzhang
In my reproduction test, `route_test1` is also ref. host by service.
The logic of the test should be identical to the reproduction steps you provided.
<img width="838" alt="image" src="https://github.com/user-attachments/assets/b1422285-f9ff-4668-b44a-1d200096a899">
BTW, about the `priority`
If a route has `host[s]` field and ref. `service` that also has `host[s]` field, the directly `host[s]` of the route will take effect.
Notice that it's in ONE route, but we are using TWO.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [I] bug: Route matching priority that does not meet expectations [apisix]
Posted by "zhoujiexiong (via GitHub)" <gi...@apache.org>.
zhoujiexiong commented on issue #11421:
URL: https://github.com/apache/apisix/issues/11421#issuecomment-2277101353
> > @jagerzhang
> > Welcome :D
> > > @zhoujiexiong thanks a lot
> > > It seems I have misunderstood. I have always thought that the domain name in the service has a lower priority than the domain name in the route, and only serves as a default. Now it seems that the domain name in the service will eventually be merged into all routes for match calculation.
> > > What's strange is that this issue only appears now. We have always thought this way before, and it also meets our expectations. However, the usage situation in our production environment is indeed quite complex, so we won't delve into it.
> >
> >
> > @jagerzhang
> > Welcome :D
> > `Now it seems that the domain name in the service will eventually be merged into all routes for match calculation.` Not merge, but prioritize to route.host[s].
>
> if `Not merge, but prioritize to route.host[s]`, why the route `test1` without host matched first? It stands to reason that the matching conditions of the routes bound to the domain name are more specific and should be matched first.
>
> If the community is sure that's the design, then close the case. I'm just confused about the mechanism.
>
> thanks!
@jagerzhang
- `route_test1` ref. `hosts` through `"service_id": "test"`, so it would also match hosts `test1.com and test2.com`.
- `Not Merge` means, for example, `route_test2`, it has `hosts` field and ref. service that also has `hosts` field, then `route_test2.hosts` would **override** `service.hosts`.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [I] bug: Route matching priority that does not meet expectations [apisix]
Posted by "jagerzhang (via GitHub)" <gi...@apache.org>.
jagerzhang commented on issue #11421:
URL: https://github.com/apache/apisix/issues/11421#issuecomment-2277037009
> @jagerzhang
>
> Welcome :D
>
> > @zhoujiexiong thanks a lot
> > It seems I have misunderstood. I have always thought that the domain name in the service has a lower priority than the domain name in the route, and only serves as a default. Now it seems that the domain name in the service will eventually be merged into all routes for match calculation.
> > What's strange is that this issue only appears now. We have always thought this way before, and it also meets our expectations. However, the usage situation in our production environment is indeed quite complex, so we won't delve into it.
>
> @jagerzhang
>
> Welcome :D
>
> `Now it seems that the domain name in the service will eventually be merged into all routes for match calculation.` Not merge, but prioritize to route.host[s].
if `Not merge, but prioritize to route.host[s]`, why the route `test1` without host matched first? It stands to reason that the matching conditions of the routes bound to the domain name are more specific and should be matched first.
If the community is sure that's the design, then close the case. I'm just confused about the mechanism.
thanks!
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [I] bug: Route matching priority that does not meet expectations [apisix]
Posted by "zhoujiexiong (via GitHub)" <gi...@apache.org>.
zhoujiexiong commented on issue #11421:
URL: https://github.com/apache/apisix/issues/11421#issuecomment-2264361342
@jagerzhang
`route_test1` also associate with `host test2.com`, so if `route_test1` set 1st, it has the higher priority(static config with `apisix.yaml` behave like this, I will confirm dynamic way using admin API later).
<img width="838" alt="image" src="https://github.com/user-attachments/assets/c6fa7f3d-ee24-4cb8-b3c7-030a2269b075">
```
routes:
- uri: /test
hosts: ["test1.com", "test2.com"]
plugins:
serverless-pre-function:
phase: rewrite
functions:
- "return function (conf, ctx) ngx.say(\"test1\"); ngx.exit(200) end"
- uri: /test
hosts: ["test2.com"]
plugins:
serverless-pre-function:
phase: rewrite
functions:
- "return function (conf, ctx) ngx.say(\"test2\"); ngx.exit(200) end"
```
```
curl 127.0.0.1:9080/test -H "host: test2.com"
test1
```
```
routes:
- uri: /test
hosts: ["test2.com"]
plugins:
serverless-pre-function:
phase: rewrite
functions:
- "return function (conf, ctx) ngx.say(\"test2\"); ngx.exit(200) end"
- uri: /test
hosts: ["test1.com", "test2.com"]
plugins:
serverless-pre-function:
phase: rewrite
functions:
- "return function (conf, ctx) ngx.say(\"test1\"); ngx.exit(200) end"
```
```
curl 127.0.0.1:9080/test -H "host: test2.com"
test2
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org