如何在 apache2.4 网络服务器后面处理 expressjs 的会话 ID

如何在 apache2.4 网络服务器后面处理 expressjs 的会话 ID

我有一个 apache 2.4.29,已激活 TLS 来处理静态文件。然后我配置了一个代理,将动态内容传递给 NodeJS 10.5.0:

  • ExpressJS 4.16.3
  • 快速会话 1.15.6
  • 会话文件存储 1.2.0

apache中配置代理到Node的site.conf是:

            ProxyPreserveHost On
            RemoteIPHeader X-Forwarded-For
            RemoteIPInternalProxy 127.0.0.0/8
            RequestHeader add X-SSL off
            ProxyPass "/s"  "http://127.0.0.1:3001"
            ProxyPassReverse "/s"  "http://127.0.0.1:3001"
            RewriteRule ^/s/(.*) http://127.0.0.1:3001/$1 [P,L]
            ProxyPass "/n"  "http://127.0.0.1:3001/n"
            ProxyPassReverse "/n"  "http://127.0.0.1:3001/n"
            RewriteRule ^/n/(.*) http://127.0.0.1:3001/n/$1 [P,L]

Node/Express 没有 TLS 配置。两者都在同一台机器上。在 server.js 中,我有以下代码来处理会话:

var express = require('express'),
    sessao = require('express-session'),
    armazenamento_ficheiros = require('session-file-store')(sessao),
    app = express();
app.set('trust proxy', 1); // trust first proxy
app.use(sessao({
    store: new armazenamento_ficheiros(),
    secret: 'teste',
    name: 'xxx',
    resave: true,
    cookie: {secure: false }, //i also tried with true, auto and removing the property
    saveUninitialized: true
}));

问题是,对于每个客户端对动态文件的请求,它都会显示一个会话 ID JSON 文件,而不是只使用一个。此外,浏览器中显示的 cookie ID 会话的 ID 并未出现在 Express 的会话 ID 文件中。我删除并重复查看,发现 cookie 已再次创建,但现在没有。这么多测试让我感到困惑。

有人能指导我如何正确解决这个问题吗?谢谢。

更新 我把脚本改成:

app.use(sessao({ store: new armazenamento_ficheiros(), secret: 'teste', name: 'xxx', resave: true, proxy: true, cookie: {secure: true }, saveUninitialized: true }));

现在,如果我使用 URL 直接访问 NodeJS,它会按预期工作;它会创建一个带有会话 ID 的 cookie。但是如果我通过 Apache 访问,我可以在收到的 http 标头中看到 set-cookie 命令,但浏览器不会创建 cookie。我怀疑 Apache 配置中存在一些错误。

所以我添加了:RequestHeader 设置 X-Forwarded-Proto“https”

但问题仍然存在。

答案1

除了 ExpressJS 和 Apache 配置的小问题之外,最终的问题是 JS 的获取请求中缺少凭据选项。更多详细信息请参见此处:在此处输入链接描述

相关内容