node.js - redis session 在服务器上不工作

标签 node.js express redis

我在我的 node.js Express 应用程序中使用 redis 作为 session 。它在我的开发箱上运行良好,但在生产环境中,似乎没有保存 redis session 。

除了无法登录外,我没有看到任何类型的错误。

Redis 正在使用相同的配置运行。但是,当我运行 redis-cli 并键入“select 1”(数据库)和 KEYS '*' 时,我什么也没得到。

  var RedisStore = require('connect-redis')(express);

  app.use(express.session({
    store: new RedisStore({
      host: cfg.redis.host,
      db: cfg.redis.db
    }),
    secret: 'sauce'
  }));

cfg.redis.host 是本地主机 而 cfg.redis.db 是 1

这是我在运行 redis-cli monitor 时遇到的错误

Error: Protocol error, got "s" as reply type byte

最佳答案

一些建议。您确定 Redis 在生产中使用相同的端口和密码吗?如果您将 SSL 与 Heroku 等服务一起使用,则需要设置 proxy: true 以使 Express 处理在较早 SSL 终止后到达的 cookie。

   .use(express.session({
        store: new RedisStore({
            port: config.redisPort,
            host: config.redisHost,
            db: config.redisDatabase,
            pass: config.redisPassword}),
        secret: 'sauce',
        proxy: true,
        cookie: { secure: true }
    }))

我需要以下 config.js 文件来传递 Redis 配置值:

var url = require('url')
var config = {};
var redisUrl;

if (typeof(process.env.REDISTOGO_URL) != 'undefined') {
    redisUrl = url.parse(process.env.REDISTOGO_URL);
}
else redisUrl = url.parse('redis://:@127.0.0.1:6379/0');

config.redisProtocol = redisUrl.protocol.substr(0, redisUrl.protocol.length - 1); // Remove trailing ':'
config.redisUsername = redisUrl.auth.split(':')[0];
config.redisPassword = redisUrl.auth.split(':')[1];
config.redisHost = redisUrl.hostname;
config.redisPort = redisUrl.port;
config.redisDatabase = redisUrl.path.substring(1);

console.log('Using Redis store ' + config.redisDatabase)

module.exports = config;

关于node.js - redis session 在服务器上不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12722978/

相关文章:

mysql - 在已释放回池的连接上触发 Node mysql 错误事件

javascript - 循环nodejs中的setInterval

node.js - express 抛出错误为 `body-parser deprecated undefined extended`

php - 如何在 48 小时后从 Redis 中删除数据?

node.js - Azure 诊断日志大小限制

javascript - nodejs - 将客户端时间戳存储在数据库中

node.js - 如何将 PDF 附加到从 Express 应用程序发送的电子邮件中

node.js - mongodb增量编号-500内部服务器错误

redis - 为远程连接设置redis缓存

python - 从 Python 管理与 redis 的连接