express-session 无法在生产中设置 connect.sid cookie

问题描述 投票:0回答:2

应用程序正在使用 postgres 池来存储会话。一直工作到应用程序成为整体并且请求通过反向代理路由。 但现在它自己的服务部署到 Elastic Beanstalk 上的生产,并且没有按照预期的方式运行。

app.use(cors({credentials: true, origin: 'http://localhost:3000'}));

var sess = {
    store: new (connectPgSimple(session))({ pool: db.pool }),
    cookie: { 
        maxAge: 30 * 24 * 60 * 60 * 1000,
        httpOnly: false
    },
    secret: process.env.COOKIE_SECRET,
    resave: false,
    saveUninitialized: true,
}

// Trust first proxy for production
if (process.env.NODE_ENV === 'production') {
    app.set('trust proxy', 1) 
    sess.cookie.secure = true
}

app.use(session(sess));

还为来自客户端的所有请求添加了

withCredentials: *true*

似乎只有当传入请求被代理到 Express 服务器时才会设置

connect.sid
会话 cookie。 我尝试了使用 Express-Session 的配置变体来信任代理,但它仍然没有创建所需的 cookie。

Sample response for a POST request 当我检查 Application > Storage > Cookies 时,您可以看到发送 Set-Cookie 标头的响应,我没有看到

connect.sid
cookie

node.js nginx cookies amazon-elastic-beanstalk express-session
2个回答
0
投票

对于遇到

express-session unable to set connect.sid cookie
问题的人。另外,您可以检查您的
'saveUninitialized': true,
是否为
true
,这意味着如果设置 'saveUninitialized': false,则不会设置 cookie,因为没有任何因素导致 cookie 被设置。


0
投票

就我而言,问题出在 nginx https 代理并在配置中添加 X-Forwarded-Proto 标头:

location / {
  ....
  proxy_set_header X-Forwarded-Proto $scheme;
}
© www.soinside.com 2019 - 2024. All rights reserved.