CORS preflight failing only in production: the checklist that finds it
Your API works on localhost, then the live site throws a CORS preflight error. Here is the checklist I use to find the cause: OPTIONS handling, origin mismatches, credentials, Nginx headers and the network tab.

Everything worked on your laptop. You deploy, open the live site, and the console fills with red: a CORS preflight error, and every API call fails before it even reaches your code. This is one of the most common "but it works locally" bugs, and it almost always comes down to a short list of causes.
I have chased this bug on more than one platform. The fix is rarely clever. It is usually a missing header, an origin that is off by one character, or Nginx doing something you did not ask it to do. Here is the checklist I go through, in order.
Why CORS works locally but not in production
On your machine you probably never had CORS at all. Tools like Vite or Create React App ship a dev proxy: the browser talks to localhost:5173, and the dev server forwards /api calls to your backend. To the browser that is the same origin, so no preflight happens.
In production the frontend sits on https://example.com and the API on https://api.example.com. Different origin, so the browser enforces CORS. For any request that is not "simple" (a PUT or DELETE, a JSON body, or an Authorization header) it first sends an OPTIONS request, the preflight, and asks the server for permission. If the answer is wrong or missing, the real request is never sent.
So when CORS works locally but not in production, the code did not suddenly break. Production is the first place the preflight ever ran.
Read the browser network tab first
Before you touch any config, open DevTools, go to the Network tab, reload, and trigger the failing call.
- Look for the OPTIONS row next to your real request. Chrome labels it as a preflight. If you only see the real request marked "CORS error", click it and check the console message.
- Click the OPTIONS row and check three things: the status code, the response headers, and whether there is a redirect.
- Read the console error word for word. It usually tells you exactly which check failed: "No 'Access-Control-Allow-Origin' header is present", "contains multiple values", "Redirect is not allowed for preflight request", or "must not be the wildcard '*' when the request's credentials mode is 'include'".
You can also reproduce the preflight outside the browser with curl. This takes the frontend out of the picture:
curl -i -X OPTIONS https://api.example.com/v1/orders \
-H "Origin: https://www.example.com" \
-H "Access-Control-Request-Method: POST" \
-H "Access-Control-Request-Headers: content-type, authorization"
A healthy response is a 204 or 200 that includes Access-Control-Allow-Origin, Access-Control-Allow-Methods and Access-Control-Allow-Headers. Anything else gives you your next step.
Check 1: is the OPTIONS request handled at all?
The most common cause: the preflight hits a route that does not exist, or it hits your auth middleware first.
Your route is app.post('/v1/orders'). Nobody defined OPTIONS /v1/orders, so it returns 404. Or your JWT middleware runs before CORS, sees no token (preflights never carry your Authorization header), and returns 401. Either way, the browser blocks the request.
In Express, register CORS before everything else, including auth:
const express = require('express');
const cors = require('cors');
const app = express();
const allowedOrigins = [
'https://example.com',
'https://www.example.com',
];
app.use(cors({
origin(origin, callback) {
// Allow non-browser tools (no Origin header) and listed origins
if (!origin || allowedOrigins.includes(origin)) {
return callback(null, true);
}
return callback(null, false);
},
credentials: true,
}));
// Auth and routes come after CORS
app.use('/v1', requireAuth, apiRouter);
Used with app.use, the cors package answers OPTIONS requests itself, before your auth and routes run. If you write your own middleware, make sure it ends OPTIONS requests early with a 204 instead of passing them on.
Check 2: origin mismatch (www vs non-www, http vs https)
The origin is scheme, host and port, compared exactly. These are all different origins:
https://example.comhttps://www.example.comhttp://example.comhttps://example.com:8443
A typical production story: you whitelist https://example.com, but some users arrive on www, and only they get the error. Or a staging build still has http:// in an environment variable. Also watch for a trailing slash: https://example.com/ is not a valid origin value and will never match.
Two more traps here:
Redirects. If the preflight gets a 301 (http to https, non-www to www, or /orders to /orders/), the browser refuses to follow it. Call the final URL directly from your frontend. If you are still setting up HTTPS, my guide to free HTTPS on a VPS with Let's Encrypt shows the redirect setup. Just make sure the API URL in your frontend already points to the https address.
Vary: Origin. If you reflect the request origin back instead of using a fixed value, the response should include Vary: Origin. Otherwise a CDN or proxy cache can serve one origin's headers to another. The cors package adds this for you when the origin is dynamic.
Check 3: credentials and the wildcard trap
If your frontend sends cookies (credentials: 'include' in fetch, or withCredentials: true in Axios), the rules get stricter:
Access-Control-Allow-Origincannot be*. It must be the exact origin.- The response must include
Access-Control-Allow-Credentials: true. Access-Control-Allow-Headers: *does not work as a wildcard here either. List the headers explicitly.
Locally you might have used origin: '*' with no cookies, so everything passed. In production you add session cookies and it breaks. Also check the cookie itself: for a cross-site request it needs SameSite=None; Secure, or the browser will not send it even when CORS is correct.
Check 4: Nginx stripping or duplicating headers
If curl against your Node process directly (curl -i -X OPTIONS http://127.0.0.1:3000/... on the server) looks fine but the public URL does not, Nginx is the suspect. There are three classic problems.
Headers added twice. Your app sets Access-Control-Allow-Origin and Nginx also adds it. The browser sees two values and rejects them. Pick one place for CORS. I keep it in the app and make sure Nginx adds none.
add_header without always. Nginx only adds headers to successful responses by default. When your API returns a 401 or 500, the CORS header disappears, and the browser shows a CORS error that hides the real one. If you do set headers in Nginx, use add_header ... always;.
add_header inheritance. If a location block has any add_header, it stops inheriting add_header lines from the server block. Adding one security header inside a location can silently drop all your CORS headers.
To see everything Nginx is doing with these headers:
sudo nginx -T | grep -i -n "access-control\|add_header\|proxy_hide_header"
A clean reverse proxy for an app that handles CORS itself looks like this:
location / {
proxy_pass http://127.0.0.1:3000;
proxy_http_version 1.1;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
After any change, run sudo nginx -t and then sudo systemctl reload nginx. If you are building this setup from scratch, my walkthrough on deploying a Node.js app with PM2 and Nginx covers the full config. And if large uploads fail and show up as CORS errors, see the 413 fix behind Nginx: an error page without CORS headers looks exactly like a CORS problem.
The order I check things in
- Network tab: find the OPTIONS row, note the status and console message.
- curl the preflight against the public URL.
- Status 404 or 401: fix OPTIONS handling and middleware order.
- Status 301 or 302: call the final URL directly.
- Header missing or wrong: compare the origin character by character.
- Cookies involved: no wildcard, add
Allow-Credentials, checkSameSite. - Works on
127.0.0.1but not publicly: inspect Nginx.
FAQ
Why does my OPTIONS request return 404 or 401?
Your server has no handler for OPTIONS on that path, or your auth middleware runs before CORS. Register CORS first so preflights get a response before auth runs.
Can I just disable CORS in production?
CORS is enforced by the browser, not your server, so you cannot turn it off. You can avoid it by serving the frontend and API from the same origin, for example by proxying /api through Nginx on the main domain.
Why do I get a CORS error when the real problem is a 500?
Error responses often skip your CORS headers, especially if Nginx adds them without always. Fix the headers on error responses, then the real error will show.
Does a CORS preflight error mean my API is insecure?
No. It means the browser did not get permission it understood. CORS protects users in the browser. It is not a replacement for authentication on your API.
Stuck on a CORS bug that will not go away? Talk to the editor at ahmed-adil.com.



