Rails 3.1 Assets on S3 with HTTPS

So your Rails app is deployed on Heroku which is pretty sweet and makes things easy. The Rails 3.1 asset pipeline is pretty awesome and packages your files up for you. You're using carrierwave to store Image attachments on Amazon S3, and everything is going great. Then, it's time to scale. Heroku isn't going to like serving all your assets all the time, CDNs are the right place to do that. Oh, and you need to be able to use SSL on the registration and login pages. Seems pretty easy, right?

There are plenty of very easily findable docs on each of those topics, but gluing them together took some figuring. First up, Make sure your Amazon S3 buckets are in the format 'media-example-com' and _NOT_ 'media.example.com'. Due to the way HTTPS works, if you try to access a https://media.example.com.s3.amazonaws.com/ URL you're going to get an invalid certificate warning. Doh! If you haven't done this, it's pretty annoying because there is no concept of 'move' or 'rename', so you'll have to copy files from the old bucket to the new one. Go ahead and start on this, it might take you a while.

Once that's done, it's pretty much just gems and configuration. Make sure you have these guys installed:

Then make a rake task that depends on 'assets:precompile' (which with asset_sync will take care of uploading your compiled assets to heroku):

That `assets:clean` part? That makes sure to wipe out your assets locally so that when you make changes to CSS/JS locally, they will show up in your browser as expected. That one took a few of us a while to figure out. ("WHY WONT MY CSS UPDATE!!!111!!!")

Then configure carrierwave, paying extra special attention to the fog_host URL, starting it with "//" which makes sure the resources will be loaded via http when the page is http, and https when the page is https:

Last up is configuring your asset_host for your production environment. All the docs suggest using a Proc here, but precompiling assets (needed for Rails 3.1 -> S3 on Heroku) doesn't have a 'request' to detect the hostname from so this completely blows up and you end up with no CSS or JS on your asset hosts. There are several related issues in the Rails issue tracker, but there's a super simple solution that works better than all of them:

And you're all set! `rake deploy` will compile your assets, upload them to your bucket on S3, and deploy your code to Heroku. When a visitor visits http://example.com/ they'll get all the assets (JS/CSS) and all your media (item images) from the HTTP version of your Amazon S3 buckets, and when they visit https://example.com/, they'll get the HTTPS version.

It should be super easy to plug in CloudFront if you need to scale up even further.

Heroku isn't going to like

Heroku isn't going to like serving all your assets all the time

At what point does Heroku start getting fussy? Is this solution to keep you from having to add workers to keep up with trafic or what?

Serving assets from a CDN (or

Serving assets from a CDN (or just elsewhere) will be: faster (time to load full asset file will be quicker, especially for people closer to the CDN entry points than us-east-1), faster (browser can open connections to CDN and Heroku at the same time so it gets all the files faster), faster (doesn't use up web processes on Heroku so they can immediately fill 'actual' requests instead of other ones)

Thanks. I needed a tip on how

Thanks. I needed a tip on how to get images to download as http not https

I'm a newbie and have been

I'm a newbie and have been trying to use CarrierWave on Heroku with fog. I keep getting an error that says "missing aws_access_key_id and aws_secret_access_key" even though I have it in my initializer file. I've tried everything but perhaps I am missing the deploy.rake file in your commentary. My question is, where exactly do I put that file? Sorry I am super new, esp to deploying. Thanks for this btw.

This isn't really a good

This isn't really a good forum for troubleshooting. You should post a blog post with all the details and the trouble you are having, or ask questions on a place like StackOverflow.