Redis session does not work on server

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

I do not see any error, except that I can not enter.

Redis works with the same configuration. But when I run redis-cli and type ' select 1 ' (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 - localhost and cfg.redis.db is 1

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

 Error: Protocol error, got "s" as reply type byte 
+4
source share
1 answer

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

  .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 need the following config.js file to pass Redis configuration 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; 
+1
source

Source: https://habr.com/ru/post/1437748/


All Articles