Rails资产Cloudfront fontawesome CORS [英] Rails assets Cloudfront fontawesome CORS
问题描述
我为此问题尝试了很多在stackoverflow/github上找到的解决方案,但我无法使其正常工作.
I tried a lot of solutions found on stackoverflow/github for this issue but I can't get it to work.
我正在使用 font-awesome-rails ,并且我将自己的资产预编译为生产.我已经在生产配置中为资产设置了CloudFront:
I'm using font-awesome-rails and I precompile my assets for production. I've set CloudFront for my assets in my production config:
config.action_controller.asset_host = "https://XXXX.cloudfront.net"
当我加载页面(从Chrome/Firefox开始,因为Safari可以通过CORS正常运行)时,我收到以下常见错误消息:
When I load a page (from Chrome/Firefox because Safari is OK with CORS) I get this common error message :
Font from origin 'https://XXXX.cloudfront.net' has been blocked from loading by Cross-Origin Resource Sharing policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'https://example.com' is therefore not allowed access.
我已经尝试过:
I already tried to :
- 添加 gem rack-cors 和设置服务器标头没有成功
- 在CloudFront上创建无效失败的三个fontawesome文件
- Add the gem rack-cors and change my config.ru without success
- Set an
after_filter
to set the server headers without success - Create an invalidation on CloudFront for the three fontawesome files without success
一种解决方法是删除fontawesome宝石并改为使用:
A workaround would be to remove the fontawesome gem and use instead:
<%= stylesheet_link_tag "//cdnjs.cloudflare.com/ajax/libs/font-awesome/4.4.0/css/font-awesome.min.css", :media => "all" %>
但是我宁愿找到问题.
谢谢
推荐答案
我遇到了这个问题,并通过以下更改解决了该问题.首先,在您的http服务器上设置标头 add_header Access-Control-Allow-Origin *;
I have had this issue and it was solved by doing the following changes. First of all, set a header on your http server
add_header Access-Control-Allow-Origin *;
使用的完整配置:
location ~* \.(ttf|ttc|otf|eot|woff|woff2|svg|font.css)$ {
add_header Access-Control-Allow-Origin *;
expires max;
allow all;
access_log off;
add_header Cache-Control "public";
}
,然后在Cloudfront上创建失效.
and then create the invalidation on Cloudfront.
ps:我没有使用架芯或其他任何宝石
ps: I did not use rack-cors or any other gem
这篇关于Rails资产Cloudfront fontawesome CORS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!