0

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

首先,我将解释基础架构:

NGINX => (prox_pass) => Docker (向主机公开端口) => Node APP

应用程序运行良好,但连接闪存有问题(我将它与 PassportJS 一起使用)。我有错误:

req.flash() 需要会话

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

我发现 cookie 会话没有存储在浏览器上,所以 req.flash() 需要会话。我不使用 session.destroy,我将使用会话配置放在护照会话和闪存之前。

这是 server.js 代码的一部分(我尝试了不同的配置:secrets、resave、saveUninitialized ;不同的顺序;如果 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() requires sessions","stack":"Error: req.flash() requires sessions\n at 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 在层。处理 [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 在 /var/www/application/node_modules/express/lib/router/index.js:277:22\n 在函数.process_params (/var/www/application/node_modules/express/lib/router/index.js:330:12)\n 在下一个 (/var/www/application/node_modules/express/lib/router/index.js:271 :10)\n 在 Function.handle (/var/www/application/node_modules/express/lib/router/index.js:176:3)\n 在路由器 (/var/www/application/node_modules/express/lib /router/index.js:46:12)\n 在 Layer.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\n在 Function.process_params (/var/www/application/node_modules/express/lib/router/index.js:330:12)\n 在下一个 (/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

4

0 回答 0