question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Secure cookies over reverse proxy with https

See original GitHub issue

The following configuration properly produces secure cookies, however

var config = require(configPath),
    express = require('express'),
    cookieParser = require('cookie-parser'),
    session = require('express-session'),
    mongoStore = require('connect-mongo')(session),
    csrf = require('csurf'),
    ....
    sessionConfig = {
      // according to https://github.com/expressjs/session#resave
      // see "How do I know if this is necessary for my store?"
      resave: false,
      saveUninitialized: true,
      secret: config.cryptoKey,
      store: new mongoStore({ url: config.mongodb.uri }),
      cookie: {}
    };

app.use(cookieParser(config.cryptoKey));
if (node_env === "prod") {
    app.set('trust proxy', 1);
    sessionConfig.cookie.secure = true;
    logger.info("using secure cookies");
}
app.use(session(sessionConfig));

I’m getting a new sessionID for every subsequent request fired during a pageload. My application is sitting behind apache which acts as a reverse proxy and has https, the application itself is running on http.

This is not the case if I skip setting sessionConfig.cookie.secure = true; or setting sessionConfig.cookie.secure = false; and the same sessionID is assigned to every request during single or multiple pageloads while I’m firing requests from the same browser instance.

Do you have any idea why I’m getting new sessionID for every request made in a single pageload, is this the intended behavior?

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:7 (2 by maintainers)

github_iconTop GitHub Comments

6reactions
dougwilsoncommented, Feb 25, 2016

Hi @akoskm, your issue is you have not finished standard reverse proxy configuration. Googling around for Apache setup information, it looks like you need to add RequestHeader set X-Forwarded-Proto "https" to your SSL virtual host declaration.

0reactions
nagmancommented, May 14, 2019

@dougwilson Thanks man!!! In my case, I had to set this header on both Apache and Nginx because my project looks like this: [Apache]===>[Docker (Nginx)===>(Express)]

Read more comments on GitHub >

github_iconTop Results From Across the Web

Secure cookies over reverse proxy with https #1887 - GitHub
Actually, it's not possible to set secure cookies over reverse proxy with https. Is your feature request related to a problem? Please describe....
Read more >
Using secure sessions behind an HTTP proxy - GoSquared Blog
GoSquared is served entirely via HTTPS, so it was a logical and easy decision to modify our user sessions to use secure cookies....
Read more >
In nginx reverse proxy, how to set the secure flag for cookies?
I'm using nginx as a reverse proxy to serve a https-only site. So I want the cookies for this site flagged as secure....
Read more >
How to Implement HTTPOnly and Secure Cookie in Nginx?
By using “add_header” directive. An easy way to set cookie flag as HTTPOnly and Secure in Set-Cookie HTTP response header. · By using...
Read more >
Secure cookies with reverse proxy - No Fluff Web
To mark cookies as secure server must work on HTTPS, in case reverse proxy accepts requests on HTTPS and then forwards to back-end...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found