应用程序正在使用 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。
当我检查 Application > Storage > Cookies 时,您可以看到发送 Set-Cookie 标头的响应,我没有看到
connect.sid
cookie
对于遇到
express-session unable to set connect.sid cookie
问题的人。另外,您可以检查您的 'saveUninitialized': true,
是否为 true
,这意味着如果设置 'saveUninitialized': false,则不会设置 cookie,因为没有任何因素导致 cookie 被设置。
就我而言,问题出在 nginx https 代理并在配置中添加 X-Forwarded-Proto 标头:
location / {
....
proxy_set_header X-Forwarded-Proto $scheme;
}