I've setup a CDN using Cloudfront for my Rails 3 app. I created 4 CNAME redirects (cdn1.mywebsite.com, cdn2.mywebasite.com, etc) so I can parallelize the static assets download.

Is it possible to configure rails to use multiple assets hosts?

I tried

config.action_controller.asset_host = ["http://cdn1.mywebasite.com", "http://cdn2.mywebasite.com", "http://cdn3.mywebasite.com", "http://cdn4.mywebasite.com"]

But it didn't work.

有帮助吗?

解决方案

See http://api.rubyonrails.org/classes/ActionView/Helpers/AssetUrlHelper.html

Browsers typically open at most two simultaneous connections to a single host, which means your assets often have to wait for other assets to finish downloading. You can alleviate this by using a %d wildcard in the asset_host. For example, “assets%d.example.com”. If that wildcard is present Rails distributes asset requests among the corresponding four hosts “assets0.example.com”, …, “assets3.example.com”. With this trick browsers will open eight simultaneous connections rather than two.

You'd need to tweak your DNS a bit to start counting from 0 and then do:

config.action_controller.asset_host = 'http://cdn%d.mywebasite.com'
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top