问题
I've been researching this issue where a specific font file isn't rendered on remote sites because of CORS restrictions.
So far I have been able to identify that requests for that url are responding with access-control-allow-origin, but nginx is rejecting requests for that font when made remotely.
I am using laravel and the laravel-cors plugin from spatie, but I wouldn't think that would return header information for a style sheet or font not rendered from laravel routes.
Does anyone know why this would happen?
My Error
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.
Header retrieved via curl (It seems to allow curl requests)
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
My nginx config to Access-Control-Allow-Origin (It's set to the proper case sensitivity).
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;
}
Mime types added to pass ttf
application/x-font-ttf ttc ttf;
application/x-font-otf otf;
application/font-woff woff;
application/font-woff2 woff2;
回答1:
I faced similar errors when trying to get CORS working properly on nginx + Lumen stack. Eventually, I ended up removing the nginx virtual host side config for enabling CORS and used custom middleware to handle the requests. I haven't used the laravel-cors
plugin, but for the sake of testing, you may try this simple solution:
<?php
namespace App\Http\Middleware;
use Closure;
class CorsMiddleware
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next) {
$headers = [
'Access-Control-Allow-Origin' => '*',
'Access-Control-Allow-Methods' => 'POST, GET, OPTIONS, PUT, DELETE',
'Access-Control-Allow-Credentials' => 'true',
'Access-Control-Max-Age' => '86400',
'Access-Control-Allow-Headers' => 'Origin, Content-Type, Authorization, X-Requested-With'
];
if ($request->isMethod('OPTIONS')) {
return response()->json('{"method":"OPTIONS"}', 200, $headers);
}
$response = $next($request);
foreach ($headers as $key => $value) {
$response->header($key, $value);
}
return $response;
}
}
回答2:
I resolved this issue by modifying the mime.types used for ttf from application/x-font-ttf to font/ttf.
my nginx config
location ~* .(js|css|ttf|ttc|otf|eot|woff|woff2)$ {
add_header access-control-allow-origin "*";
expires max;
}
mime.types file
mime.types edit.
application/x-font-ttf ttc;
application/x-font-otf otf;
application/font-woff2 woff2;
font/ttf ttf;
来源:https://stackoverflow.com/questions/54171710/why-isnt-my-nginx-web-server-handling-ttf-fonts