Express Session Cookie Not Set in Production: trust proxy Fix

Login works locally but express-session sets no cookie on your live Node.js site? Learn why secure cookies fail behind a proxy, and fix it with trust proxy, forwarded headers and the right cookie options.

If express-session sets no cookie in production, Express almost certainly thinks the request is plain HTTP. Your app sits behind a proxy (Nginx, a load balancer, Cloudflare, a platform router) that ends HTTPS and forwards HTTP. With cookie: { secure: true }, express-session refuses to send Set-Cookie on what it sees as an insecure connection. Add app.set('trust proxy', 1) before the session middleware and make sure the proxy sends X-Forwarded-Proto.

The symptom is confusing. Sign-in returns 200, no error is logged, and the user is logged out on the next request. In the browser there is no session cookie at all. On your laptop everything worked, because there is no proxy and no HTTPS. This guide walks through the checks in the order that finds the cause fastest.

Do not guess. Look at the login response headers:

curl -i -X POST https://your-app.com/login \
  -H "Content-Type: application/json" \
  -d '{"email":"test@example.com","password":"secret"}'

Look for a Set-Cookie: connect.sid=... line. If it is missing, the cookie was never issued. If it is present but the browser drops it, the problem is SameSite, Domain or a cross-site call, which Step 5 covers.

In the browser, open DevTools, go to Network, click the login request and read the Response Headers and the Cookies tab. Chrome shows a small warning icon next to cookies it rejected and says why.

Step 2: Understand Why secure: true Breaks Behind a Proxy

A typical production setup looks like this:

Browser --HTTPS--> Proxy / load balancer --HTTP--> Your Express app

The browser uses HTTPS. Your app only ever receives plain HTTP from the proxy. By default Express ignores the X-Forwarded-* headers, so req.secure is false. The express-session docs explain that with secure: true the module will not set the cookie on an insecure connection, and that if you have a proxy in front you need to enable trust proxy. The same thing is reported in an express-session issue and in forum threads where adding trust proxy fixed it.

This is also why it works locally. Locally secure is usually off, or you run on http://localhost where browsers allow some exceptions.

Step 3: Set trust proxy Before the Session Middleware

Order matters. Set it first:

const express = require('express');
const session = require('express-session');

const app = express();

// Trust the first proxy hop in front of the app
app.set('trust proxy', 1);

app.use(session({
  secret: process.env.SESSION_SECRET,
  resave: false,
  saveUninitialized: false,
  cookie: {
    secure: process.env.NODE_ENV === 'production',
    httpOnly: true,
    sameSite: 'lax',
    maxAge: 1000 * 60 * 60 * 24 * 7,
  },
}));

The number is how many proxy hops to trust. Use 1 for one proxy (a platform router, or Nginx alone). Use 2 if you have, for example, Cloudflare in front of a load balancer. Avoid true unless you control the network edge, because with true a client can fake X-Forwarded-For and spoof its IP address.

You can also set cookie.secure: 'auto', which makes express-session decide per request. The docs warn that a cookie set over HTTPS will not be visible over HTTP, so this only suits sites that are fully HTTPS.

Step 4: Make Sure the Proxy Forwards the Right Headers

trust proxy only helps if the proxy tells Express the original protocol. Add a temporary debug route:

app.get('/debug/proto', (req, res) => {
  res.json({
    secure: req.secure,
    protocol: req.protocol,
    forwardedProto: req.get('x-forwarded-proto'),
    ip: req.ip,
  });
});

Call it over HTTPS. You want secure: true and forwardedProto: "https". Remove the route afterwards.

If forwardedProto is empty, fix the proxy. For Nginx:

location / {
  proxy_pass http://127.0.0.1:3000;
  proxy_set_header Host $host;
  proxy_set_header X-Real-IP $remote_addr;
  proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  proxy_set_header X-Forwarded-Proto $scheme;
}

Reload with sudo nginx -t && sudo systemctl reload nginx. Most managed platforms already send this header. If forwardedProto says http even on an HTTPS URL, a layer in front is talking HTTP to the next layer. A common case is a CDN set to a mode that connects to your origin over HTTP. Switch it to full HTTPS to the origin.

If the proxy itself fails, see Nginx 502 Bad Gateway With Node.js.

Step 5: Check SameSite, Domain and Cross-Site Calls

If Set-Cookie is present but the session still does not stick, check these:

  • Frontend and API on different sites. If your React app is on app.example.com and the API is on a different site, the cookie is third-party. You need sameSite: 'none' with secure: true, and the browser request needs credentials: 'include'. One Stack Overflow answer describes this for a split deployment.
  • CORS with credentials. The API must send Access-Control-Allow-Credentials: true and a specific origin, not *.
  • Cookie domain. If you set domain wrongly, the browser ignores the cookie. Leave it unset unless you need to share it across subdomains.
  • Same-origin is simpler. Serving the API under the same domain (for example /api) avoids most of these problems.

Step 6: Check the Session Store and Multiple Instances

The default memory store is for development only. It loses sessions on restart and does not share them between processes. If you run two instances or restart on each deploy, users get logged out even when cookies work. Use a shared store such as Redis. If Redis is memory-limited, see Redis OOM Command Not Allowed.

Step 7: Fix the Same Root Cause for Rate Limits and Logs

Behind a proxy without trust proxy, req.ip is the proxy's address. Every user looks like one client. IP-based rate limiting then blocks everyone together, and your logs show the wrong address. Setting trust proxy correctly fixes these too. After the change, confirm req.ip in your debug route shows a real client address.

How Relia Helps

A session bug like this fails quietly: the login request succeeds, so no error is thrown. Relia watches users and APIs of a live product, so a spike in users who sign in and are then sent back to the login page, next to a recent deploy or config change, shows up as one incident explained in plain words, with who was affected. For Node.js apps, setup is npx relia-sdk@latest init. There is a Free plan and a 14-day Pro trial, and you can look at the live demo.

FAQ

Why does express-session work locally but not in production?

Locally there is no HTTPS and no proxy, so secure cookies are off or not needed. In production a proxy ends HTTPS, so Express sees HTTP and withholds a secure cookie unless trust proxy is set.

What number should I use for trust proxy?

Use the number of proxies between the internet and your app. One proxy means 1. Avoid true unless you fully control the edge.

Should I set cookie.secure to false to make it work?

No. That sends session cookies over plain HTTP if a client ever connects that way. Fix trust proxy and the forwarded headers instead.

Why am I logged out after each deploy even though the cookie is set?

You are probably using the default memory store. Move sessions to Redis or a database so they survive restarts and work across instances.

Is a JWT a better choice than sessions?

Both work. Tokens bring their own failure modes, covered in JWT Expired Errors in Production.

All guides