Does Amazon pass custom headers to origin?

不问归期 提交于 2019-12-22 19:59:42

问题


I am using CloudFront to front requests to our service hosted outside of amazon. The service is protected and we expect an "Authorization" header to be passed by the applications invoking our service.

We have tried invoking our service from Cloud Front but looks like the header is getting dropped by cloud front. Hence the service rejects the request and client gets 401 forbidden response. For some static requests, which do not need authorization, we are not getting any error and are getting proper response from CloudFront.

I have gone through CloudFront documentation and there is no specific information available on how headers are handled and hence was hoping that they will be passed as is, but looks like thats not the case. Any guidance from you folks?


回答1:


The list of the headers CF drops or modifies can be found here

http://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/RequestAndResponseBehaviorCustomOrigin.html#RequestCustomRemovedHeaders




回答2:


CloudFront does drop Authorization headers by default and will not pass it to the origin.

If you would like certain headers to be sent to the origin, you can setup a whitelist of headers under CloudFront->Behavior Settings->Forward headers. Just select the headers that you would like to be forwarded and CloudFront will do the job for you. I have tested it this way for one of our location based services and it works like a charm.

One thing that I need to verify is if the Authorization header will be included in the cache key and if its safe to do that?? That is something you might want to watch out for as well.




回答3:


In addition to specifying them under the Origin Behaviour section, you can also add custom headers to your origin configuration. In the AWS documentation for CloudFront custom headers:

If the header names and values that you specify are not already present in the viewer request, CloudFront adds them. If a header is present, CloudFront overwrites the header value before forwarding the request to the origin.

The benefit of this is that you can then use an All/wildcard setting for whitelisting your headers in the behaviour section.




回答4:


It sounds like you are trying to serve up dynamic content from CloudFront (at least in the sense that the content is different for authenticated vs unauthenticated users) which is not really what it is designed to do.

CloudFront is a Content Distribution Network (CDN) for caching content at distributed edge servers so that the data is served near your clients rather than hitting your server each time.

You can configure CloudFront to cache pages for a short time if it changes regularly and there are some use cases where this is worthwhile (e.g. a high volume web site where you want to "micro cache" to reduce server load) but it doesn't sound like this is the way you are trying to use it.

In the case you describe:

  • The user will hit CloudFront for the page.
  • It won't be in the cache so CloudFront will try to pull a copy from the origin server.
  • The origin server will reply with a 401 so CloudFront will not cache it.

Even if this worked and headers were passed back and forth in some way, there is is simply no point in using CloudFront if every page is going to hit your server anyway; you would just make the page slower because of the extra round trip to your server.




回答5:


It makes sense CF drops the Authorization header, just imagine 2 users asking for the same object, the first one will grant access, CF will cache the object, then the second user will get the object as it was previously cached by CloudFront.

Great news are using forward headers you can forward the Authorization header to the origin, that means the object will be cached more than once as the header value is part of the cache "key"

For exmple user A GETS private/index.html Authorization: XXXXXXXXXXXXX

  • The object will be cached as private/index.html + XXXXXXXXXXXXX (this is the key to cahce the object in CF)

Now when the new request from a diferent user arrives to CloudFront GET private/index.html Authorization: YYYYYYYYYYYY

  • The object will be passed to the origin as the combinaiton of private/index.html + YYYYYYYYYYYY is not in CF cache.

Then Cf will be cached 2 diferent objects with the same name (but diferent hash combinaiton name).



来源:https://stackoverflow.com/questions/18076474/does-amazon-pass-custom-headers-to-origin

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!