Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any way to identify requests coming to custom origin server from CloudFront?

I'm using CloudFront with custom origin and want to redirect certain requests coming to a web app to CloudFront (clients use direct URLs, which cannot be changed to CloudFront-based URLs). In order to ensure that cache on CloudFront is updated properly, I must not redirect requests coming from CloudFront itself. Is there any way to identify such requests on origin server?

Does CloudFront add any custom headers to requests sent to origin server? Or is there any other reliable way to determine that requests is coming from CloudFront?

like image 967
Dima Malenko Avatar asked Nov 23 '12 07:11

Dima Malenko


4 Answers

yes you can identify requests coming to your origin server from cloudfront by checking the useragent. the user agent would be 'Amazon CloudFront'

like image 152
Avinash Bijja Avatar answered Oct 16 '22 02:10

Avinash Bijja


Update

It's an old question, but my update useful for someone research or looking for the new solution.

Recently AWS added new feature Origin Custom Headers.You can set a header with a secret value and check it on your origin server by the web server or your applications.

enter image description here

like image 40
Reza Mousavi Avatar answered Oct 16 '22 02:10

Reza Mousavi


Update

Avinash Bijja correctly pointed out (+1) that the HTTP User-agent header would be 'Amazon CloudFront' for requests coming from Amazon CloudFront servers. Unfortunately this doesn't seem to be explicitly documented indeed, but is implicitly acknowledged by various posts in the respective forum, see e.g. the AWS Team response to User Agent String - does CF overwrite the user agent string?:

You are correct. The User-Agent field is always populated as "Amazon CloudFront".

However, it turns out this is not currently entirely reliable, insofar CloudFront sends an empty User-Agent to the origin if one is missing in the originating client request already:

I can confirm that CloudFront is not sending a User-Agent to the origin when the original client does not send a User-Agent. We have enhancements & fixes to User-Agent handling on our backlog, but no release dates at this time. I've sent you a PM with further details.

These enhancements & fixes are apparently not rolled out still as of February 07 2013 at least.

These enhancements & fixes have been rolled out as of August 05 2013 (thanks webbiedave for the update!).


Initial Answer

Does CloudFront add any custom headers to requests sent to origin server?

One would think so indeed, but at least they don't appear to be documented where I would have expected it, namely in How CloudFront Processes and Forwards Requests to Your Custom Origin Server. Given you are in control of the origin server, you might just check its HTTP access logs though?

Or is there any other reliable way to determine that requests is coming from CloudFront?

You'll need to judge the reliability yourself, but The IP address that CloudFront forwards to the origin server is the IP addresses of a CloudFront server, not the IP address of the end user's computer. - consequently you could restrict access to the published Amazon CloudFront Public IP Ranges; however, be aware of the respective disclaimer:

The CloudFront IP addresses change frequently and we cannot guarantee advance notice of changes. On a best-effort basis, we will provide the list of current addresses. Customers should not use these addresses for mission critical applications and must never hard code them in DNS names. [emphasis mine]

Consequently you'll need to monitor this forum/post to take notice of respective changes as early as possible (if this constraint is acceptable for your use case in the first place of course).

like image 44
Steffen Opel Avatar answered Oct 16 '22 02:10

Steffen Opel


CloudFront appears to add a X-Amz-Cf-Id header to every request before forwarding it to the origin. At least, it currently is doing that for me.

http://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/RequestAndResponseBehaviorCustomOrigin.html#request-custom-headers-behavior

like image 26
overthink Avatar answered Oct 16 '22 01:10

overthink