

文档 AWS SDK 示例 GitHub 存储库中还有更多 [S AWS DK 示例](https://github.com/awsdocs/aws-doc-sdk-examples)。

本文属于机器翻译版本。若本译文内容与英语原文存在差异，则一律以英文原文为准。

# 将 Origin 标头添加到 CloudFront 函数查看器请求事件
<a name="cloudfront_example_cloudfront_functions_add_origin_header_section"></a>

以下代码示例演示如何向 CloudFront 函数查看器请求事件添加源标头。

------
#### [ JavaScript ]

**JavaScript CloudFront 函数的运行时 2.0**  
 还有更多相关信息 GitHub。在 Functions 示例存储库中查找完整的[示例](https://github.com/aws-samples/amazon-cloudfront-functions/tree/main/add-origin-header)并学习如何设置和运行。CloudFront 

```
async function handler(event) {
    var request = event.request;
    var headers = request.headers;
    var host = request.headers.host.value;
   
   // If origin header is missing, set it equal to the host header.
   if (!headers.origin)
       headers.origin = {value:`https://${host}`};
       
   return request;
}
```

------