【问题标题】:Font files are blocked by CORS policy字体文件被 CORS 策略阻止
【发布时间】:2018-12-10 11:14:14
【问题描述】:

我想构建一个由 CDN 提供商加速的服务器,它为我的其他网站提供静态文件。我决定使用https://www.funfun.org.cn,那么例如https://www.funfun.org.cn/libs/font-awesome.min.csshttps://www.funfun.org.cn/libs/octicons.min.css应该可以被其他网站使用。

但是,当我在 localhost 中通过我的网站测试此服务器时,我看到以下错误:

例如,

从 'https://www.funfun.org.cn/libs/octicons.woff' 访问字体 来源“https://localhost:3000”已被 CORS 策略阻止:否 请求中存在“Access-Control-Allow-Origin”标头 资源。因此不允许使用原点“https://localhost:3000” 访问。

其他错误与https://www.funfun.org.cn/fonts/fontawesome-webfont.woff2?v=4.4.0https://www.funfun.org.cn/libs/octicons.ttf等类似

在我的网站中,上面的那些文件并没有直接调用,我只有以下两行关于字体的,一定是调用了.ttf.woff2等。

<link rel="stylesheet" href="https://www.funfun.org.cn/libs/font-awesome.min.css"> 
<link rel="stylesheet" href="https://www.funfun.org.cn/libs/octicons.min.css">

那么,有谁知道我可以做些什么来修复这些错误?

PS:我的nginx配置文件如下,所有文件都在/var/www/html/libs//var/www/html/fonts/下。

server {
    listen 443 ssl;

    ssl_certificate /etc/nginx/cert/1530490648293.pem;
    ssl_certificate_key /etc/nginx/cert/1530490648293.key;

    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_prefer_server_ciphers on;
    ssl_dhparam /etc/ssl/certs/dhparam.pem;
    ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
    ssl_session_timeout 1d;
    ssl_stapling on;
    ssl_stapling_verify on;
    add_header Strict-Transport-Security max-age=15768000;

    root /var/www/html;

    server_name funfun.org.cn www.funfun.org.cn;

    location / {
        try_files $uri $uri/ =404;
    }
}

【问题讨论】:

    标签: nginx cors font-awesome cdn octicons


    【解决方案1】:

    尝试将其添加到您的配置中:

    location ~* \.(eot|otf|ttf|woff|woff2)$ {
        add_header Access-Control-Allow-Origin *;
        expires max;
    }
    
    
    location / {
        try_files $uri $uri/ =404;
    }
    

    您可能还需要更新 nginx mine.types 文件,如下所示:

    application/x-font-ttf           ttc ttf;
    application/x-font-otf           otf;
    application/font-woff            woff;
    application/font-woff2           woff2;
    application/vnd.ms-fontobject    eot;
    

    【讨论】:

      【解决方案2】:

      检查您是否有足够的权限来读取这些文件。如果没有,则运行以下命令

      chmod 777 -R fonts_Root_Folder
      

      【讨论】:

        猜你喜欢
        • 2017-10-12
        • 1970-01-01
        • 2018-02-23
        • 2020-09-11
        • 2021-04-16
        • 2018-02-26
        • 2019-11-19
        • 2020-09-12
        • 2020-05-08
        相关资源
        最近更新 更多