Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

redis session not working on server

I am using redis for session in my node.js express app. It works fine on my dev box, but on production, it seems redis sessions are not being saved.

I'm not seeing any kind of error, other than I cannot login.

Redis is running w/ same configuration. But when I run redis-cli and type 'select 1' (the db) and KEYS '*' I get nothing.

  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 is localhost and cfg.redis.db is 1

This is the error I get when I run redis-cli monitor

Error: Protocol error, got "s" as reply type byte
like image 411
chovy Avatar asked Nov 03 '22 14:11

chovy


1 Answers

A few suggestions. Are you sure Redis uses the same port and password in production? If you're using SSL with a service like Heroku, you need to set proxy: true to have Express treat cookies that arrive after after earlier SSL termination.

   .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 }
    }))

I require the following config.js file to pass on Redis config values:

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;
like image 186
Dan Kohn Avatar answered Nov 09 '22 04:11

Dan Kohn