如何使用无服务器在 CloudFront 中启用 HTTP/2 支持?

How to enable HTTP/2 support in CloudFront using serverless?

如何使用无服务器在 CloudFront 中启用 HTTP/2 支持?

我正在使用无服务器 (https://github.com/serverless/serverless) 创建一个简单的无服务器应用程序,该应用程序使用 CloudFront 从 S3 容器提供 HTML + JS。

为此,我正在编写一个 serverless.yml 模板,之后将为我构建此设置。

我需要在模板中 include/configure 什么才能在 CloudFront 中启用 HTTP/2 支持?

这可以通过在 DistributionConfig: 属性 下添加 HttpVersion: 'http2' 来实现。请参阅下面的完整示例。

## Specifying the CloudFront Distribution to server your Web Application
WebAppCloudFrontDistribution:
  Type:                               AWS::CloudFront::Distribution
  Properties:
    DistributionConfig:
      HttpVersion:                    'http2'
      Origins:
        - DomainName:                 ${self:custom.s3Bucket}.s3.amazonaws.com
          ## An identifier for the origin which must be unique within the distribution
          Id:                         WebApp
          CustomOriginConfig:
            HTTPPort:                 80
            HTTPSPort:                443
            OriginProtocolPolicy:     https-only
          ## In case you want to restrict the bucket access use S3OriginConfig and remove CustomOriginConfig
          # S3OriginConfig:
          #   OriginAccessIdentity:   origin-access-identity/cloudfront/E127EXAMPLE51Z
      Enabled:                        'true'
      ## Uncomment the following section in case you are using a custom domain
      # Aliases:
      # - mysite.example.com
      DefaultRootObject:              index.html
      ## Since the Single Page App is taking care of the routing we need to make sure ever path is served with index.html
      ## The only exception are files that actually exist e.h. app.js, reset.css