Using Amazon Cloudfront with Rails and Apache and https

Cloudfront is an easy way to cache assets from your Rails application. However there are several gotchas, so these are the steps I took.

Let’s say you have this website:

  • Your domain name is
  • You have an SSL cert so it is running at
  • You would like assets being served from /assets to be served from cloudfront
  1. Create a Web Distribution:
    Log into your Amazon AWS Console

2.  Distribution Settings

  • Origin Domain Name: This is the domain name of the assets you want to cache.   Since you want to cache assets (images, css, js) put in
  • Origin Protocol Policy: if you know you are using SSL, you can select it here

  • Forward Headers: One of the things to avoid problems with CORS is to forward headers with ‘origin’.  What this will do is forward your ‘Access-Control-Origin’ to cloudfront

Afterwards you can click save

3) Edit Distribution


You will see that now your distribution is:

  • name:
  • Status: In Progress

At this point you can continue, but all of your URLS will be\assets\main.css

We want to make it look a little prettier, so click into the distribution, click general, then click ‘edit’

4) Custom SSL

Now let’s say we want things mapped from to  The first thing is we need an SSL Cert, and we can get one through AWS.  Click Request or Import a Certificate

  • Enter the domain name ‘’.  What will happen is a series of validation steps to ensure you actually own it
  • Alternate Domain Names: Enter in  Later we will make a cname record for this
  • Custom SSL Certificate: Select the certificate
  • Custom SSL Client Support:  If you want to save money (like a LOT) then select the SNI support.  Most browsers support this anyways.

5) Create cname record

  • Create a cname record of cloudfront to –>

6) Rails configuration
In production.rb set

config.action_controller.asset_host = “”
And ensure your assets are using Rails helpers that point to the asset pipeline (like image_tag, etc).

7) Apache Vhost Configuration

If you don’t add any CORS configuration what will happen is that will access and in your browser console, you will see a bunch of CORS problems.

With the setting you specified earlier, cloudfront will read one of your original assets such as

  • Caches it, forwards the origin header
  • Then your rails site accesses it as

In your Apache vhost file add this setting

<VirtualHost *:443>
Header set Access-Control-Allow-Origin ""

If you open chrome and check, the response headers for any request should be

Response Headers:

So when Cloudfront pulls in the asset, it will also pull in that header.    An alternative method is to use rack-cors

Restart apache

8) Finally wait until your distribution says ‘deployed’

Deploy the new code with the production config, and if all goes well, you should be distributing from Cloudfront!