Skip to content

Instantly share code, notes, and snippets.

@lgs
Forked from mm53bar/README.md
Last active August 29, 2015 14:27
Show Gist options
  • Save lgs/502f34a07d98f3a4313b to your computer and use it in GitHub Desktop.
Save lgs/502f34a07d98f3a4313b to your computer and use it in GitHub Desktop.
Using Cloudfront CDN with a rails app

Using Cloudfront CDN with a Rails app

Stop serving assets from your own nginx instance. Instead, let Cloudfront CDN do it for you.

Steps:

  1. Read http://ryantownsend.co.uk/post/13126016608/cloudfront-cdn-on-rails
  2. Set up your nginx virtual host according to the snippet I've included in nginx_vhost.conf
  3. Set up your rails app to use an asset host as described in the article. I've included my config in production.rb
  4. Deploy your app and restart your nginx.

Enjoy!

#
# Paste this near the bottom of your existing virtual host file
#
# config["host"] - your naked domain name i.e. hopper.io
# config["app_name"] - the name of your app i.e. hopper
server {
listen 80;
server_name assets.<%= config["host"] %>;
root /var/applications/<%= config["app_name"] %>/public/;
access_log /var/applications/<%= config["app_name"] %>/log/access-assets.log;
error_log /var/applications/<%= config["app_name"] %>/log/error-assets.log;
location / {
deny all;
}
location ^~ /assets/ {
allow all;
gzip_http_version 1.0;
gzip_static on;
expires 365d;
add_header Last-Modified "";
add_header Cache-Control public;
}
}
# Enable serving of images, stylesheets, and JavaScripts from an asset server
# config.action_controller.asset_host = "http://assets.example.com"
config.action_controller.asset_host = "//dleunmbl874bp.cloudfront.net"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment