Answer a question

I'm currently configuring my nginx server for a laravel application and added the Access-Control-Allow-Origin header. Suddenly my browser threw an error saying that there are multiple header values present for this key. Turns out there's a cors.php configuration available in Laravel. Now i'm a bit confused whether it's a good idea to let this header be handled by the php application itself or whether my nginx server should be responsible for this.

Is there a best practice on how to deal with this?

Answers

Using laravels cors feature, you can attach the cors headers to specific routes. But you can only attach it on laravel routes, your static assets such as css files, js, images, fonts, etc will not be covered by the cors since they are accessed directly from filesystem without entering the laravel application.

On the other hand, if you set cors in nginx, it will affect all requests ( or based on how you configure )

So based on your requirement, you can choose which you need. If you need the cors headers for everything, you'll need to set the header through nginx. If you instead need cors only for some specific routes ( such as all routes starting with /api ) then you can use laravels cors

Logo

开发云社区提供前沿行业资讯和优质的学习知识,同时提供优质稳定、价格优惠的云主机、数据库、网络、云储存等云服务产品

更多推荐