Secure cookies over reverse proxy with https
See original GitHub issueThe 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:
- Created 8 years ago
- Comments:7 (2 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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.@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)]