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:
client_max_body_size 0;
server {
listen 80;
index index.php index.html;
server_name $SERVER_NAME;
root /app/public;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location /storage {
add_header 'Access-Control-Allow-Origin' *;
try_files $uri $uri/ =404;
}
location /temp {
add_header 'Access-Control-Allow-Origin' *;
try_files $uri $uri/ =404;
}
location ~ \.php$ {
fastcgi_pass backend:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
location ~ /\.ht {
deny all;
}
}
server {
listen 80;
# all subdomains proxy_pass to frontend
server_name *.$SERVER_NAME;
location / {
proxy_pass http://frontend:3000;
}
location ~ /\. {
deny all;
}
}
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;
}
}
Please or to participate in this conversation.