我在我的 node.js Express 应用程序中使用 redis 作为 session 。它在我的开发箱上运行良好,但在生产环境中,似乎没有保存 redis session 。
除了无法登录外,我没有看到任何类型的错误。
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
最佳答案
一些建议。您确定 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;
关于node.js - redis session 在服务器上不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12722978/