Laravel CORS and nginx generator

Get a config/cors.php for your frontend, the nginx server block from the Laravel 13 documentation, and the Sanctum settings a separate single-page app needs.

Your frontend
Scheme and host, with a port if there is one, and no path: https://app.example.com or http://localhost:5173. Leave it empty to allow any site.
0 sends no cache header. Chrome caps the value at 7200.
Your Laravel server
The folder that holds artisan. Nginx serves its public directory.
config/cors.php

To apply it

    Set CORS in one place

    Laravel answers CORS requests itself. The HandleCors middleware is in the global stack of every Laravel 13 app, with nothing to register. It reads config/cors.php, which does not exist until you publish it:

    php artisan config:publish cors

    This is why the nginx file here has no Access-Control-* lines. If nginx adds them as well, the browser receives two Access-Control-Allow-Origin headers and rejects the response. The nginx file is Laravel's documented server block with your domain, path and PHP version filled in.

    Wildcards and credentials do not mix

    A browser refuses a response that allows any origin and also allows credentials. When the frontend signs in with cookies, list each origin exactly. Bearer tokens are not credentials in this sense, so a token API can keep supports_credentials off.

    Sanctum with a separate frontend

    • The frontend and the API must share a top-level domain, such as app.example.com and api.example.com. Different subdomains are fine.
    • Call $middleware->statefulApi() in bootstrap/app.php.
    • List the frontend's host in SANCTUM_STATEFUL_DOMAINS, with its port if it has one.
    • Set the session cookie domain to the shared parent with a leading dot, such as .example.com.
    • In the frontend, turn on withCredentials and withXSRFToken for Axios, and request /sanctum/csrf-cookie before signing in.

    When a request still fails

    • No header at all: the path is not in paths, or the request never reached Laravel. A 500 error or a redirect from nginx carries no CORS headers.
    • Origin mismatch: origins are compared exactly. https://example.com and https://www.example.com are different, and a trailing slash never matches.
    • Old settings: after editing the file on a server with cached config, run php artisan config:cache again.
    • Not a CORS problem: requests from a server, curl or a mobile app ignore CORS. Only browsers enforce it.

    Sources: Laravel 13 documentation for CORS, Sanctum SPA authentication and nginx deployment, and MDN on CORS.

    Building a SaaS on Laravel? Start with the foundation already built.

    Stack Toast is a Laravel 13 starter kit with authentication, billing through four payment processors, teams and a complete admin panel. Choose Livewire, React, Vue or API.

    • From $199, one payment
    • Full GitHub source
    • Unlimited projects
    • 30-day money-back guarantee
    The Stack Toast admin dashboard with revenue and subscription metrics