Laravel 为什么不是';我的nginx web服务器是否处理ttf字体?

Laravel 为什么不是';我的nginx web服务器是否处理ttf字体?,laravel,nginx,cors,nginx-config,Laravel,Nginx,Cors,Nginx Config,我一直在研究这个问题,因为CORS限制,远程站点上没有呈现特定的字体文件 到目前为止,我已经能够确定对该url的请求是通过access control allow origin响应的,但nginx在远程发出时拒绝了对该字体的请求 我正在使用Spatial中的laravel和laravel cors插件,但我认为这不会返回未从laravel routes渲染的样式表或字体的标题信息 有人知道为什么会这样吗 我的错误 Access to font at 'https://example.com/cs

我一直在研究这个问题,因为CORS限制,远程站点上没有呈现特定的字体文件

到目前为止,我已经能够确定对该url的请求是通过access control allow origin响应的,但nginx在远程发出时拒绝了对该字体的请求

我正在使用Spatial中的laravel和laravel cors插件,但我认为这不会返回未从laravel routes渲染的样式表或字体的标题信息

有人知道为什么会这样吗

我的错误

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.
通过curl检索头(似乎允许curl请求)

我的nginx配置访问控制允许来源(设置为适当的区分大小写)。

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;
}
添加到传递ttf的Mime类型

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

我在试图让CORS在nginx+Lumen堆栈上正常工作时遇到了类似的错误。最后,我删除了用于启用CORS的nginx虚拟主机端配置,并使用自定义中间件处理请求。我没有使用过
laravel cors
插件,但是为了测试,您可以尝试以下简单的解决方案:

<?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;
    }
}

我通过将用于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;
    }
mime.types文件

mime.types edit. 
    application/x-font-ttf           ttc;
    application/x-font-otf           otf;
    application/font-woff2           woff2;
    font/ttf                         ttf;

我对nextcloud有问题。这足以添加woff2

   location ~* \.(?:svg|gif|png|html|ttf|woff|woff2|ico|jpg|jpeg)$ {
        try_files $uri /index.php$uri$is_args$args;
        # Optional: Don't log access to other assets
        access_log off;
   }

浏览器不关心标题的大小写。您应该考虑问题的另一个原因。你可能想冲破缓存;到期时间看起来很遥远。这并没有解决我的问题,但这是一个很好的解决cors问题的方法。
   location ~* \.(?:svg|gif|png|html|ttf|woff|woff2|ico|jpg|jpeg)$ {
        try_files $uri /index.php$uri$is_args$args;
        # Optional: Don't log access to other assets
        access_log off;
   }