Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

mgksmv's avatar
Level 1

Laravel CORS block after few hours

I'm running Laravel application in Docker (PHP-FPM, Nginx) and AFTER a few hours CORS error begins to appear randomly on POST requests. When I just start Docker everything works perfectly, CORS appears only after few hours. The app has backend API (Laravel) and frontend (Vue) in different folders. I found the same issue on GitHub https://github.com/laravel/sanctum/issues/239 but there's no solution how to solve it. What could cause this error? And why error occurs after few hours and not immediately? I also tried setting 777 permissions for /storage and /bootstrap/cache folders and adding add_header 'Access-Control-Allow-Origin' * in location / in Nginx. I have \Illuminate\Http\Middleware\HandleCors::class in the middleware stack in Kernel.php:

protected $middleware = [
    \App\Http\Middleware\TrustProxies::class,
    \Illuminate\Http\Middleware\HandleCors::class,
    \App\Http\Middleware\PreventRequestsDuringMaintenance::class,
    \Illuminate\Foundation\Http\Middleware\ValidatePostSize::class,
    \App\Http\Middleware\TrimStrings::class,
    \Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull::class,
];

cors.php:

<?php

return [
    'paths' => ['api/*', 'sanctum/csrf-cookie', 'storage/*'],
    'allowed_methods' => ['*'],
    'allowed_origins' => ['*'],
    'allowed_origins_patterns' => [],
    'allowed_headers' => ['*'],
    'exposed_headers' => [],
    'max_age' => 0,
    'supports_credentials' => true,
];

Nginx config looks like this in backend:

And like this in frontend:

server {
    listen 3000;
    server_name *.$SERVER_NAME;

    root /usr/share/nginx/html;
    index index.html;

    location / {
        try_files $uri /index.html;
    }

    location ~ /\. {
        deny all;
    }
}
0 likes
0 replies

Please or to participate in this conversation.