mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
[ci skip] docs asset_pipeline#CDNs
This commit is contained in:
parent
597f98cdaf
commit
a05b46cb0f
1 changed files with 19 additions and 11 deletions
|
@ -917,19 +917,27 @@ end
|
||||||
|
|
||||||
### CDNs
|
### CDNs
|
||||||
|
|
||||||
If your assets are being served by a CDN, ensure they don't stick around in your
|
CDN stands for [Content Delivery
|
||||||
cache forever. This can cause problems. If you use
|
Network](http://en.wikipedia.org/wiki/Content_delivery_network), they are
|
||||||
`config.action_controller.perform_caching = true`, Rack::Cache will use
|
primarily designed to cache assets all over the world so that when a browser
|
||||||
`Rails.cache` to store assets. This can cause your cache to fill up quickly.
|
requests the asset, a cached copy will be geographically close to that browser.
|
||||||
|
If you are serving assets directly from your Rails server in production, the
|
||||||
|
best practice is to use a CDN in front of your application.
|
||||||
|
|
||||||
Every cache is different, so evaluate how your CDN handles caching and make sure
|
A common pattern for using a CDN is to set your production application as the
|
||||||
that it plays nicely with the pipeline. You may find quirks related to your
|
"origin" server. This means when a browser requests an asset from the CDN and
|
||||||
specific set up, you may not. The defaults NGINX uses, for example, should give
|
there is a cache miss, it will grab the file from your server on the fly and
|
||||||
you no problems when used as an HTTP cache.
|
then cache it. For example if you are running a Rails application on
|
||||||
|
`example.com` and have a CDN configured at `mycdnsubdomain.fictional-cdn.com`,
|
||||||
|
then when a request is made to `mycdnsubdomain.fictional-
|
||||||
|
cdn.com/assets/smile.png`, the CDN will query your server once at
|
||||||
|
`example.com/assets/smile.png` and cache the request. The next request to the
|
||||||
|
CDN that comes in to the same URL will hit the cached copy. When the CDN can
|
||||||
|
serve an asset directly the request never touches your Rails server. Since the
|
||||||
|
assets from a CDN are geographically closer to the browser, the request is
|
||||||
|
faster, and since your server doesn't need to spend time serving assets, it can
|
||||||
|
focus on serving application code as fast as possible.
|
||||||
|
|
||||||
If you want to serve only some assets from your CDN, you can use custom
|
|
||||||
`:host` option of `asset_url` helper, which overwrites value set in
|
|
||||||
`config.action_controller.asset_host`.
|
|
||||||
|
|
||||||
```ruby
|
```ruby
|
||||||
asset_url 'image.png', :host => 'http://cdn.example.com'
|
asset_url 'image.png', :host => 'http://cdn.example.com'
|
||||||
|
|
Loading…
Reference in a new issue