首页 文章

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

提问于
浏览
5

我在我的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 回答

  • 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;
    

相关问题