问题
Is there a gem or method available in Rails 3.1 that can upload assets to amazon cloud front automatically and use those instead of serving locally hosted ones? I guess it's easy to upload compiled assets manually and then change the rails app config to use that asset host, but when an asset is modified, the uploads to cloud front would need to be done manually again. Any good ways out there for this?
回答1:
Definitely check out asset_sync on github. Or our Heroku dev centre article on Using a CDN asset Host with Rails 3.1 on Heroku.
There is quite a big performance improvement in using asset_sync vs a CDN custom origin, letting your application lazily compile assets in production or serving them precompiled directly off your app servers. However I would say that. I wrote it.
- With asset_sync and S3 you can precompile assets meaning all the assets are there ready to be served on the asset host / CDN immediately
- You can only require the :assets bundle in application.rb on precompile, saving memory in production
- Your app servers are NEVER hit for asset requests. You can spend expensive compute time on, you know. Computing.
- Best practice HTTP cache headers are all set by default
- You can enable automatic gzip compression with one extra config
回答2:
If you use Cloudfronts “Custom origin” option you do not need to upload anything, Cloudfront will fetch the assets from your server when needed. For details of setting this up see:
http://blog.ertesvag.no/post/10720082458
回答3:
Take a look at https://github.com/rumblelabs/asset_sync - we're using it just to S3 but I guess the CloudFront part is pretty easy once the assets are on S3.
It's ends up being a rake task that you just add to execute in your deployment process.
回答4:
another option would be https://github.com/moocode/asset_id, the readme has an example to use it with cloudfront. It should work with rails 3.1 but I have only used it on 3.0.x.
Ss John said all solutions would end up being a rake task + a bit of logic to change the asset path in rails.
来源:https://stackoverflow.com/questions/8354514/rails-3-automatic-asset-deployment-to-amazon-cloudfront