amazon-web-services - API 网关缓存与 CloudFront

标签 amazon-web-services amazon-cloudfront aws-api-gateway api-gateway

我对 API Gateway 和 CloudFront 如何协同工作有点困惑。最终,我希望能够将自定义 header 和值视为我的缓存键的一部分。我知道这可以通过白名单来完成(如果我使用的是 CloudFront)。

所以当我提出以下请求时:

GET /pagesRead/4
Some-Header: fizz

例如,这会返回“29 页”

然后有一篇文章将 id 4 更新为“45 页”

如果我提出这个请求

GET /pagesRead/4
Some-Header: buzz

现在将返回“45 页”

但我使用的是 API Gateway,它显然在幕后拥有自己的 CloudFront。有没有办法将 API Gateway 配置为使用其“幕后”CloudFront 将我的自定义 header 列入白名单?这还需要做吗?

根据本文档:AWS-API-Gatway ,看来我只需在 API Gateway 中启用 API 缓存,它就会将我的 header 视为缓存键的一部分。

我的理解正确吗?如果我想要的只是让我的 header 成为缓存键的一部分,那么 API Gateway 中的“启用 API 缓存” 与在顶部添加 CloudFront 实例有什么区别API Gateway 和 CloudFront 中的白名单?

更新:

我在 API 网关中添加了如下 header : enter image description here

但是在 GET 上,我从缓存中获取过时的数据。

GET /pagesRead/4 test-header: buzz

最佳答案

区别在于 API Gateway 实际上并不使用 CloudFront 缓存。 CloudFront 确实为所有 API Gateway API 边缘优化的 API 端点 1 提供了一些前端服务,但基于以下内容,缓存似乎不是其中之一:

API Gateway enables caching by creating a dedicated cache instance.

...还有...

You should not use the X-Cache header from the CloudFront response to determine if your API is being served from your API Gateway cache instance.

https://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-caching.html

可以在您创建的 CloudFront 分配后面级联边缘优化 API 网关端点,但这并非没有某些不便。由于您要经过更多系统,因此延迟会有所增加。根据该配置,CloudFront-Is-*-ViewerCloudFront-Viewer-Country header 以及客户端 IP 的任何概念都可能无效,因为 API 网关部署将看到其前面的附加 CloudFront 分配的属性,而不是真实客户端的属性。 X-Forwarded-For 仍然是正确的,但必须小心处理,因为它将包含一个必须正确处理的额外跃点。

对于希望将 API Gateway 置于您自己的 CloudFront 发行版后面的应用程序,请使用新的区域终端节点之一来部署您的 API 阶段。

it will consider my headers as part of the cache key.

您确实必须根据您引用的文档显式配置缓存键,但是,API Gateway 缓存将根据该 header 的值以及缓存键中的其他属性来缓存响应。

<小时/>

1 边缘优化端点。 API网关现在有two different kinds of endpoints 。原始设计现在称为边缘优化,新选项称为区域。区域终端节点不使用 CloudFront 的前端服务,并且在从同一 AWS 区域内的 EC2 访问时可以提供较低的延迟。当新的区域功能推出时,所有现有端点都被归类为边缘优化。对于区域端点,CloudFront-* header 不会出现在请求中,除非您使用自己的 CloudFront 分配并将这些 header 列入白名单以转发到源。

关于amazon-web-services - API 网关缓存与 CloudFront,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46490508/

相关文章:

node.js - Azure DevOp - 指定已弃用的运行时 (nodejs12.x)

javascript - 在我使用 cloudformation 创建的 aws 账户上找不到堆栈

amazon-s3 - 使用 cloudfront 在 S3 上流式传输服务器端加密视频

html - 从 CDN (Amazon Cloudfront) 引用样式表时出错

json - 有没有办法将请求的 header 和正文传递到 aws 步骤函数管道?

amazon-web-services - AWS Athena 使用 BOTO3 截断表

amazon-web-services - 如何在 "s3 sync"中包含空文件夹?

amazon-web-services - 我可以在 AWS 中将弹性 IP 与 Cloudfront 结合使用吗?

amazon-web-services - 如何在 AWS 中启用 swagger UI

amazon-web-services - 如何打开对 AWS API Gateway 资源的匿名访问