如何解决Cloudfront发行版定向到S3而不是API网关,从而产生404错误NoSuchKey
我创建了一个有效的API网关。如果我通过https://example-api.amazonaws.com/Prod/some/endpoint
手动触发端点,则API会给我所需的结果。另外,我还设置了一个Cloudfront行为,以在路径包含/api/*
时路由到该端点。
如果我现在通过cloudfront发行版访问端点:https://xxxxxxx.cloudfront.net/some/endpoint
,则会显示404错误。 Cloudfront会将/ prod正确地附加到路径上,该路径告诉我至少某些东西在起作用。
更具体地说,它给了我
404 Not Found
Code: NoSuchKey
Message: The specified key does not exist.
An Error Occurred While Attempting to Retrieve a Custom Error Document
我可以看到cloudfront分发正常运行,因为它可以正确重定向。我已经为此苦了一段时间了。此错误代码是否模棱两可以有所帮助?
进行更多挖掘后,似乎cloudfront发行版正在我的s3中寻找带有密钥的对象。因此出于某些原因,PathPattern: /api/v1/*
不会重定向到APIOrigin
。
我在下面附加了Cloudformation模板:
Resources:
WebsiteCloudfront:
Type: AWS::CloudFront::Distribution
Properties:
DistributionConfig:
Comment: Cloudfront Distribution pointing to S3 bucket and API Gateway.
Origins:
- Id: S3Origin
DomainName: bucket-for-static-files.amazonaws.com
CustomOriginConfig:
HTTPPort: 80
HTTPSPort: 443
OriginProtocolPolicy: http-only
- Id: APIOrigin
DomainName: example.api.endpoint.amazonaws.com
OriginPath: /prod
CustomOriginConfig:
# HTTPPort: 80
HTTPSPort: 443
OriginProtocolPolicy: https-only
OriginSSLProtocols:
- TLSv1.2
Enabled: true
HttpVersion: 'http2'
DefaultRootObject: index.html
Aliases:
- !Ref DomainName
DefaultCacheBehavior:
AllowedMethods:
- DELETE
- GET
- HEAD
- OPTIONS
- PATCH
- POST
- PUT
Compress: true
# Logging: Logging
TargetOriginId: S3Origin
ForwardedValues:
QueryString: true
Cookies:
Forward: none
ViewerProtocolPolicy: redirect-to-https
CacheBehaviors:
- TargetOriginId: APIOrigin
ViewerProtocolPolicy: https-only
PathPattern: /api/v1/*
ForwardedValues:
QueryString: true
Headers:
- Host
AllowedMethods:
- DELETE
- GET
- HEAD
- OPTIONS
- PATCH
- POST
- PUT
PriceClass: PriceClass_All
ViewerCertificate:
AcmCertificateArn: arn:aws:acm:SOME_HARD_CODED_ARN
SslSupportMethod: sni-only
MinimumProtocolVersion: TLSv1.2_2019
但是,奇怪的部分仍然存在,即云阵面分布附加了/prod
。使得pathPattern似乎正确路由到API网关。
编辑:当前认为我可能缺少API网关标头。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。