简体   繁体   English

Azure Cloud Service CDN字体CORS错误

[英]Azure Cloud Service CDN font CORS error

I've been following this tutorial for enabling Azure CDN for Cloud Services: link . 我一直在关注本教程中启用Azure CDN for Cloud Services: 链接 And I integrated my bundling and minification with CDN and everything works fine except my site cannot fetch fonts, I'm getting this error: 我将我的捆绑和缩小与CDN集成,一切正常,除了我的网站无法获取字体,我收到此错误:

Font from origin 'http://azurecdn.vo.msecnd.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 'http:// mysite.com' is therefore not allowed access.

I tired to find solution from my problem and added these lines to my Web.config file: 我厌倦了从我的问题中找到解决方案并将这些行添加到我的Web.config文件中:

  <httpProtocol>
  <customHeaders>
    <add name="access-control-allow-origin" value="*" />
    <add name="access-control-allow-headers" value="content-type" />
  </customHeaders>
</httpProtocol>
   <rewrite>
  <outboundRules>
    <rule name="Set Access-Control-Allow-Origin header">
      <match serverVariable="RESPONSE_Access-Control-Allow-Origin" pattern="(.*)" />
      <action type="Rewrite" value="*" />
    </rule>
  </outboundRules>
  <rules>

    <rule name="RewriteIncomingCdnRequest" stopProcessing="true">
      <match url="^cdn/(.*)$"/>
      <action type="Rewrite" url="{R:1}"/>
    </rule>
  </rules>
</rewrite>

But that didn't help, also I found questions & answers such as this: link , but this helps only if you're using CDN + Blob storage (which I'm not using) 但这没有帮助,我也找到了这样的问题和答案: 链接 ,但这只有在你使用CDN + Blob存储(我没有使用)的情况下才有帮助

How can I solve this ? 我怎么解决这个问题?

EDIT 编辑

I removed fonts from my bundle and linked them without CDN and that did the trick, but that isn't exactly solution to this problem 我从我的软件包中删除了字体,并将它们连接起来没有CDN,这就是诀窍,但这并不能解决这个问题

This rewrite configuration for Azure CDN is working for me... Azure CDN的这种重写配置对我有用......

<rewrite>
    <outboundRules>
        <rule name="Set Access Control Allow Origin Header" preCondition="Origin header">
            <match serverVariable="RESPONSE_Access-Control-Allow-Origin" pattern="(.*)" />
            <action type="Rewrite" value="*" />
        </rule>
        <preConditions>
            <preCondition name="Origin header" logicalGrouping="MatchAll">
                <add input="{HTTP_ORIGIN}" pattern="(.+)" />
                <add input="{HTTP_X_ORIGINAL_URL}" pattern="^/cdn/(.*)$" />
            </preCondition>
        </preConditions>
    </outboundRules>
    <rules>
        <rule name="Rewrite CDN requests" stopProcessing="true">
            <match url="^cdn/(.*)$" />
            <action type="Rewrite" url="{R:1}" />
        </rule>
    </rules>
</rewrite>

For every request from Azure CDN (url starting with cdn/ ) containing non empty Origin: http header it adds Access-Control-Allow-Origin: * http header to response. 对于来自Azure CDN(以cdn/开头的url)的每个请求,包含非空的Origin: http标头,它将Access-Control-Allow-Origin: * http标头添加到响应中。

Answering this in case it helps other people looking for a solution. 回答这个问题,以防其他人寻求解决方案。 In Azure CDN, you can set rules on the Verizon Premium profile to enable CORS as well. 在Azure CDN中,您可以在Verizon Premium配置文件上设置规则以启用CORS。

https://azure.microsoft.com/en-us/documentation/articles/cdn-cors/ https://azure.microsoft.com/en-us/documentation/articles/cdn-cors/

This will also allow you to specify an explicit list of domains allowed access instead of just a wildcard. 这还允许您指定允许访问的域的显式列表,而不仅仅是通配符。

I had the same issue when creating an Azure CDN with my Azure web app as the origin. 在使用我的Azure Web应用程序作为源创建Azure CDN时,我遇到了同样的问题。

In the end I used a similar workaround to you. 最后,我使用了类似的解决方法。

I replaced the src attribute of my @font-face declarations to use the google fonts cdn. 我替换了@ font-face声明的src属性来使用google fonts cdn。 Not always an option for everyone but it worked for me. 对每个人来说并不总是一个选择,但它对我有用。

 @font-face { font-family: 'Roboto'; font-style: normal; font-weight: 400; src: local('Roboto'), local('Roboto-Regular'), url(https://fonts.gstatic.com/s/roboto/v15/NdF9MtnOpLzo-noMoG0miPesZW2xOQ-xsNqO47m55DA.woff2) format('woff2'); unicode-range: U+0102-0103, U+1EA0-1EF9, U+20AB; } 

Check out all the fonts google have available here: https://fonts.google.com/ 查看Google提供的所有字体: https//fonts.google.com/

Hope this helps someone! 希望这有助于某人!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM