CORS Error: What is the correct way to configure Symfony 5 to accept CORS?
You are facing a classic cross-origin resource sharing (CORS) failure where your API works perfectly in tools like Insomnia or Postman, but fails immediately when called from a browser. The error message is explicit: Access-Control-Allow-Origin header is missing. This discrepancy isn't magic; it's how browsers enforce security policies that server-side testing tools bypass entirely.
When you attempt to fetch data from https://localhost:8000 while your Vue frontend runs on http://localhost:8080, the browser sends a preflight OPTIONS request before your actual GET. If the response to this preflight lacks the specific header, the browser blocks the request and throws the error you are seeing. The fact that it works in Postman confirms your Symfony logic is sound; the issue lies strictly in how the server responds to cross-origin requests versus same-origin ones.
The NelmioCorsBundle is the standard solution for this in Symfony 5, but its configuration requires precision. Your current setup uses a regex pattern in .env to define allowed origins:
CORS_ALLOW_ORIGIN='^http?://(localhost|127\.0\.0\.1)(:[0-9]+)?$'
While this regex appears robust on paper, it contains a critical logical flaw that often causes preflight failures. The allow_origin configuration in NelmioCorsBundle expects either a specific origin string or a valid wildcard logic. However, when using origin_regex: true, the library attempts to match the incoming request's origin against your regex.
The primary pitfall here involves how browsers send the Origin header versus how you are defining the allowlist. If your frontend runs on http://localhost:8080, the browser sends that exact string in the Origin header of the preflight request. Your regex currently allows localhost and 127.0.0.1. While technically correct, relying solely on regex for origins can be fragile depending on the specific version of NelmioCorsBundle and how the environment variable is parsed.
A more reliable approach in many Symfony projects is to define the allowed origin as a specific string rather than relying entirely on regex matching, unless you strictly need dynamic subdomain support. If your goal is to allow any protocol (http/https) on localhost with any port, you should ensure the .env variable matches the exact request or use a broader string if security permits.
Consider updating your .env file to be explicit about the specific origin you want to trust:
# Option A: Specific origin (Recommended for development isolation)
CORS_ALLOW_ORIGIN='http://localhost:8080,https://localhost:8000'
# Option B: If you truly need regex logic, ensure the syntax matches the bundle's expectation exactly
# CORS_ALLOW_ORIGIN='^http?://(localhost|127\.0\.0\.1)(:[0-9]+)?$'
In your config/packages/nelmio_cors.yaml, your current configuration sets allow_origin_regex to true. If you switch back to a static string list in .env, you might need to set allow_origin_regex: false in the YAML file, or ensure the regex engine in PHP correctly interprets your pattern. Mixing a static allowlist concept with a regex flag often leads to mismatches where the preflight request fails because the header isn't added at all.
Another common mistake is handling credentials. Your Vue code includes credentials: 'include'. This tells the browser to send cookies and authorization headers. When credentials are included, CORS rules become stricter. Browsers generally do not accept a wildcard (*) for Access-Control-Allow-Origin when credentials are involved. If your configuration inadvertently allows wildcards while credentials are set, the browser will block the request regardless of other settings. Ensure your Symfony config does not output * as the origin if you intend to use cookies or authentication tokens.
Finally, verify that the bundle is correctly registered and that no other middleware is stripping headers before NelmioCorsBundle can inject them. If you are deploying this in production, remember that exposing http://localhost:8080 might be fine for dev, but your production .env must restrict access to your actual application domain and not allow generic localhost patterns from unsecured networks.
By aligning the Origin header sent by your browser with the exact string or regex pattern defined in your Symfony environment, you will resolve the preflight blockage and allow your Vue app to communicate securely with your API.