nuxt字体cors问题与cdn(cloudfront)

时间:2018-04-13 02:42:24

标签: nginx cors amazon-cloudfront cdn

我有一些cloudfront支持的字体,如下所示: https://xxx.cloudfront.net/_nuxt/fonts/fontawesome-webfont.b06871f.ttf

该文件可以在chrome中下载成功,与原始文件(https://example.com/_nuxt/fonts/fontawesome-webfont.b06871f.ttf)相同。

但我在chrome dev控制台中遇到错误,而且字体无效。

  

CORS策略阻止了对来自“https://xxx.cloudfront.net/_nuxt/fonts/fontawesome-webfont.b06871f.ttf”的“http://example.com”字体的访问:请求的资源上没有“Access-Control-Allow-Origin”标头。因此,不允许原点“http://example.com”访问。

这是我的云端设置

enter image description here

enter image description here

nginx设置:

upstream my_nodejs_upstream {
    server 127.0.0.1:3000;
    keepalive 64;
}

server {
  listen 80;
  listen 443 ssl http2;
  server_name example.com *.example.com;

  if ($http_user_agent ~* (Jorgee) ) {
     return 403;
  }

#  I try to add Access-Control-Allow-Origin header here, but not working
#  location ~* \.(ttf|ttc|otf|eot|woff|svg|font.css)$ {
#    add_header Access-Control-Allow-Origin *;
#  }

  location / {
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;
    proxy_set_header X-NginX-Proxy true;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";
    proxy_max_temp_file_size 0;
    proxy_pass http://my_nodejs_upstream/;
    proxy_redirect off;
    proxy_read_timeout 240s;
  }

}

卷曲的结果

curl -I https://xxx.cloudfront.net/_nuxt/fonts/fontawesome-webfont.b06871f.ttf

HTTP/2 200
content-type: application/x-font-ttf
content-length: 165548
date: Fri, 13 Apr 2018 02:43:21 GMT
server: nginx
accept-ranges: bytes
cache-control: public, max-age=31536000
last-modified: Fri, 13 Apr 2018 01:47:18 GMT
etag: W/"286ac-162bcaf7470"
vary: Accept-Encoding
x-cache: Miss from cloudfront
via: 1.1 20ec3b4214c4cf2bbb05faf96ff61033.cloudfront.net (CloudFront)
x-amz-cf-id: I-b_DNfst4q48vJtNRrzxCX2uSNi6yO1_BFSPVuWxRP1Q5Ii6AElUQ==

curl -I https://example.com/_nuxt/fonts/fontawesome-webfont.b06871f.ttf

HTTP/2 200
date: Fri, 13 Apr 2018 02:45:28 GMT
content-type: application/x-font-ttf
content-length: 165548
server: nginx
vary: Accept-Encoding
accept-ranges: bytes
cache-control: public, max-age=31536000
last-modified: Fri, 13 Apr 2018 01:47:18 GMT
etag: W/"286ac-162bcaf7470"

任何人都可以给我一些想法〜??

2 个答案:

答案 0 :(得分:1)

您需要在location /块中嵌套特定于字体的nginx配置块,如下所示:

upstream my_nodejs_upstream {
    server 127.0.0.1:3000;
    keepalive 64;
}

server {
  listen 80;
  listen 443 ssl http2;
  server_name example.com *.example.com;

  if ($http_user_agent ~* (Jorgee) ) {
    return 403;
  }

  location / {
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;
    proxy_set_header X-NginX-Proxy true;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";
    proxy_max_temp_file_size 0;
    proxy_pass http://my_nodejs_upstream/;
    proxy_redirect off;
    proxy_read_timeout 240s;

    location ~* \.(ttf|ttc|otf|eot|woff|svg|font.css)$ {
      add_header Access-Control-Allow-Origin *;
    }
  }
}

答案 1 :(得分:0)

Nginx设置应为

location / {
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;
    proxy_set_header X-NginX-Proxy true;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";
    proxy_max_temp_file_size 0;
    proxy_pass http://my_nodejs_upstream/;
    proxy_redirect off;
    proxy_read_timeout 240s;
    if ($request_uri ~* \.(ttf|ttc|otf|eot|woff|woff2|svg|font.css)$) {
      add_header Access-Control-Allow-Origin *;
    }
}