CORS on Google Cloud Load Balancing looks easy right up until the browser starts throwing No 'Access-Control-Allow-Origin' header and your backend logs show perfectly healthy 200 responses.
That mismatch is the whole problem: your server can be fine, your load balancer can be fine, and the browser still blocks the response because the headers are wrong, missing, cached badly, or only added on the happy path.
I’ve seen the same set of mistakes over and over on Google Cloud external HTTP(S) load balancers. Most of them come down to one thing: treating CORS like an app feature when it’s really a browser contract enforced at the edge.
Here’s what breaks most often, and how to fix it.
Mistake #1: Only handling GET and forgetting preflight
A lot of teams test with a simple GET request, see it work, and assume CORS is done. Then the frontend adds:
AuthorizationContent-Type: application/jsonPUT,PATCH, orDELETE
Now the browser sends an OPTIONS preflight before the real request. If your backend service or load balancer doesn’t answer that preflight correctly, the actual request never happens.
Typical broken flow:
OPTIONS /api/user HTTP/1.1
Origin: https://app.example.com
Access-Control-Request-Method: PATCH
Access-Control-Request-Headers: authorization, content-type
Bad response:
HTTP/1.1 404 Not Found
Or:
HTTP/1.1 200 OK
Access-Control-Allow-Origin: *
That still fails if Access-Control-Allow-Methods and Access-Control-Allow-Headers are missing.
Fix
Make sure preflight requests return all the required headers:
HTTP/1.1 204 No Content
Access-Control-Allow-Origin: https://app.example.com
Access-Control-Allow-Methods: GET, POST, PUT, PATCH, DELETE, OPTIONS
Access-Control-Allow-Headers: Authorization, Content-Type
Access-Control-Max-Age: 3600
Vary: Origin
If you’re configuring CORS at the Google Cloud Load Balancer layer, verify that OPTIONS is actually handled there. If not, your backend must respond correctly for OPTIONS too.
If you’re doing this in app code, a simple Express example looks like this:
app.use((req, res, next) => {
const origin = req.headers.origin;
const allowedOrigins = new Set([
"https://app.example.com",
"https://admin.example.com",
]);
if (allowedOrigins.has(origin)) {
res.setHeader("Access-Control-Allow-Origin", origin);
res.setHeader("Vary", "Origin");
res.setHeader(
"Access-Control-Allow-Methods",
"GET, POST, PUT, PATCH, DELETE, OPTIONS"
);
res.setHeader(
"Access-Control-Allow-Headers",
"Authorization, Content-Type"
);
res.setHeader("Access-Control-Max-Age", "3600");
}
if (req.method === "OPTIONS") {
return res.status(204).end();
}
next();
});
Mistake #2: Using * with credentials
This one is classic. Somebody sets:
Access-Control-Allow-Origin: *
Access-Control-Allow-Credentials: true
Browsers reject that combination. If credentials are involved — cookies, Authorization headers, or TLS client certs — wildcard origin is not allowed.
Fix
Reflect or explicitly set the requesting origin, but only if it’s trusted.
Correct:
Access-Control-Allow-Origin: https://app.example.com
Access-Control-Allow-Credentials: true
Vary: Origin
Not correct:
Access-Control-Allow-Origin: *
Access-Control-Allow-Credentials: true
If your API is truly public and does not use credentials, wildcard can be fine. GitHub is a good real-world example of a public API using:
access-control-allow-origin: *
And exposing useful response headers:
access-control-expose-headers: ETag, Link, Location, Retry-After, X-GitHub-OTP, X-RateLimit-Limit, X-RateLimit-Remaining, X-RateLimit-Used, X-RateLimit-Resource, X-RateLimit-Reset, X-OAuth-Scopes, X-Accepted-OAuth-Scopes, X-Poll-Interval, X-GitHub-Media-Type, X-GitHub-SSO, X-GitHub-Request-Id, Deprecation, Sunset, Warning
That works because the API is designed for broad cross-origin access without browser credentials.
Mistake #3: Forgetting Vary: Origin
This gets ugly when you put Cloud CDN or any caching layer behind the load balancer.
Say request one comes from:
Origin: https://app.example.com
Your backend returns:
Access-Control-Allow-Origin: https://app.example.com
Then request two comes from:
Origin: https://admin.example.com
Without Vary: Origin, a cache may serve the first response to the second caller. Now the browser sees the wrong ACAO value and blocks it.
Fix
Any time you return different CORS headers depending on the requesting origin, include:
Vary: Origin
If you vary preflight responses based on requested method or headers, also consider:
Vary: Origin, Access-Control-Request-Method, Access-Control-Request-Headers
This matters a lot on Google Cloud Load Balancing when responses are cached or normalized upstream. If you skip Vary, you can end up debugging “random” CORS failures that are really cache poisoning by configuration.
Mistake #4: Adding CORS headers only on 200 responses
I’ve seen APIs add CORS headers in the success handler and nowhere else. Then auth fails with 401, validation fails with 400, or your backend throws 500, and the browser reports a CORS error instead of exposing the real response.
That makes frontend debugging miserable.
Fix
Add CORS headers on all relevant responses, including errors.
Bad pattern:
app.get("/api/data", (req, res) => {
res.setHeader("Access-Control-Allow-Origin", "https://app.example.com");
res.json({ ok: true });
});
Better:
app.use((req, res, next) => {
const origin = req.headers.origin;
if (origin === "https://app.example.com") {
res.setHeader("Access-Control-Allow-Origin", origin);
res.setHeader("Vary", "Origin");
}
next();
});
That way 401, 403, and 500 responses still carry the same CORS policy.
If you terminate or rewrite responses at the load balancer, check that header injection applies consistently and not just on one route matcher or backend path.
Mistake #5: Not allowing the headers your frontend actually sends
Browsers are strict here. If the frontend sends:
Authorization: Bearer ...
Content-Type: application/json
X-Requested-With: ...
the preflight asks for those headers explicitly. If your response doesn’t allow them, the browser blocks the request.
Broken preflight response:
Access-Control-Allow-Headers: Content-Type
But the request needed Authorization too.
Fix
Match your real traffic. Don’t guess.
A safe starting point for many APIs:
Access-Control-Allow-Headers: Authorization, Content-Type
If you need custom headers, list them too:
Access-Control-Allow-Headers: Authorization, Content-Type, X-Request-ID
Keep this list tight. Don’t mindlessly allow every possible header if you don’t need to.
Mistake #6: Forgetting Access-Control-Expose-Headers
Your API may return useful headers, but browser JavaScript can only read a limited set unless you explicitly expose them.
This surprises people when pagination, rate limiting, or ETag-based caching depends on response headers.
Real-world example from api.github.com:
access-control-expose-headers: ETag, Link, Location, Retry-After, X-GitHub-OTP, X-RateLimit-Limit, X-RateLimit-Remaining, X-RateLimit-Used, X-RateLimit-Resource, X-RateLimit-Reset, X-OAuth-Scopes, X-Accepted-OAuth-Scopes, X-Poll-Interval, X-GitHub-Media-Type, X-GitHub-SSO, X-GitHub-Request-Id, Deprecation, Sunset, Warning
That’s a solid example of exposing exactly what clients may need.
Fix
If your frontend needs to read headers like ETag, Link, or rate-limit metadata, expose them:
Access-Control-Expose-Headers: ETag, Link, X-RateLimit-Remaining, X-RateLimit-Reset
Without that, the network tab will show the headers, but fetch() or XHR won’t let your code read them.
Mistake #7: Treating CORS as auth
CORS is not access control for non-browser clients. It does not protect your API from curl, mobile apps, server-side requests, or abuse.
I still run into setups where teams whitelist one frontend origin and think that means the API is secured. It isn’t. CORS only tells browsers when to expose a response to JavaScript.
Fix
Use real auth and authorization:
- session validation
- OAuth tokens
- signed JWT verification
- backend ACLs
- rate limiting
Use CORS only to define browser access rules.
If you’re reviewing edge security at the same time, this is also a good moment to check other headers like CSP, though that’s a separate topic. If you need a CSP reference, use https://csp-guide.com.
Mistake #8: Allowing every origin in production because “we’ll tighten it later”
That “later” usually never happens.
For internal tools, admin panels, or anything using cookies, I strongly recommend explicit origin allowlists. Wildcards are fine for intentionally public APIs, but they’re a bad default for private app backends.
Fix
Start with a strict allowlist:
const allowedOrigins = new Set([
"https://app.example.com",
"https://admin.example.com",
]);
Then reflect only approved origins:
const origin = req.headers.origin;
if (allowedOrigins.has(origin)) {
res.setHeader("Access-Control-Allow-Origin", origin);
res.setHeader("Access-Control-Allow-Credentials", "true");
res.setHeader("Vary", "Origin");
}
If your Google Cloud Load Balancer is doing the CORS handling, the same rule applies: configure explicit origins where possible instead of broad patterns you don’t really mean.
A practical checklist for Google Cloud Load Balancing
When CORS breaks behind Google Cloud Load Balancing, I check these in order:
- Does the response include
Access-Control-Allow-Originon the actual request? - Does
OPTIONSreturn a valid preflight response? - If credentials are used, is the origin explicit instead of
*? - Is
Vary: Originpresent when origin-specific responses are returned? - Are
Access-Control-Allow-HeadersandAccess-Control-Allow-Methodsaligned with real browser requests? - Are CORS headers present on error responses too?
- If the frontend reads response headers, are they listed in
Access-Control-Expose-Headers? - Is a cache or CDN serving stale CORS headers across origins?
That last one bites people more than they expect.
The official docs
For Google Cloud Load Balancing behavior and configuration details, use the official documentation:
CORS failures are rarely mysterious once you inspect the raw request and response. The trick is to stop thinking “my API returned 200, so it works” and start thinking like the browser. That’s where the real contract lives.