繁体   English   中英

Azure Cloud Service CDN字体CORS错误

[英]Azure Cloud Service CDN font CORS error

我一直在关注本教程中启用Azure CDN for Cloud Services: 链接 我将我的捆绑和缩小与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.

我厌倦了从我的问题中找到解决方案并将这些行添加到我的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>

但这没有帮助,我也找到了这样的问题和答案: 链接 ,但这只有在你使用CDN + Blob存储(我没有使用)的情况下才有帮助

我怎么解决这个问题?

编辑

我从我的软件包中删除了字体,并将它们连接起来没有CDN,这就是诀窍,但这并不能解决这个问题

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>

对于来自Azure CDN(以cdn/开头的url)的每个请求,包含非空的Origin: http标头,它将Access-Control-Allow-Origin: * http标头添加到响应中。

回答这个问题,以防其他人寻求解决方案。 在Azure CDN中,您可以在Verizon Premium配置文件上设置规则以启用CORS。

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

这还允许您指定允许访问的域的显式列表,而不仅仅是通配符。

在使用我的Azure Web应用程序作为源创建Azure CDN时,我遇到了同样的问题。

最后,我使用了类似的解决方法。

我替换了@ font-face声明的src属性来使用google fonts cdn。 对每个人来说并不总是一个选择,但它对我有用。

 @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; } 

查看Google提供的所有字体: https//fonts.google.com/

希望这有助于某人!

暂无
暂无

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

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