NodeJS,docker,nginx - req.flash()需要会话

时间:2017-08-02 09:28:18

标签: node.js express nginx connect-flash

我的nginx背后的Node应用程序有问题。

在开始时,我将解释基础设施:

  

NGINX => (prox_pass)=> Docker(将端口暴露给主机)=>节点APP

应用程序运行良好,但我遇到连接闪存问题(我使用PassportJS)。我有错误:

req.flash()需要会话

在我的本地计算机上,我没有任何问题(我作为生产环境运行)。 所以问题可能出在nginx方面。

我发现cookie会话不存储在浏览器上,因此req.flash()需要会话。 我没有使用session.destroy,我在护照会话和flash之前使用了session config。

这是server.js代码的一部分(我尝试了不同的配置:秘密,重新保存,保存未初始化;不同的顺序;我也注释掉了NODE_ENV_PRODUCTION - 是否有效):

app.use(bodyParser.urlencoded({extended: true}));
app.use(bodyParser.json());
app.use(express.static(__dirname + '/public'));

app.use(cookieParser());


const store = new RedisStore({
  client: redisClient
});

const sessionConfig = {
  secret: 'keyboard cat',
  resave: true,
  saveUninitialized: true,
  cookie: {
    maxAge: 1000 * 60 * 60 * 24 * 7 // 1 week
  },
  store: store,
};


if (NODE_ENV_PRODUCTION) {
  app.set('trust proxy', 1);
  app.use(compression());
  sessionConfig.cookie.httpOnly = true;
  sessionConfig.cookie.secret = 'Uc7gv6L397H6';
}
app.use(session(sessionConfig));

require('./modules/user/server/passport');
app.use(passport.initialize());
app.use(passport.session());


app.use(flash());

和NGINX配置:

server {
    server_name DOMAIN_NAME;
    client_max_body_size 0;

    location / {
        auth_basic "Restricted";
        auth_basic_user_file /.htpasswd;
        proxy_http_version 1.1;
        proxy_redirect off;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_set_header X-NginX-Proxy true;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        proxy_cache_bypass $http_upgrade;
        proxy_buffering off;
        proxy_pass http://127.0.0.1:8989/;
    }
}

在日志中,我可以看到:

  

{" message":" req.flash()需要会话"," stack":"错误:req.flash()需要会话\ n在IncomingMessage._flash [as flash](/var/www/application/node_modules/connect-flash/lib/flash.js:60:41)\ n在ctrl.login(/ var / www / application / src / modules / user / server / controllers / login.js:17:16)\ n在Layer.handle [as handle_request](/var/www/application/node_modules/express/lib/router/layer.js:95:5) \ n下一个(/var/www/application/node_modules/express/lib/router/route.js:131:13)\ n在Route.dispatch(/ var / www / application / node_modules / express / lib / router / route.js:112:3)\ n在Layer.handle [as handle_request](/var/www/application/node_modules/express/lib/router/layer.js:95:5),n at / var / www /应用程序/ node_modules / express / lib / router / index.js:277:22 \ n在Function.process_params(/var/www/application/node_modules/express/lib/router/index.js:330:12),n at下一个(/var/www/application/node_modules/express/lib/router/index.js:271:10)在动态路由器(/var/www/application/node_modules/express/lib/router/index.js) :46:12)\ n在trim.handle [as handle_request](/var/www/application/node_modules/express/lib/router/layer.js:95:5)n在trim_prefix(/ var / www / application /node_modules/express/lib/router/index.js:312:13)n在/var/www/application/node_modules/express/lib/router/index.js:280:7,在Function.process_params(/ var / www / application / node_modules / express / lib / router / index.js:330:12)\ n at next(/var/www/application/node_modules/express/lib/router/index.js:271:10) ""电平":"错误""时间戳":" 2017-08-01T21:17:49.367Z"}

节点版本为8.2.1

Nginx版本为1.6.2

分辨

我找到了一个解决方案 - 问题在于连接。我使用redis作为存储,我连接到localhost而不是链接redis

0 个答案:

没有答案