API Gateway 如何評估路由規則的範例 - Amazon API Gateway

API Gateway 如何評估路由規則的範例

下節說明 API Gateway 評估路由規則和 API 映射的四個範例。

範例 1:僅限路由規則

在此範例中,自訂網域名稱 https://petstore.example.com 的路由模式設定為 ROUTING_RULE_ONLY,且具有下列路由規則和優先順序。

規則 ID 優先順序 條件 動作

abc123

10

如果請求包含標頭:Hello:World

目標 API 1

zzz000

50

如果請求包含標頭:Accept:image/webpPet:Dog-*,且如果基本路徑包含 PetStoreShopper

目標 API 2

efg456

100

目標 API 3

下表顯示 API Gateway 如何將上述路由規則套用至範例請求。

要求 選取的 API 說明

https://petstore.example.com -h "Hello:World"

目標 API 1

請求符合路由規則 abc123

https://petstore.example.com/PetStoreShopper -h "Hello:World", "Pet:Dog-Bella", "Accept:image/webp"

目標 API 1

API Gateway 會依優先順序評估所有路由規則。路由規則 abc123 具有第一優先順序且條件相符,因此 API Gateway 會調用目標 API 1。

雖然請求的條件也符合路由規則 zzz000,但 API Gateway 找到相符項目後,就不會評估任何其他路由規則。

https://petstore.example.com/PetStoreShopper -h "Pet:Dog-Bella", "Accept:image/webp"

目標 API 2

請求符合路由規則 zzz000。這是相符項目,因為 Pet:Dog-BellaPet:Dog-* 的字串相符項目

https://petstore.example.com/PetStoreShopper -h "Pet:Dog-Bella"

目標 API 3

請求不符合路由規則 abc123。請求不符合路由規則 zzz000,因為所有必要的標頭都不存在。下一個優先順序規則符合所有傳入的請求,因此 API Gateway 會調用目標 API 3。

範例 2:路由規則和 API 映射

在此範例中,自訂網域名稱 https://petstore.diagram.example.com 的路由模式設定為 ROUTING_RULE_THEN_API_MAPPING,且具有下列路由規則和 API 映射。

規則 ID 優先順序 條件 動作

abc123

1

如果請求包含 pets

調用 PetStore API 的 Prod 階段。

000zzz

5

如果請求包含標頭:Cookie:*ux=beta*,且如果基本路徑包含 /refunds

調用 Refunds API 的 Beta 階段。

下表顯示 https://petstore.backup.example.com 的 API 映射。

API 映射 選取的 API

/refunds

調用 Refunds API 的 Prod 階段。

(none)

調用 Search API 的 Prod 階段。

下圖顯示 API Gateway 如何將上述路由規則和 API 映射套用至範例請求。此圖後面的表中摘要整理了範例請求。

API Gateway 如何套用上述路由規則和 API 映射的圖。

下表顯示 API Gateway 如何將上述路由規則和 API 映射套用至範例請求。

要求 選取的 API 說明

https://petstore.diagram.com/pets

PetStore API 的 Prod 階段。

請求符合路由規則 abc123

https://petstore.diagram.example.com/refunds -h "Cookie:lang=en-us;ux=beta"

Refunds API 的 Beta 階段。

請求符合路由規則 000zzzCookie 標頭包含此條件的正確 *contains* 相符項目和基本路徑相符項目。

https://petstore.diagram.example.com/refunds

Refunds API 的 Prod 階段。

請求沒有比對路由規則 zzz000 所需的標頭。如果 API Gateway 無法成功比對路由規則,則會回復為 API 映射。API Gateway 可以將基本路徑對應至 Refunds API 的 Prod 階段。

https://petstore.diagram.example.com/

Search API 的 Prod 階段。

請求符合空白路徑 (none) 的 API 映射。

範例 3:具有多層的路由規則和 API 映射

在此範例中,自訂網域名稱 https://petstore.backup.example.com 的路由模式設定為 ROUTING_RULE_THEN_API_MAPPING,且具有下列路由規則和 API 映射。

下表顯示 https://petstore.backup.example.com 的路由規則。

規則 ID 優先順序 條件 動作

abc123

10

如果請求包含標頭:Hello:World

目標 API 1

000zzz

50

如果請求包含標頭:Accept:image/webpPet:Dog-*,且如果基本路徑包含 PetStoreShopper

目標 API 2

下表顯示 https://petstore.backup.example.com 的 API 映射。

API 映射 選取的 API

PetStoreShopper

目標 API 3

PetStoreShopper/cats

目標 API 4

下表顯示 API Gateway 如何將上述路由規則和 API 映射套用至範例請求。

要求 選取的 API 說明

https://petstore.example.com/PetStoreShopper -h "Accept:image/webp", "Pet:Cats"

目標 API 3

請求沒有比對路由規則 zzz000 所需的標頭。如果 API Gateway 無法成功比對路由規則,則會回復為 API 映射。API Gateway 可以將基本路徑對應至目標 API 3。

https://petstore.example.com/PetStoreShopper/cats -h "Hello:World"

目標 API 1

請求符合路由規則 abc123。如果路由模式設定為 ROUTING_RULE_THEN_API_MAPPING,則路由規則一律優先於 API 映射。

https://petstore.example.com/Admin -h "Pet:Dog-Bella"

請求不符合任何路由規則或 API 映射。由於沒有預設的路由規則,因此 API Gateway 會拒絕呼叫並傳送 403 Forbidden 狀態碼給呼叫方。

範例 4:萬用字元網域名稱的路由規則

在此範例中,自訂網域名稱 https://*.example.com 是萬用字元網域名稱。萬用字元支援所有路由回相同網域的子網域。下列範例路由規則會變更此行為,以允許子網域使用 Host 標頭路由傳送至不同的目標 API。

下表顯示 https://*.example.com 的路由規則。

規則 ID 優先順序 條件 動作

abc123

10

如果請求包含標頭:Host:a.example.com

目標 API 1

000zzz

50

如果請求包含標頭:Host:b.example.com

目標 API 2

efg456

500

目標 API 3

下表顯示 API Gateway 如何將上述路由規則套用至範例請求。

要求 選取的 API 說明

https://a.example.com

目標 API 1

Host 標頭為 a.example.com。此請求符合路由規則 abc123

https://b.example.com

目標 API 2

Host 標頭為 b.example.com。此請求符合路由規則 000zzz

https://testing.example.com

目標 API 3

這會符合全部擷取路由規則 efg456