

本文為英文版的機器翻譯版本，如內容有任何歧義或不一致之處，概以英文版為準。

# 建立回應標頭政策
<a name="creating-response-headers-policies"></a>

您可以使用回應標頭政策來指定 Amazon CloudFront 移除或新增至 HTTP 回應的 HTTP 標頭。如需回應標頭政策及其使用原因的詳細資訊，請參閱[在 CloudFront 回應中使用政策新增或移除 HTTP 標頭](modifying-response-headers.md)。

您可以在 CloudFront 主控台中建立回應標頭政策，或者，您可以使用 AWS CloudFormation、 AWS Command Line Interface (AWS CLI) 或 CloudFront API 建立一個。建立回應標頭政策之後，您可以將它連接到 CloudFront 分佈中的一或多個快取行為。

建立自訂回應標頭政策之前，請先確認是否有任何[受管回應標頭政策](using-managed-response-headers-policies.md)適合您的使用案例；如果有，您就可以將該政策連接到您的快取行為，而不需自行建立或管理回應標頭政策。

------
#### [ Console ]

**建立回應標頭政策 (主控台)**

1. 登入 AWS 管理主控台，然後前往 CloudFront 主控台**的政策**頁面上的**回應標頭**索引標籤[https://console.aws.amazon.com/cloudfront/v4/home#/policies/responseHeaders](https://console.aws.amazon.com/cloudfront/v4/home#/policies/responseHeaders)。

1. 選擇 **Create response headers policy (建立回應標頭政策)**。

1. 在 **Create response headers policy (建立回應標頭政策)** 表單中執行下列動作：

   1. 在 **Details (詳細資訊)** 面板中，輸入回應標頭政策的 **Name (名稱)** 和 (選用) 解釋政策用途的 **Description (描述)**。

   1. 在 **Cross-origin resource sharing (CORS) (跨來源資源分享 (CORS))** 面板中，選擇 **Configure CORS (設定 CORS)** 切換開關，並設定您要新增至政策的任何 CORS 標頭。如果您希望設定的標頭覆寫 CloudFront 從原始伺服器接收的標頭，請選取 **Origin override (原始伺服器覆寫)** 核取方塊。

      如需 CORS 標頭設定的詳細資訊，請參閱 [CORS 標頭](understanding-response-headers-policies.md#understanding-response-headers-policies-cors)。

   1. 在 **Security headers (安全性標頭)** 面板中，選擇切換開關並設定您要新增至政策的每個安全性標頭。

      如需安全性標頭設定的詳細資訊，請參閱 [安全性標頭](understanding-response-headers-policies.md#understanding-response-headers-policies-security)。

   1. 在 **Custom headers (自訂標頭)** 面板中，新增您想要納入政策的任何自訂標頭。

      如需自訂標頭設定的詳細資訊，請參閱 [自訂標頭](understanding-response-headers-policies.md#understanding-response-headers-policies-custom)。

   1. 在 **Remove headers** (移除標頭) 面板中，新增您希望 CloudFront 從原始伺服器回應移除，而不包含於 CloudFront 傳送給檢視者的回應中的任何標頭名稱。

      如需移除標頭設定的詳細資訊，請參閱 [移除標頭](understanding-response-headers-policies.md#understanding-response-headers-policies-remove-headers)。

   1. 在 **Server-Timing header** (Server-Timing 標頭) 面板中，選擇 **Enable** (啟用) 切換，然後輸入採樣率 (須為介於 0 到 100 之間的數值，包括 0 與 100)。

      如需有關 `Server-Timing` 標頭的詳細資訊，請參閱[Server-Timing 標頭](understanding-response-headers-policies.md#server-timing-header)。

1. 選擇 **Create (建立)** 以建立政策。

建立回應標頭政策之後，您可以將它連接到 CloudFront 分佈中的快取行為。

**若要將回應標頭政策連接至現有分佈 (主控台)**

1. 在位於 [https://console.aws.amazon.com/cloudfront/v4/home#/distributions](https://console.aws.amazon.com/cloudfront/v4/home#/distributions) 的 CloudFront 主控台中開啟 **Distributions** (分佈) 頁面。

1. 選擇要更新的分佈，然後選擇 **Behaviors (行為)** 索引標籤。

1. 選擇要更新的快取行為，然後選擇 **Edit (編輯)**。

   或者，若要建立新的快取行為，請選擇 **Create behavior (建立行為)**。

1. 對於 **Response headers policy (回應標頭政策)**，選擇要新增至快取行為的政策。

1. 選擇 **Save changes (儲存變更)** 以更新快取行為。如果要建立新的快取行為，請選擇 **Create behavior** (建立行為)。

**若要將回應標頭政策連接至新分佈 (主控台)**

1. 在 [https://console.aws.amazon.com/cloudfront/v4/home](https://console.aws.amazon.com/cloudfront/v4/home) 中開啟 CloudFront 主控台。

1. 選擇 **Create Distribution (建立分佈)**。

1. 對於 **Response headers policy (回應標頭政策)**，選擇要新增至快取行為的政策。

1. 為您的分佈選擇其他設定。如需詳細資訊，請參閱[所有分佈設定參考](distribution-web-values-specify.md)。

1. 選擇 **Create distribution (建立分佈)** 以建立分佈。

------
#### [ CloudFormation ]

若要使用 建立回應標頭政策 CloudFormation，請使用 `AWS::CloudFront::ResponseHeadersPolicy` 資源類型。下列範例以 YAML 格式顯示 CloudFormation 範本語法，用於建立回應標頭政策。

```
Type: AWS::CloudFront::ResponseHeadersPolicy
Properties: 
  ResponseHeadersPolicyConfig: 
    Name: EXAMPLE-Response-Headers-Policy
    Comment: Example response headers policy for the documentation
    CorsConfig: 
      AccessControlAllowCredentials: false
      AccessControlAllowHeaders: 
        Items: 
          - '*'
      AccessControlAllowMethods: 
        Items: 
          - GET
          - OPTIONS
      AccessControlAllowOrigins: 
        Items: 
          - https://example.com
          - https://docs.example.com
      AccessControlExposeHeaders: 
        Items: 
          - '*'
      AccessControlMaxAgeSec: 600
      OriginOverride: false
    CustomHeadersConfig: 
      Items: 
        - Header: Example-Custom-Header-1
          Value: value-1
          Override: true
        - Header: Example-Custom-Header-2
          Value: value-2
          Override: true
    SecurityHeadersConfig: 
      ContentSecurityPolicy: 
        ContentSecurityPolicy: default-src 'none'; img-src 'self'; script-src 'self'; style-src 'self'; object-src 'none'; frame-ancestors 'none'
        Override: false
      ContentTypeOptions: # You don't need to specify a value for 'X-Content-Type-Options'.
                          # Simply including it in the template sets its value to 'nosniff'.
        Override: false
      FrameOptions: 
        FrameOption: DENY
        Override: false
      ReferrerPolicy: 
        ReferrerPolicy: same-origin
        Override: false
      StrictTransportSecurity: 
        AccessControlMaxAgeSec: 63072000
        IncludeSubdomains: true
        Preload: true
        Override: false
      XSSProtection: 
        ModeBlock: true # You can set ModeBlock to 'true' OR set a value for ReportUri, but not both
        Protection: true
        Override: false
    ServerTimingHeadersConfig:
      Enabled: true
      SamplingRate: 50
    RemoveHeadersConfig:
      Items:
        - Header: Vary
        - Header: X-Powered-By
```

如需詳細資訊，請參閱 *AWS CloudFormation 使用者指南*中的 [AWS::CloudFront::ResponseHeadersPolicy](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-cloudfront-responseheaderspolicy.html)。

------
#### [ CLI ]

若要使用 AWS Command Line Interface (AWS CLI) 建立回應標頭政策，請使用 **aws cloudfront create-response-headers-policy**命令。您可以使用輸入檔案來提供命令的輸入參數，而不必分別將每個個別參數指定為命令列輸入。

**建立回應標頭政策 (包含輸入檔案的 CLI)**

1. 使用下列命令建立名為 `response-headers-policy.yaml` 的檔案。這個檔案中包含 **create-response-headers-policy** 命令的所有輸入參數。

   ```
   aws cloudfront create-response-headers-policy --generate-cli-skeleton yaml-input > response-headers-policy.yaml
   ```

1. 開啟您剛才建立的 `response-headers-policy.yaml` 檔案。編輯檔案以指定政策名稱和所需的回應標頭政策組態，然後儲存檔案。

   如需有關回應標頭政策設定的詳細資訊，請參閱 [了解回應標頭政策](understanding-response-headers-policies.md)。

1. 使用下列命令建立回應標頭政策。您所建立的政策會使用 `response-headers-policy.yaml` 檔案中的輸入參數。

   ```
   aws cloudfront create-response-headers-policy --cli-input-yaml file://response-headers-policy.yaml
   ```

   記下命令輸出中的 `Id` 值，這是回應標頭政策的 ID，將此政策連接到 CloudFront 分佈的快取行為時需要使用這個值。

**將回應標頭政策連接至現有分佈 (包含輸入檔案的 CLI)**

1. 使用下列命令來儲存您想要更新之 CloudFront 分佈的分佈組態。將 *distribution\$1ID* 改為分佈的 ID。

   ```
   aws cloudfront get-distribution-config --id distribution_ID --output yaml > dist-config.yaml
   ```

1. 開啟您剛才建立且命名為 `dist-config.yaml` 的檔案。編輯檔案，對快取行為進行下列變更，以便讓該行為使用此回應標頭政策。
   + 在快取行為中新增名為 `ResponseHeadersPolicyId` 的欄位。對於欄位值，請使用您在建立政策後記下的回應標頭政策 ID。
   + 將 `ETag` 欄位重新命名為 `IfMatch`，但不要變更欄位的值。

   完成後儲存檔案。

1. 使用下列命令來更新分佈以使用回應標頭政策。將 *distribution\$1ID* 改為分佈的 ID。

   ```
   aws cloudfront update-distribution --id distribution_ID --cli-input-yaml file://dist-config.yaml
   ```

**若要將回應標頭政策連接至新分佈 (包含輸入檔案的 CLI)**

1. 使用下列命令建立名為 `distribution.yaml` 的檔案。這個檔案中包含 **create-distribution** 命令的所有輸入參數。

   ```
   aws cloudfront create-distribution --generate-cli-skeleton yaml-input > distribution.yaml
   ```

1. 開啟您剛才建立的 `distribution.yaml` 檔案。在預設快取行為的 `ResponseHeadersPolicyId` 欄位中，輸入您在建立政策後記下的回應標頭政策 ID。繼續編輯檔案以指定所需的分佈設定，然後在完成後儲存檔案。

   如需有關分佈設定的詳細資訊，請參閱 [所有分佈設定參考](distribution-web-values-specify.md)。

1. 使用下列命令，使用 `distribution.yaml` 檔案中的輸入參數建立分佈。

   ```
   aws cloudfront create-distribution --cli-input-yaml file://distribution.yaml
   ```

------
#### [ API ]

若要使用 CloudFront API 建立回應標頭政策，請使用 [CreateResponseHeadersPolicy](https://docs.aws.amazon.com/cloudfront/latest/APIReference/API_CreateResponseHeadersPolicy.html)。如需您在此 API 呼叫中指定欄位的詳細資訊，請參閱 [了解回應標頭政策](understanding-response-headers-policies.md)和 AWS SDK 或其他 API 用戶端的 API 參考文件。

建立回應標頭政策之後，您可以使用下列其中一個 API 呼叫，將其連接至快取行為：
+ 若要將它附加到現有分佈中的快取行為，請使用 [UpdateDistribution](https://docs.aws.amazon.com/cloudfront/latest/APIReference/API_UpdateDistribution.html)。
+ 若要將它附加到新分佈中的快取行為，請使用 [CreateDistribution](https://docs.aws.amazon.com/cloudfront/latest/APIReference/API_CreateDistribution.html)。

對於這兩個 API 呼叫，請在快取行為中的 `ResponseHeadersPolicyId` 欄位中提供回應標頭政策的 ID。如需您在這些 API 呼叫中指定之其他欄位的詳細資訊，請參閱 [所有分佈設定參考](distribution-web-values-specify.md)和 AWS SDK 或其他 API 用戶端的 API 參考文件。

------