

# 有关 API Gateway 如何评估路由规则的示例
<a name="rest-api-routing-rules-examples"></a>

下一节显示了有关 API Gateway 如何评估路由规则和 API 映射的四个示例。

## 示例 1：仅路由规则
<a name="rest-api-routing-rules-examples-rule-only"></a>

在此示例中，自定义域名 `https://petstore.example.com` 的路由模式设置为 `ROUTING_RULE_ONLY`，并具有以下路由规则和优先级。


|  规则 ID  |  优先级  |  条件  |  操作  | 
| --- | --- | --- | --- | 
|  `abc123`  |   10   |   如果请求包含标头：`Hello:World`  |   目标 API 1   | 
|  `zzz000`  |   50   |   如果请求包含标头：`Accept:image/webp` 和 `Pet: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-Bella` 是 `Pet:Dog-*` 的字符串匹配  | 
|  `https://petstore.example.com/PetStoreShopper -h "Pet:Dog-Bella"`  |  目标 API 3  |  请求与路由规则 `abc123` 不匹配。请求与路由规则 `zzz000` 不匹配，因为所有必需的标头都不存在。下一个优先级规则匹配所有传入的请求，因此 API Gateway 调用目标 API 3。  | 

## 示例 2：路由规则和 API 映射
<a name="rest-api-routing-rules-examples-rule-and-mappings"></a>

在此示例中，自定义域名 `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 映射的示意图。\]](http://docs.aws.amazon.com/zh_cn/apigateway/latest/developerguide/images/rr-diagram.png)


下表说明了 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` 阶段。  |  请求与路由规则 `000zzz` 匹配。`Cookie` 标头包含此条件的正确 `*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` 阶段。  |  该请求将 API 映射与空路径 `(none)` 匹配。  | 

## 示例 3：具有多个级别的路由规则和 API 映射
<a name="rest-api-routing-rules-examples-rule-and-mappings-with-multiple-levels"></a>

在此示例中，自定义域名 `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/webp` 和 `Pet: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：通配符域名的路由规则
<a name="rest-api-routing-rules-examples-rule-for-wildcard-domains"></a>

在此示例中，自定义域名 `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` 匹配。  | 