为什么我的 nginx Web 服务器不处理 ttf 字体?

Dom*_*Dom 0 nginx cors laravel nginx-config

我一直在研究这个问题,由于 CORS 限制,特定字体文件未在远程站点上呈现。

到目前为止,我已经能够确定对该 url 的请求正在使用 access-control-allow-origin 进行响应,但 nginx 在远程发出时拒绝对该字体的请求。

我正在使用 laravel 和 spatie 的 laravel-cors 插件,但我认为这不会返回未从 laravel 路由渲染的样式表或字体的标头信息。

有谁知道为什么会发生这种情况?

我的错误

Access to font at 'https://example.com/css/widget-icons/icomoon.ttf?wiodlm' from origin 'http://www.second-example.com' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.
Run Code Online (Sandbox Code Playgroud)

通过curl检索标头(似乎允许curl请求)

curl -I https://example.com/css/widget-icons/icomoon.ttf?wiodlm

HTTP/2 200 
date: Sun, 13 Jan 2019 16:57:34 GMT
content-type: application/x-font-ttf
content-length: 1316
set-cookie: AWSALB=r3yRudj6XwTlfaFzEdtxrecHzLLplOKlpRMbKuqL8InwUQYylNVFaZtmGHK2wQDgjvaXsBtRVcTCyjWidjTFUFmoDzKLBLH0gL6qarns38Qn4FuDNCZogawHtOjD; Expires=Sun, 20 Jan 2019 16:57:34 GMT; Path=/
server: nginx/1.14.1
last-modified: Tue, 25 Dec 2018 05:42:49 GMT
etag: "5c21c359-524"
expires: Thu, 31 Dec 2037 23:55:55 GMT
cache-control: max-age=315360000
access-control-allow-origin: *
accept-ranges: bytes
Run Code Online (Sandbox Code Playgroud)

我的 nginx 配置为 Access-Control-Allow-Origin (已设置为正确的区分大小写)。

location ~* .(gif|jpg|jpeg|png|ico|wmv|3gp|avi|mpg|mpeg|mp4|flv|mp3|mid|js|css|wml|swf|ttf|ttc|otf|eot|woff|woff2)$ {
        add_header Access-Control-Allow-Origin "*";
        expires max;
}
Run Code Online (Sandbox Code Playgroud)

添加 Mime 类型以传递 ttf

application/x-font-ttf           ttc ttf;
application/x-font-otf           otf;
application/font-woff                            woff;
application/font-woff2           woff2;
Run Code Online (Sandbox Code Playgroud)

Dom*_*Dom 8

我通过将 ttf 使用的 mime.types 从 application/x-font-ttf 修改为 font/ttf 解决了这个问题。

我的 nginx 配置

    location ~* .(js|css|ttf|ttc|otf|eot|woff|woff2)$ {
            add_header access-control-allow-origin "*";
            expires max;
    }
Run Code Online (Sandbox Code Playgroud)

mime.types 文件

mime.types edit. 
    application/x-font-ttf           ttc;
    application/x-font-otf           otf;
    application/font-woff2           woff2;
    font/ttf                         ttf;
Run Code Online (Sandbox Code Playgroud)