Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
d736e9e4
R
rails
项目概览
张重言
/
rails
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
R
rails
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
d736e9e4
编写于
8月 24, 2014
作者:
S
schneems
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
[ci skip] docs asset_pipeline CDN set up
上级
a05b46cb
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
63 addition
and
1 deletion
+63
-1
guides/source/asset_pipeline.md
guides/source/asset_pipeline.md
+63
-1
未找到文件。
guides/source/asset_pipeline.md
浏览文件 @
d736e9e4
...
...
@@ -938,9 +938,71 @@ 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.
#### Set up a CDN to Serve Static Assets
To set up your CDN you have to have your application running in production on
the internet at a publically available URL, for example
`example.com`
. Next
you'll need to sign up for a CDN service from a cloud hosting provider. When you
do this you need to configure the "origin" of the CDN to point back at your
website
`example.com`
, check your provider for documentation on configuring the
origin server.
The CDN you provisioned should give you a custom subdomain for your application
such as
`mycdnsubdomain.fictional-cdn.com`
(note fictional-cdn.com is not a
valid CDN provider at the time of this writing). Now that you have configured
your CDN server, you need to tell browsers to use your CDN to grab assets
instead of your Rails server directly. You can do this by configuring Rails to
set your CDN as the asset host instead of using a relative path. To set your
asset host in Rails, you need to set
`config.action_controller.asset_host`
in
`config/production.rb`
:
```
ruby
asset_url
'image.png'
,
:host
=>
'http://cdn.example.com'
config
.
action_controller
.
asset_host
=
'mycdnsubdomain.fictional-cdn.com'
```
Note: You only need to provide the "host", this is the subdomain and root
domain, you do not need to specify a protocol or "scheme" such as
`http://`
or
`https://`
. When a web page is requested, the protocol in the link to your asset
that is generated will match how the webpage is accessed by default.
You can also set this value through an
[
environment
variable
](
http://en.wikipedia.org/wiki/Environment_variable
)
to make running a
staging copy of your site easier:
```
config.action_controller.asset_host = ENV['CDN_HOST']
```
Note: You would need to set
`CDN_HOST`
on your server to
`mycdnsubdomain
.fictional-cdn.com`
for this to work.
Once you have configured your server and your CDN when you serve a webpage that
has an asset:
```
erb
<%=
asset_path
(
'smile.png'
)
%>
```
Instead of returning a path such as
`/assets/smile.png`
(digests are left out
for readability). The URL generated will have the full path to your CDN.
```
http://mycdnsubdomain.fictional-cdn.com/assets/smile.png
```
If the CDN has a copy of
`smile.png`
it will serve it to the browser and your
server doesn't even know it was requested. If the CDN does not have a copy it
will try to find it a the "origin"
`example.com/assets/smile.png`
and then store
it for future use.
If you want to serve only some assets from your CDN, you can use custom
`:host`
option your asset helper, which overwrites value set in
`config.action_controller.asset_host`
.
```
erb
<%=
asset_path
'image.png'
,
host:
'mycdnsubdomain.fictional-cdn.com'
%>
```
Customizing the Pipeline
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录