redis会话无法在服务器上运行

时间:2012-10-04 08:12:37

标签: node.js express redis

我在我的node.js express app中使用redis进行会话。它在我的开发盒上工作正常,但在生产时,似乎redis会话没有被保存。

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

Redis正在运行w /相同的配置。但是,当我运行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是localhost 和cfg.redis.db是1

这是我运行redis-cli monitor

时遇到的错误
Error: Protocol error, got "s" as reply type byte

1 个答案:

答案 0 :(得分:2)

一些建议。你确定Redis在生产中使用相同的端口和密码吗?如果您使用像Heroku这样的服务使用SSL,则需要设置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;
相关问题