Laravel:为静态文件添加Cors标头 [英] Laravel : add Cors header for static files
问题描述
我有一个Laravel应用程序,该应用程序托管在Apache上,但现在已在nginx上迁移.我是nginx的新手.
在Apache上,我的htaccess文件中有此内容:
I have a Laravel app, which was hosted on Apache, but now has been migrated on nginx. I'm a totally newbie with nginx.
On Apache I had this in my htaccess :
<IfModule mod_headers.c>
<FilesMatch "\.(svg|ttf|otf|eot|woff|woff2)$">
Header set Access-Control-Allow-Origin "*"
</FilesMatch>
</IfModule>
<IfModule mod_headers.c>
<FilesMatch "\.(svg|ttf|otf|eot|woff|woff2)$">
Header set Access-Control-Allow-Origin "*"
</FilesMatch>
</IfModule>
新的托管服务提供商不允许自定义nginx配置.
The new hosting provider does not allow custom nginx configuration.
是否可以在Laravel应用PHP代码中为静态字体文件(扩展名:svg | ttf | otf | eot | woff | woff2)添加Cors标头(Access-Control-Allow-Origin:*)?我尝试过(添加Access-Control- Laravel 5.3 Passport中的Allow-Origin标头响应)没有成功,我想那段代码不会将静态文件作为目标.你确认吗?
Is it possible to add a Cors header (Access-Control-Allow-Origin: *) for static font files (extensions : svg|ttf|otf|eot|woff|woff2) in the Laravel app PHP code ? I tried (Adding Access-Control-Allow-Origin header response in Laravel 5.3 Passport) without success, my guess is that static files are not targeted by that piece of code. Do you confirm ?
有没有一种方法可以在我的应用程序的PHP代码中实现此目标?
Is there a way to achieve this within my app's PHP code ?
谢谢
推荐答案
在服务器块或nginx.conf
中使用此选项可全局应用.
Use this in you server block or nginx.conf
to apply globally.
location ~* \.(svg|ttf|otf|eot|woff|woff2)$ {
add_header Access-Control-Allow-Origin *;
}
请确保重新启动Nginx服务器,以使更改生效.
Make sure to restart nginx server for changes to take effect.
这篇关于Laravel:为静态文件添加Cors标头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!