CORS error on server error – Laravel with Apache

  apache2, cors, docker, laravel, php

I am using Laravel 8 as API with Apache2.4 inside a Docker container, and I take requests from my Vue 2 APP using Axios.

But when I get a server error (500), caused by any code error, I get a CORS error. If I use my return pattern (code below), everything works fine.

return R::message('Error message!', R::MSG_ERROR, 500);

This code just return a JSON encoded string as below:

return response()->json(compact('message', 'type'), $status);

I’ve already tried to enable Apache header mod, allowed * in my CORS config, and some other things, but without success (also, APP_DEBUG is true).

My Laravel .htaccess is the default, and my Apache API vhost is as follows:

<VirtualHost *:80>
        ServerName api.project.local
        ServerAdmin [email protected]
        DocumentRoot /var/www/html/project-api/public

        ErrorLog ${APACHE_LOG_DIR}/api-error.log
        CustomLog ${APACHE_LOG_DIR}/api-access.log combined
</VirtualHost>

Reading the Response Headers, there is no Access-Control-Allow-Origin when I get a server error:

HTTP/1.0 500 Internal Server Error
Date: Tue, 23 Mar 2021 23:48:39 GMT
Server: Apache/2.4.38 (Debian)
X-Powered-By: PHP/7.4.16
Cache-Control: no-cache, private
Content-Length: 3159
Connection: close
Content-Type: application/json

Here is an example of a successful request:

HTTP/1.1 200 OK
Date: Tue, 23 Mar 2021 23:48:21 GMT
Server: Apache/2.4.38 (Debian)
X-Powered-By: PHP/7.4.16
Cache-Control: no-cache, private
X-RateLimit-Limit: 10000
X-RateLimit-Remaining: 9999
Access-Control-Allow-Origin: http://localhost:8080
Vary: Origin
Content-Length: 13
Keep-Alive: timeout=5, max=100
Connection: Keep-Alive
Content-Type: application/json

I think it might be a problem in my Apache configuration or something with Laravel error return, but I can’t understand what’s going on.

EDIT: I forgot to mention it, but when I get a CORS error, no logs are generated in Laravel or Apache, I can’t say why.

Source: Laravel

Leave a Reply