How to setup CDN CloudFront for Refile gem? - ruby-on-rails

I've got my application working fine with Refile and uploading images to S3. I've read that you should put a CDN in front of the application to make it more efficient and secure.
Is there any place I can read to set it up properly? I've look at the github repo and tried following the instructions but I feel like I did it wrong.
This is what I did:
Create a AWS CloudFront distribution where origin domain name points to my S3 instance (bucket_name.s3.amazonaws.com).
Create a CNAME with Route 53 to add a custom domain name "cdn.mydomain.com" that points to the CloudFront domain name "xxx.cloudfront.net".
Go to my /config/initializers/refile.rb and added the line:
Refile.cdn_host = "https://cdn.myappname.com".
Commit and push to Heroku
All my image links are broken now.
What did I do wrong?

The way I did this was to put a put Cloudfront in front of the whole site and not the bucket itself.
Create AWS CloudFront distribution setting the origin to my domain (www.example.com)
Create CNAME (cloud.example.com) that points to xxx.cloudfront.net
Go to config/environments/production.rb and add the line config.action_controller.asset_host = 'http://cloud.example.com'
Go to config/initializers/refile.rb and add the line Refile.cdn_host = 'http://cloud.example.com'

Related

Enabling Rails CloudFront on Heroku

I followed this article on Heroku to enable CloudFront on my Rails 5 app hosted on Heroku. However, it doesn't seem to be working for my images (which are hosted on S3).
I have tried setting the Origin Domain Name in AWS to both by S3 bucket (selected from the dropdown), as well as my app's domain name.
However, when I look at the source code, the images are still using the S3 URL instead of the being prefaced with the CloudFront URL which leads me to believe that something is not set-up right.
When I look at the source, I can see the some things are using the preface, such as my favicon https://mycloudfront.cloudfront.net/assets/favicon-28fde7db6babda6b94460b806d567abe4521f8dd77bc3741debadbf30eeecb19.ico.
I saw this post
Rails Cloudfront assets not served, but I'm using image_tag on the images showing the S3 URL in the source code.
in production.rb
config.action_controller.asset_host = "https://mycloudfront.cloudfront.net/"

Problems serving .js and .css assets cloudfront CDN Heroku Rails 4 App

I am trying to host the assets of my Rails 4 App over Cloudfront CDN. I used to use asset_sync and s3 but I wanted to switch over to a CDN.
When I go to my Heroku App, I see that all the pages are just bare HTML. None of the JS or CSS is being loaded.
These are the errors I am getting from my Console:
Screenshot of console errors: http://i61.tinypic.com/15rxdsj.jpg
Also, I am not sure if I have set up my Origin Domain Name and Origin Path correctly on the Cloudfront Origin Settings.
Currently I am using my heroku app url as the Origin Domain Name and "/production/assets" as the Origin Path.
Production.rb file: http://pastebin.com/2dzLpGfE
I have been trying unsuccessfully for the past week to get the Heroku app to display the CSS and JS. I would greatly appreciate any insight. Thanks in advance!
Moving to Cloudfront isn't as drastic as change as you seem to think if you had everything working from S3. After all, Cloudfront simply distributes the contents of your S3 bucket to edge locations. This means you just have to let Rails know to look for the CDN and not S3.
There are a lot of things that could be going on. You could have misconfigured Cloudfront, which should be pointing to your S3 bucket as the origin. You should test that setup by checking to see an asset in the browser by using the Cloudfront URL. The main point is that Cloudfront should have no idea about your Rails app.
Meanwhile, you can still use AssetSync to push your assets to S3 "underneath" an assets path. You must also configure config.action_controller.asset_host as described here.

Use CDN and HTTPS for Spree::Images

Is it possible to use a CDN like Amazon Cloudfront with spree? I know I can set config.action_controller.asset_host in production.rb, but this doesn't affect the Spree::Image, or any spree helper functions like product_image().
Also, the /admin/image_settings/edit url with setting for s3_protocol, which seems to have no effect, even setting it to blank. I would like to be protocol agnostic, and have the URLs formed like //foo.cloudfront.com
Spree's image uploader is provided the Paperclip gem. There's a handy guide for Using Cloudfront with Paperclip. Paperclip will not use asset_host.
The first step would be to get your S3 image hosting working the way you want, and then get it to work through Cloudfront.
s3_protocol being '' should use protocol relative URL's as shown in this pull request.

How do I setup a route as an alias for Amazon S3?

I'm hosting my rails app with heroku and managing my DNS with ZerigoDNS (the free heroku addon). I use Amazon S3 to store all of my assets.
I have a public folder in my S3 bucket called newsletter.
I'd like http://mydomain.com/newsletter to simply act as an alias to https://my-bucket.s3.amazonaws.com/newsletter.
How would I go about doing that?
Thanks!
Edit: preferably, I'd like the URL to not redirect but instead just read from the S3 folder. So I'd basically just set up an alias for https://my-bucket.s3.amazonaws.com/newsletter. Is there a way to do that using routes.rb or some gem?

When the protocol is https, Amazon S3 assets won't load, - Rails, Heroku

I set up asset_sync gem on heroku, following this URL: https://github.com/rumblelabs/asset_sync
The settings are working and I uploaded all the static assets at S3.
The problem is, when I open page through https protocol, can't access any of the assets, because the browser returns "This Connection is Untrusted". (same with Chrome & Firefox).
Every assets will be able to use after I admit access to s3 assets url. https://myapp.asset.s3.amazonaws.com/assets
Anyone had same problem? how to fix this problem?
The SSL certificate for s3 is a wildcard certificate, i.e. it is for *.s3.amazonaws.com. However a lot of certificate checking libraries define this to cover foo.s3.amazonaws.com but not foo.bar.amazonaws.com: wildcard certificates only go one level down.
The simplest solution is to pick a bucket name with no dots in it, e.g. myapp-assets.
Another solution is to access the files as https://s3.amazonaws.com/myapp.asset/assets/.... I believe you'd have to set config.assets.prefix to tell rails that the assets aren't in the normal location relative to the asset host.

Resources