0

虽然多年来以各种形式提出了这个问题,但没有一个答案对我有用,而且有效的解决方案根本没有意义。
我希望有人能够理解为什么解决方案可以解决问题,或者帮助我找出真正的问题。
问题是我们的一些 woff2 font-awesome 字体无法正确显示,如下面的输出所示

Request URL: https://example.com/fontawesome-webfont.af7ae505a9eed503f8b8.woff2?v=4.7.0
Request Method: GET
Status Code: 200 
Remote Address: xxx.xxx.xxx.xxx:443
Referrer Policy: no-referrer-when-downgrade
content-encoding: br
content-type: text/html

如下添加位置块似乎可以解决问题,即使位置块本身什么也不做(至少根据我的理解)

location ~* \.(eot|otf|ttf|woff|woff2)$ {
}

我的 mime.types 中也有以下映射

font/ttf     ttf;
font/woff     woff;
font/woff2     woff2;
application/font-ttf     ttf;
application/font-woff     woff;
application/font-woff2     woff2;
application/x-font-ttf     ttc ttf;
application/x-font-otf     otf;
application/x-font-woff     woff;
application/x-font-woff2     woff2;

我们使用 brotli 和 gzip 进行压缩

4

1 回答 1

1

我也面临同样的问题。发布我的解决方案,以便它可以帮助某人。对我有用的解决方案

解决方案 1

location ~* \.(eot|otf|ttf|woff|woff2)$ {
   access_log      off;
   log_not_found   off;
   # expires 30d;
   add_header Access-Control-Allow-Origin *;

   types     {font/opentype otf;}
   types     {application/vnd.ms-fontobject eot;}
   types     {font/truetype ttf;}
   types     {application/font-woff woff;}
   types     {font/x-woff woff2;}
}

但是如果我在 mime.type 中添加以上内容,它就不起作用。所以为此我在下面尝试

解决方案 2

将此添加到mime.types

    font/opentype                       otf;
    font/truetype                       ttf;
    application/font-woff               woff;
    font/x-woff                         woff2;

由于定义的顺序, mime.types 被默认的 application/octet-stream 覆盖

nginx.conf

添加包含/etc/nginx/mime.types;再次在您的位置块内

location ~* \.(eot|otf|ttf|woff|woff2)$ {
   access_log      off;
   log_not_found   off;
   # expires 30d;
   include             /etc/nginx/mime.types;
   add_header Access-Control-Allow-Origin *;
}

笔记

如果您启用了缓存,请尝试清除服务器缓存一次。此外,如果您使用的是 cloudflare 之类的 CDN,请确保在进行此更改后清除缓存。

于 2020-08-22T16:00:35.297 回答