LoginSignup
66
63

More than 5 years have passed since last update.

Ruby on Rails + Assets on Cloud をCloudFront経由で高速化

Last updated at Posted at 2015-08-05

はじめに

ファイルサイズの大きい画像などをクラウドに配置することでウェブサーバーへのリクエストを減らし、ネットワークリソースの節約と海外からのアクセスもあるため、CloudFrontでアクセスポイントからもっとエッジサーバーへ誘導させる設定を行う

  • S3のバケット作成
  • CloudFrontのDistributions作成
  • IAMの作成
  • Gemのインストールと設定 (asset_sync/asset_host)
  • デプロイ

手順

S3バケット作成

  • バケットの作成
    S3_Management_Console.png

  • ホスティングの有効化 (エンドポイントは後ほど、CloudFrontのオリジンサーバーに利用)
    S3_Management_Console.png

CloudFrontのDistributions作成

AWS_CloudFront_Management_Console.png

AWS_CloudFront_Management_Console.png

  • Distributionの設定
Origin Domain Name オリジンサーバーの設定(今回はS3のエンドポイント)
Origin Path CloudFrontへのリクエストをオリジンサーバの特定のパスにルーティングしたい場合は設定
Origin ID なんでもOK
Restrict Bucket Access 今回はNoで設定

その他、諸々は下記を参照
http://aws.amazon.com/jp/cloudfront/details/

AWS_CloudFront_Management_Console.png

IAMの作成

RailsAppからバケットへのアクセス権限を渡します。

  • ユーザーの作成 IAM_Management_Console.png

IAM_Management_Console.png

IAM_Management_Console.png

  • s3へのアクセス許可

IAM_Management_Console.png

IAM_Management_Console.png

Gemのインストールと設定 (asset_sync/asset_host)

  • assetsとs3の連携にasset_syncを利用
    gem 'asset_sync'
  • 設定ファイルの作成
    • aws_access_key_idとaws_secret_access_keyに作成したIAMのIDとシークレットキーを設定
    • fog_directoryに作成したバケット名を指定
    • リージョンを任意で設定した場合はfog_regionを設定
config/asset_sync.yml
defaults: &defaults
  fog_provider: 'AWS'
  aws_access_key_id: 'hogehoge'
  aws_secret_access_key: 'hogehoge'
  # To use AWS reduced redundancy storage.
  # aws_reduced_redundancy: true
  fog_directory: "develop-hogehoge" # バケット名を指定
  # You may need to specify what region your storage bucket is in
  # fog_region: "eu-west-1"
  existing_remote_files: keep
  # To delete existing remote files.
  # existing_remote_files: delete
  # Automatically replace files with their equivalent gzip compressed version
  # gzip_compression: true
  # Fail silently.  Useful for environments such as Heroku
  # fail_silently: true

development:
  <<: *defaults
  enabled: false

test:
  <<: *defaults
  enabled: false

staging:
  <<: *defaults
  fog_directory: "staging-hogehoge"

production:
  <<: *defaults
  fog_directory: "production-hogehoge"

  • asset_hostの設定

作成した、DistributionのドメインNameをasset_hostに設定

config/environments/production.rb
Rails.application.configure do
  config.action_controller.asset_host = '//hogehoge.cloudfront.net'
end

デプロイ

デプロイ時の設定は特になし、rake assets:precompile時にs3へコピーされます。

S3_Management_Console.png

  • コンソールからもcloudfrontが呼ばれていることを確認 XVOLVE___main.png

簡単に設定できますね!

参考

CloudFrontとの連携
http://tiro105.hateblo.jp/entry/2015/06/21/200542

heroku+CloudFrontで静的ファイルを配信する(Webフォント対応)
http://qiita.com/Oakbow/items/9ceb93d47d918aeafe3a

AWS S3 + Cloudfrontでwebサイトを公開する手順
http://blog.ybbo.net/2014/05/14/aws-s3-cloudfront%E3%81%A7web%E3%82%B5%E3%82%A4%E3%83%88%E3%82%92%E5%85%AC%E9%96%8B%E3%81%99%E3%82%8B%E6%89%8B%E9%A0%86/

Railsアプリケーションを公開するならAssets on Cloudパターンを使おう
http://d.hatena.ne.jp/lettas0726/20130320/1363773153

66
63
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
66
63