5

我在我的 node.js express 应用程序中使用 redis 进行会话。它在我的开发盒上运行良好,但在生产中,似乎没有保存 redis 会话。

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

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
4

1 回答 1

2

几点建议。您确定 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;
于 2013-06-24T03:43:08.253 回答