简体   繁体   English

将S3 / CloudFront与Rails 3资产和较少CSS一起使用

[英]Using S3/CloudFront with Rails 3 Assets and Less CSS

This one is a mouthful! 这一口是满口的! Basically I'm trying to send all of my Rails 3 assets up to the S3 Cloud and use CloudFront as the CDN to deliver it all. 基本上我正在尝试将所有Rails 3资产发送到S3 Cloud,并使用CloudFront作为CDN来提供所有这些资产。 I already learned about configuring Rails to pull from an asset server in production mode. 我已经了解了如何配置Rails以从生产模式下的资产服务器中提取。 The problem I'm running into is finding a good way to automatically package and send everything to the cloud from a rake command or rails gem. 我遇到的问题是找到一种自动打包并通过rake命令或rails gem将所有内容发送到云端的好方法。 The other problem I have is I don't know if using Less CSS with the More gem is going to screw this up. 我遇到的另一个问题是我不知道使用更少的CSS与更多的宝石是否会搞砸了。 More generates a CSS file from another directory and places it in public/stylesheets. 更多从另一个目录生成一个CSS文件,并将其放在public / stylesheets中。 Any ideas or suggestions are much appreciated! 任何想法或建议都非常感谢! Thanks :) 谢谢 :)

If you are pushing to Heroku and are using the Rails 3.1 assets you are all set. 如果您正在推动Heroku并使用Rails 3.1资产,那么您已经完成了设置。

In the CloudFront configuration on amazon create your distribution and set the origin to your applications URL. 在amazon上的CloudFront配置中,创建您的分发并将原点设置为您的应用程序URL。

Then in your production.rb file add: 然后在你的production.rb文件中添加:

config.action_controller.asset_host = "xxxxxxxxx.cloudfront.net"

The host is the host of your CloudFront distribution. 主机是CloudFront分配的主机。

Then when you deploy make sure you are on the Cedar stack and that assets are being compiled. 然后在部署时确保您在Cedar堆栈上并且正在编译资产。 This will add a unique MD5 into the filenames. 这将在文件名中添加唯一的MD5。 When a request is made to your CDN (handled automatically by the setting in your production.rb file), then the CDN will either serve up it's version of the file or pull it from the origin first. 当向您的CDN发出请求(由您的production.rb文件中的设置自动处理)时,CDN将提供其文件版本或首先从原始文件中提取。 This means you don't have to push files up to the CDN, they are pulled in automatically. 这意味着您不必将文件推送到CDN,它们会自动拉入。

If you have a file that doesn't have a unique name for some reason, then you will need to look at how to invalidate the cache in CloudFront, but other than that it's pretty easy. 如果您的文件由于某种原因没有唯一的名称,那么您需要了解如何在CloudFront中使缓存失效,但除此之外它非常简单。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM