Kong 使用正则表达式 uris 的错误评估顺序
Kong wrong evaluation order with regex uris
这些是我的杠 apis:
{
"total" : 5,
"data" : [
{
"upstream_read_timeout" : 60000,
"retries" : 5,
"strip_uri" : false,
"upstream_connect_timeout" : 60000,
"created_at" : 1515179488893,
"preserve_host" : false,
"https_only" : false,
"upstream_send_timeout" : 60000,
"uris" : [
"/users/\d"
],
"id" : "edbc93f2-e897-490f-a0a5-7fb627eb8748",
"upstream_url" : "http://172.18.0.3:8002",
"http_if_terminated" : false,
"name" : "account"
},
{
"upstream_send_timeout" : 60000,
"id" : "2d76ec3e-df4d-43f1-8bbb-79e613f882a8",
"uris" : [
"/\w"
],
"upstream_url" : "http://172.18.0.3:8002",
"http_if_terminated" : false,
"name" : "login",
"upstream_read_timeout" : 60000,
"strip_uri" : false,
"retries" : 5,
"upstream_connect_timeout" : 60000,
"created_at" : 1515179488936,
"https_only" : false,
"preserve_host" : false
},
{
"name" : "event",
"upstream_url" : "http://172.18.0.10:8004",
"http_if_terminated" : false,
"uris" : [
"/users/\d /calendars/\d /events"
],
"id" : "2f5c90de-dc09-43f3-92d4-7d798b6fd149",
"upstream_send_timeout" : 60000,
"preserve_host" : false,
"https_only" : false,
"retries" : 5,
"strip_uri" : false,
"upstream_connect_timeout" : 60000,
"created_at" : 1515179488801,
"upstream_read_timeout" : 60000
},
{
"preserve_host" : false,
"https_only" : false,
"created_at" : 1515179488610,
"retries" : 5,
"strip_uri" : false,
"upstream_connect_timeout" : 60000,
"upstream_read_timeout" : 60000,
"name" : "instructions",
"http_if_terminated" : false,
"upstream_url" : "http://172.18.0.13:8005",
"uris" : [
"/users/\d /calendars/\d /events/\d /recurrence/\d /instruction"
],
"id" : "4e0462ff-3446-4f2a-9a54-d0e6fb385a2c",
"upstream_send_timeout" : 60000
},
{
"retries" : 5,
"upstream_connect_timeout" : 60000,
"strip_uri" : false,
"created_at" : 1515179488849,
"preserve_host" : false,
"https_only" : false,
"upstream_read_timeout" : 60000,
"upstream_url" : "http://172.18.0.7:8003",
"http_if_terminated" : false,
"name" : "calendar",
"upstream_send_timeout" : 60000,
"uris" : [
"/users/\d /calendars"
],
"id" : "1e12ba7e-571b-4d02-9e70-a6823db0606d"
}
]
}
使用这些命令创建的:
# instructions
curl -i -X POST http://localhost:8001/apis/ \
-d 'name=instructions' \
-d 'upstream_url=http://172.18.0.13:8005' \
-d 'uris=/users/\d+/calendars/\d+/events/\d+/recurrence/\d+/instruction' \
-d 'strip_uri=false'
curl -i -X POST http://localhost:8001/apis/instructions/plugins --data "name=jwt"
# event
curl -i -X POST http://localhost:8001/apis/ \
-d 'name=event' \
-d 'upstream_url=http://172.18.0.10:8004' \
-d 'uris=/users/\d+/calendars/\d+/events' \
-d 'strip_uri=false'
curl -i -X POST http://localhost:8001/apis/event/plugins --data "name=jwt"
# calendar
curl -i -X POST http://localhost:8001/apis/ \
-d 'name=calendar' \
-d 'upstream_url=http://172.18.0.7:8003' \
-d 'uris=/users/\d+/calendars' \
-d 'strip_uri=false'
curl -i -X POST http://localhost:8001/apis/calendar/plugins --data "name=jwt"
# account
curl -i -X POST http://localhost:8001/apis/ \
-d 'name=account' \
-d 'upstream_url=http://172.18.0.3:8002' \
-d 'uris=/users/\d+' \
-d 'strip_uri=false'
curl -i -X POST http://localhost:8001/apis/account/plugins --data "name=jwt"
# login
curl -i -X POST http://localhost:8001/apis/ \
-d 'name=login' \
-d 'upstream_url=http://172.18.0.3:8002' \
-d 'uris=/\w/' \
-d 'strip_uri=false'
如果我尝试访问 /users/3/calendars
,我将被重定向到 account
api(用 uris=/users/\d+
定义),但我期望的是重定向到 calendar
api(用 uris=/users/\d+/calendars
定义)。来自 docs:
As previously mentioned, Kong evaluates prefix URIs by length: the longest prefix URIs are evaluated first. However, Kong will evaluate regex URIs based on the order in which they are defined.
我做错了什么?
感谢您的宝贵时间。
问题出在 curl
命令中,为了在设置 uris 时在请求中使用 +
需要使用 --data-urlencode
而不是 -d
这些是我的杠 apis:
{
"total" : 5,
"data" : [
{
"upstream_read_timeout" : 60000,
"retries" : 5,
"strip_uri" : false,
"upstream_connect_timeout" : 60000,
"created_at" : 1515179488893,
"preserve_host" : false,
"https_only" : false,
"upstream_send_timeout" : 60000,
"uris" : [
"/users/\d"
],
"id" : "edbc93f2-e897-490f-a0a5-7fb627eb8748",
"upstream_url" : "http://172.18.0.3:8002",
"http_if_terminated" : false,
"name" : "account"
},
{
"upstream_send_timeout" : 60000,
"id" : "2d76ec3e-df4d-43f1-8bbb-79e613f882a8",
"uris" : [
"/\w"
],
"upstream_url" : "http://172.18.0.3:8002",
"http_if_terminated" : false,
"name" : "login",
"upstream_read_timeout" : 60000,
"strip_uri" : false,
"retries" : 5,
"upstream_connect_timeout" : 60000,
"created_at" : 1515179488936,
"https_only" : false,
"preserve_host" : false
},
{
"name" : "event",
"upstream_url" : "http://172.18.0.10:8004",
"http_if_terminated" : false,
"uris" : [
"/users/\d /calendars/\d /events"
],
"id" : "2f5c90de-dc09-43f3-92d4-7d798b6fd149",
"upstream_send_timeout" : 60000,
"preserve_host" : false,
"https_only" : false,
"retries" : 5,
"strip_uri" : false,
"upstream_connect_timeout" : 60000,
"created_at" : 1515179488801,
"upstream_read_timeout" : 60000
},
{
"preserve_host" : false,
"https_only" : false,
"created_at" : 1515179488610,
"retries" : 5,
"strip_uri" : false,
"upstream_connect_timeout" : 60000,
"upstream_read_timeout" : 60000,
"name" : "instructions",
"http_if_terminated" : false,
"upstream_url" : "http://172.18.0.13:8005",
"uris" : [
"/users/\d /calendars/\d /events/\d /recurrence/\d /instruction"
],
"id" : "4e0462ff-3446-4f2a-9a54-d0e6fb385a2c",
"upstream_send_timeout" : 60000
},
{
"retries" : 5,
"upstream_connect_timeout" : 60000,
"strip_uri" : false,
"created_at" : 1515179488849,
"preserve_host" : false,
"https_only" : false,
"upstream_read_timeout" : 60000,
"upstream_url" : "http://172.18.0.7:8003",
"http_if_terminated" : false,
"name" : "calendar",
"upstream_send_timeout" : 60000,
"uris" : [
"/users/\d /calendars"
],
"id" : "1e12ba7e-571b-4d02-9e70-a6823db0606d"
}
]
}
使用这些命令创建的:
# instructions
curl -i -X POST http://localhost:8001/apis/ \
-d 'name=instructions' \
-d 'upstream_url=http://172.18.0.13:8005' \
-d 'uris=/users/\d+/calendars/\d+/events/\d+/recurrence/\d+/instruction' \
-d 'strip_uri=false'
curl -i -X POST http://localhost:8001/apis/instructions/plugins --data "name=jwt"
# event
curl -i -X POST http://localhost:8001/apis/ \
-d 'name=event' \
-d 'upstream_url=http://172.18.0.10:8004' \
-d 'uris=/users/\d+/calendars/\d+/events' \
-d 'strip_uri=false'
curl -i -X POST http://localhost:8001/apis/event/plugins --data "name=jwt"
# calendar
curl -i -X POST http://localhost:8001/apis/ \
-d 'name=calendar' \
-d 'upstream_url=http://172.18.0.7:8003' \
-d 'uris=/users/\d+/calendars' \
-d 'strip_uri=false'
curl -i -X POST http://localhost:8001/apis/calendar/plugins --data "name=jwt"
# account
curl -i -X POST http://localhost:8001/apis/ \
-d 'name=account' \
-d 'upstream_url=http://172.18.0.3:8002' \
-d 'uris=/users/\d+' \
-d 'strip_uri=false'
curl -i -X POST http://localhost:8001/apis/account/plugins --data "name=jwt"
# login
curl -i -X POST http://localhost:8001/apis/ \
-d 'name=login' \
-d 'upstream_url=http://172.18.0.3:8002' \
-d 'uris=/\w/' \
-d 'strip_uri=false'
如果我尝试访问 /users/3/calendars
,我将被重定向到 account
api(用 uris=/users/\d+
定义),但我期望的是重定向到 calendar
api(用 uris=/users/\d+/calendars
定义)。来自 docs:
As previously mentioned, Kong evaluates prefix URIs by length: the longest prefix URIs are evaluated first. However, Kong will evaluate regex URIs based on the order in which they are defined.
我做错了什么? 感谢您的宝贵时间。
问题出在 curl
命令中,为了在设置 uris 时在请求中使用 +
需要使用 --data-urlencode
而不是 -d