我创建了一个有效的 API 网关。https://example-api.amazonaws.com/Prod/some/endpoint
如果我通过API手动触发端点会给我想要的结果。此外,我还设置了一个云端行为,以在路径包含/api/*
.
如果我现在通过云端分发访问端点: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
我可以看到云端分发工作正常,因为它重定向正确。我已经为此苦苦挣扎了一段时间。这个错误代码是否有帮助?
在进行了更多挖掘之后,似乎云端分布正在我的 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
但是,奇怪的部分仍然存在于 cloudfront 发行版附加的/prod
. 看起来好像 pathPattern 正确路由到 API Gateway。
编辑:目前认为我可能缺少 API 网关的标头。