将 True-Client-IP 标头添加到请求中 - Amazon CloudFront
Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅 中国的 Amazon Web Services 服务入门 (PDF)

True-Client-IP 标头添加到请求中

以下函数示例将 True-Client-IP HTTP 标头添加到请求中,并将查看器的 IP 地址作为标头的值。当 CloudFront 向源发送请求时,源可以确定发送请求的 CloudFront 主机的 IP 地址,但不能确定向 CloudFront 发送原始请求的查看器(客户端)的 IP 地址。此函数将添加 True-Client-IP 标头,以便源可以看到查看器的 IP 地址。

重要

要确保 CloudFront 在源请求中包含此标头,您必须将其添加到源请求策略中允许的标头列表中。

这是查看器请求函数。

在 GitHub 上查看此示例

JavaScript runtime 2.0
async function handler(event) { var request = event.request; var clientIP = event.viewer.ip; //Add the true-client-ip header to the incoming request request.headers['true-client-ip'] = {value: clientIP}; return request; }
JavaScript runtime 1.0
function handler(event) { var request = event.request; var clientIP = event.viewer.ip; //Add the true-client-ip header to the incoming request request.headers['true-client-ip'] = {value: clientIP}; return request; }