简体   繁体   English

aws cloudfront api 端点响应 Forbidden(403)

[英]aws cloudfront api endpoint responding with Forbidden(403)

I have a lambda function behind an api gateway.我在 api 网关后面有一个 lambda 函数。 This is working fine when triggered with an Invoke URL .这在使用Invoke URL触发时工作正常。 Invoke URL is the link offered in method tab under stages.调用 URL 是阶段下方法选项卡中提供的链接。

在此处输入图片说明

I am sending request to the resource as below, and this works fine.我正在向资源发送请求,如下所示,这很好用。 在此处输入图片说明

I want to set a custom domain for my api so I followed the standard procedure to set an ACM certificate.我想为我的 api 设置一个自定义域,所以我按照标准程序设置了 ACM 证书。 In addition to that I did set domain (api.tarkshala.com) in route53.除此之外,我确实在 route53 中设置了域(api.tarkshala.com)。 Check screenshots given below.检查下面给出的屏幕截图。

ACM certificate imported导入ACM证书在此处输入图片说明

Custom domain mapping自定义域映射在此处输入图片说明

Domain name mapped in route53 route53中映射的域名在此处输入图片说明

But when I hit the API using cloudfront link(d3pn2j4magp6tp.cloudfront.net) or the domain name (api.tarkshala.com) using POSTMAN, it ends up into 403 Forbidden .但是,当我使用 cloudfront 链接(d3pn2j4magp6tp.cloudfront.net)或使用 POSTMAN 的域名(api.tarkshala.com)访问 API 时,它最终变成了403 Forbidden

I don't know what exactly am I missing?我不知道我到底错过了什么?

Response Header looks as following:响应头如下所示:

Connection →keep-alive
Content-Length →23
Content-Type →application/json
Date →Fri, 13 Apr 2018 03:17:25 GMT
Via →1.1 a1cf0e6cf29b584b5fe1ada9ccee9758.cloudfront.net (CloudFront)
X-Amz-Cf-Id →9Jh5HeQIuDNfm5WGlTae34oYZ7BiN3nI2VlH_8PJHLQ0mr2C20njJQ==
X-Cache →Error from cloudfront
x-amz-apigw-id →FQoiyF1phcwFVrg=
x-amzn-ErrorType →ForbiddenException
x-amzn-RequestId →3013c239-3ec9-11e8-abe6-53449bcfc96c

Response:回复:

{
    "message": "Forbidden"
}

The documentation doesn't seem to mention an important aspect of the configuration of the CloudFront distribution associated with an API Gateway endpoint... it seems it can only be inferred: 该文档似乎并未提及与API网关端点相关联的CloudFront发行版配置的重要方面……似乎只能推断出:

When you create a custom domain name for an edge-optimized API, API Gateway sets up a CloudFront distribution. 在为边缘优化的API创建自定义域名时,API Gateway会设置CloudFront分配。 But you must set up a DNS record to map the custom domain name to the CloudFront distribution domain name [in order] for API requests bound for the custom domain name to be [correctly] routed to API Gateway through the mapped CloudFront distribution [and understood when they arrive] . 但是, 您必须设置DNS记录,以将自定义域名映射到CloudFront分发域名(以按顺序),以便将绑定到自定义域名的API请求通过映射的CloudFront分布[正确地]路由到API网关。当他们到达时] You must also provide a certificate for the custom domain name. 您还必须提供自定义域名的证书。 (emphasis added, bracketed words are mine, added for clarity) (添加了重点,括号中的单词是我的,为清楚起见添加了)

https://docs.aws.amazon.com/apigateway/latest/developerguide/how-to-custom-domains.html https://docs.aws.amazon.com/apigateway/latest/developerguide/how-to-custom-domains.html

Internally, CloudFront distributions have two ways of handling the Host header that arrives on the incoming request -- CloudFront can hand over the Host header submitted by the browser, if it is on a list of expected values, or can always pass a single static value to the back-end origin that it infers from the origin configuration. 在内部,CloudFront分发有两种方式处理传入请求中到达的Host标头-CloudFront可以移交浏览器提交的Host标头(如果它位于期望值列表中),或者始终可以传递单个静态值从原始配置推断出的后端原始。

For the CloudFront distributions managed by API Gateway, the configuration is apparently to pass through what the browser sends, which makes a good deal of sense because the other alternative would require API Gateway to engage in additional manipulation of the incoming request that would be convoluted and complicated, to say the very least. 对于由API网关管理的CloudFront发行版,该配置显然可以通过浏览器发送的内容,这很有意义,因为另一种选择将要求API网关对传入的请求进行额外的处理,这些请求将被卷积并至少可以说,它很复杂。

Thus, to test a custom domain name on an API Gateway deployment, the requester must include the custom domain name as the HTTP Host header. 因此,要在API Gateway部署上测试自定义域名,请求者必须包括该自定义域名作为HTTP Host标头。 The cloudfront.net alias target hostname is used only for DNS mapping -- it isn't an alternate hostname that API Gateway actually associates with your deployed stage when processing incoming requests. cloudfront.net别名目标主机名用于DNS映射-它不是API Gateway在处理传入请求时实际上与您部署的阶段关联的备用主机名。

If your custom domain name is pointed to the "target" domain name in DNS, and you access the API via the custom domain, this all happens automatically. 如果您的自定义域名指向DNS中的“目标”域名,并且您通过自定义域访问API,则所有操作都会自动发生。 Otherwise, you can set the Host header manually, for testing, if the tool you are using will allow it. 否则,您可以手动设置Host标头,以进行测试(如果您使用的工具允许的话)。

In my case Legacy cache settings headers was the issue.在我的情况下,遗留缓存设置标头是问题所在。

If you have selected Legacy cache settings in cloud-front behaviour In cloud-front distribution under behaviour-> Legacy cache settings-> Header drop down如果您在云前端行为中选择了传统缓存设置在云前端分发中的行为-> 传统缓存设置-> 标题下拉菜单

After selecting value as None solved my problem选择值为 None 后解决了我的问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM