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

superfive33's avatar

Header Access-Control-Allow-Origin for static file in Laravel Sail

Hi,

I'm using Laravel Sail, and I need to have a static file inside my public directory to be served with the header ' Access-Control-Allow-Origin: *'. But I don't know how to add this header in Laravel Sail since it does not use either niginx nor apache (but php artisan serve). Is that possible?

Thank you. Max.

0 likes
3 replies
alden8's avatar

@superfive33

Laravel Sail doesn't directly use Apache or Nginx.

-- inside your Laravel project's root directory, create a file named nginx.conf

-- in nginx.conf file:

server {
    location /your-static-file.ext {
        add_header 'Access-Control-Allow-Origin' '*';
    }
}

Replace your-static-file.ext with the actual path and filename of the static file you want to serve with the header.

-- in your terminal:

sail build --no-cache

This will rebuild the Sail containers, incorporating your custom Nginx configuration.

superfive33's avatar

@alden8 Thank you. But if Sail doesn't use Nginx, how can this nginx.conf file be useful ?

alden8's avatar

@superfive33

try this setup:

-- middleware approach:

--- create a middleware class that adds the header to the response for specific routes or files

--- register the middleware in your app/Http/Kernel.php file

--- this approach offers more flexibility and control over which files and origins are allowed

-- PHP header function:

--- modify your existing route or controller logic to directly add the header using the header() function before sending the response

--- this method is simpler but less reusable compared to middleware

-- .htaccess file:

--- if you're using a local development environment with Apache set up (e.g., XAMPP), you can create an .htaccess file in your public directory with the appropriate Header directive

--- this approach only works in certain development setups and won't affect the Sail development environment itself

Please or to participate in this conversation.