如何使CloudFront-Viewer-Country出现在响应标题中? [英] How do I get CloudFront-Viewer-Country to appear in response headers?

查看:182
本文介绍了如何使CloudFront-Viewer-Country出现在响应标题中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试利用CloudFront上的AWS地理跟踪功能向我的UI通知用户位置,以便它知道不将某些文件加载​​到有问题的区域。

I am attempting to leverage AWS's geotracking feature on CloudFront to inform my UI of user location so that it knows not to load certain files in problematic regions.

AWS文档提到以下内容,但没有明确说明如何准确配置CloudFront以将CloudFront-Viewer-Country标头转发到源。我需要执行此操作,以便对缓存对象的请求产生具有CloudFront-Viewer-Country值的响应。

AWS documentation mentions the following but does not give clear instructions on how exactly to configure CloudFront to forward the CloudFront-Viewer-Country header to origin. I need this to work so that requests to my cached objects produce responses with CloudFront-Viewer-Country values.


配置CloudFront以基于缓存对象在查看器的位置上

Configuring CloudFront to Cache Objects Based on the Location of the Viewer


如果您希望CloudFront根据请求来自的国家/地区缓存对象的不同版本,请配置CloudFront会将CloudFront-Viewer-Country标头转发到您的来源。 CloudFront会自动将请求来自的IP地址转换为两个字母的国家/地区代码。有关易于使用的国家/地区代码列表(可按代码和国家/地区名称排序),请参阅Wikipedia条目ISO 3166-1 alpha-2。

If you want CloudFront to cache different versions of your objects based on the country that the request came from, configure CloudFront to forward the CloudFront-Viewer-Country header to your origin. CloudFront automatically converts the IP address that the request came from into a two-letter country code. For an easy-to-use list of country codes, sortable by code and by country name, see the Wikipedia entry ISO 3166-1 alpha-2.



推荐答案

CloudFront默认情况下无法执行此操作-CloudFront-Viewer-Country用作请求标头,发送到原点,而不是 response 标头,发送到浏览器。

CloudFront can't do this by default -- CloudFront-Viewer-Country is intended as a request header, sent to the origin, rather than a response header, sent to the browser.

但是...带有Lambda @ Edge原点响应触发器,可以实现您似乎想做的事情:将该标头及其值回显到响应中。在原点响应处理期间,也可以访问您转发到原点的大多数原点请求标头。

However... with a Lambda@Edge Origin Response trigger, it is possible to achieve what you appear to be trying to do: echo this header and its value back into the response. Most of the origin request headers that you forward to the origin are also accessible during origin response processing.


“ AWS文档中提到以下内容,但没有明确说明如何准确配置CloudFront以将 CloudFront-Viewer-Country 标头转发到原始位置。

“AWS documentation mentions the following but does not give clear instructions on how exactly to configure CloudFront to forward the CloudFront-Viewer-Country header to origin.”

这是在基于选定请求标头的缓存,方法是选择白名单并选择 CloudFront-Viewer-Country 在左侧框中,然后将其移至右侧框中。

This is done in the Cache Behavior settings, under Cache Based on Selected Request Headers by choosing Whitelist and selecting CloudFront-Viewer-Country in the box on the left and moving it to the box on the right.

您需要将这个标头列入白名单,以转发到原点,以便Lambda @ Edge可以使用它-即使您的原点不使用或不需要它-您仍然需要转发它才能使此功能按预期工作,因为Origin Request触发器无法看到CloudFront无法转发的内容...并且除非您将它们包括白名单,否则大多数头都不会转发,包括大多数 CloudFront-* CloudFront可以注入的标头。

You will need to whitelist this header for forwarding to the origin, in order for it to be available to Lambda@Edge -- even if your origin doesn't use or need it -- you still need to forward it for this function to work as intended, because an Origin Request trigger can't see what CloudFront doesn't forward... and most headers are not forwarded unless you whitelist them, including most of the CloudFront-* headers that CloudFront can inject.

'use strict';

exports.handler = (event, context, callback) => {
   const request = event.Records[0].cf.request;
   const response = event.Records[0].cf.response;
   if(request.headers['cloudfront-viewer-country'])
   {
      response.headers['cloudfront-viewer-country'] = request.headers['cloudfront-viewer-country'];
   }
   return callback(null,response);
};

请注意,Lambda @ Edge要求标头对象中的外键始终小写,无论实际的标题大写字母。

Note that Lambda@Edge requires the outer keys in the header object always to be lowercase, regardless of the actual header lettercase.

这篇关于如何使CloudFront-Viewer-Country出现在响应标题中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆