跨域字体未加载

Cross-Domain Fonts not loading

我在 http://sugarcoated.co 的 Digital Ocean 上建立了一个网站。该域名是通过 Hover 购买的。

此站点的 IP 地址是 45.55.209.201

当我在浏览器中加载 IP 地址 http://45.55.209.201 时,所有字体都加载正常,但是当我加载 http://sugarcoated.co 时,我得到CORS 错误消息。

Access to Font at 'http://45.55.209.201/wp-content/themes/sugarcoated/css/fonts/montserrat/montserrat-regular-webfont.woff2' from origin 'http://sugarcoated.co' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://sugarcoated.co' is therefore not allowed access.

我是服务器配置的新手,不确定我可能错过了哪个设置。

悬停时我设置了这样的域:

在Digital Ocean中,我的DNS记录如下:

对于浏览器来说,http://sugarcoated.cohttp://45.55.209.201 是完全不同的来源。

https://developer.mozilla.org/en-US/docs/Web/Security/Same-origin_policy and https://en.wikipedia.org/wiki/Same-origin_policy 所述,浏览器遵循的 same-origin 策略是 Web 安全模型的核心,可防止对某些 类 资源的请求 cross-origin。

字体是 类 默认阻止请求的资源之一 cross-origin。

对于允许字体请求的浏览器 cross-origin,来自字体的服务器必须发送允许它的 Access-Control-Allow-Origin 响应 header。

因此,为了让浏览器允许 http://sugarcoated.co 使用来自 http://45.55.209.201 的字体,来自 http://45.55.209.201/wp-content/themes/sugarcoated/css/fonts/… 的响应必须包含 Access-Control-Allow-Origin: http://sugarcoated.co 响应 header 以允许只是 http://sugarcoated.co 使用字体,否则 Access-Control-Allow-Origin: * 允许任何来源使用字体。

或者您只需要在需要字体的整个文档源中将 http://45.55.209.201 更改为 http://sugarcoated.co,否则只需使用 URL 之类的 /wp-content/themes/sugarcoated/css/fonts/montserrat/montserrat-regular-webfont.woff2 ] 而不是包含主机名的绝对 URL。

不要紧,下面http://sugarcoated.cohttp://45.55.209.201其实是同一个服务器。浏览器无法知道这一点。为了让浏览器将它们视为同源,主机部分必须完全匹配,character-for-character.