我使用rack-cors有一个rails 4.2 app。即使我的所有其他资产都很好,我的所有字体都不会显示出来。我正在使用rackspace cdn。我的CDN网址看起来像
http://ddf908e003b5678bc25-9d6bfcdc12345678ba868a15bca98.r12.cf5.rackcdn.com/assets/main-4f3595479ce96112e1b8ab4e5357fc26.css
我的rack-cors配置位于我的config/application.rb
config.middleware.insert_before 0, "Rack::Cors" do
allow do
origins 'localhost:3000', /^http:\/\/\w+.+rackcdn.com/
resource '/assets/*', headers: :any, methods: :get
end
end
图标显示在本地就好了。它只是在他们没有表现出来的制作中。关于我可能遗失的任何想法?
修改
我正在使用font-awesome-sass宝石。当我查看页面时,我没有得到任何javascript错误或任何内容,只有CORS警告,没有图标。
Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://ddf908e003b5678bc25-9d6bfcdc12345678ba868a15bca98.r12.cf5.rackcdn.com/assets/main-4f3595479ce96112e1b8ab4e5357fc26.css/assets/font-awesome/fontawesome-webfont-e9c0e802c2b0faf4d660e2bff17b5cf1.woff. This can be fixed by moving the resource to the same domain or enabling CORS.
编辑2
我使用asset_sync帮助设置Ash Wilson建议的自定义标题。
AssetSync.configure do |config|
config.fog_provider = 'Rackspace'
config.rackspace_username = ENV['RACKSPACE_USERNAME']
config.rackspace_api_key = ENV['RACKSPACE_API_KEY']
config.fog_directory = ENV['FOG_DIRECTORY']
config.fog_region = ENV['FOG_REGION']
config.gzip_compression = true
config.manifest = true
config.custom_headers = {
"\.(ttf|otf|eot|woff|svg)$" => {
"Access-Control-Allow-Origin" => "*",
"Access-Control-Request-Method" => "*",
"Access-Control-Allow-Methods" => "*"
}
}
end
我还没有对我进行任何缓存,但我确定要清除任何浏览器缓存。我还发现2个文件在控制面板中抱怨.woff
和.ttf
,如下面提到的Ash,我手动将标题添加到其中。但是,我仍然看到同样的问题。也许还有一个额外的步骤,或者可能AssetSync刚刚死了而且不起作用?
编辑3
以下是卷曲的标题。看起来CORS标题就在那里。等待整天传播,但仍然没有。
curl -i -s -XHEAD http://ddf908e003b5678bc25-9d6bfcdc12345678ba868a15bca98.r12.cf5.rackcdn.com/assets/font-awesome/fontawesome-webfont-e0e0f28a0197446b28f818aa81b80530.ttf
HTTP/1.1 200 OK
Content-Length: 112160
Last-Modified: Thu, 07 May 2015 16:49:12 GMT
Accept-Ranges: bytes
Access-Control-Request-Method: *
ETag: c4668ed2440df82d3fd2f8be9d31d07d
X-Timestamp: 1431017351.82062
Access-Control-Allow-Origin: *
Access-Control-Allow-Methods: GET, HEAD, OPTIONS
Content-Type: application/font-sfnt
X-Trans-Id: tx53b90a9b638141c6874e6-00554bf992iad3
Cache-Control: public, max-age=259200
Expires: Sun, 10 May 2015 23:47:30 GMT
Date: Thu, 07 May 2015 23:47:30 GMT
Connection: keep-alive
答案 0 :(得分:1)
要使CORS正常工作,您需要在Rackspace端的Cloud Files对象上设置CORS标头,而不是应用程序提供的标头。您可以通过点击每个文件旁边的齿轮图标从control panel执行此操作:
Cloud Files documentation包含一个支持CORS标题的部分以及各自的含义。
如果您拥有大量资产或自动发布它们的构建过程,您将需要使用API或SDK来设置这些资产。 developer documentation包含您可以采用的代码段;将“Access-Control-Allow-Origin”替换为“Content-Type”的其他相关标题。
这是使用Ruby SDK的完整示例,Fog:
require 'fog'
# Authenticate
@client = Fog::Storage.new(
:provider => 'rackspace',
:rackspace_username => '{username}',
:rackspace_api_key => '{apiKey}',
:rackspace_region => '{region}'
)
# Find the file object
@directory = @client.directories.get("{container name}")
@file = @directory.get("{file name}")
# To only allow CORS requests from a page hosted at 'my-domain.com'
# You can also set this to '*' to allow CORS requests from anywhere
@file.access_control_allow_origin = "http://my-domain.com"
@file.save