I have a problem with my Node application behind nginx.
On the begining, I will explain infrastructure:
NGINX => (prox_pass) => Docker (expose port to host) => Node APP
Application runs good, but I have problem with connect-flash (I use it with PassportJS). I've got error:
req.flash() requires sessions
On my local computer I don't have any problems (I ran as production env). So probably the problem could be on nginx side.
I discovered that cookie session is not store on browser, so that req.flash() requires session. I don't use session.destroy and I put use session config before passport session and flash.
Here is part of server.js code (I've tried different configs: secrets, resave, saveUninitialized ; different order ; also I commented out if NODE_ENV_PRODUCTION - doesn't work ) :
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());
And NGINX config:
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/;
}
}
In logs I can see this:
{"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 at ctrl.login (/var/www/application/src/modules/user/server/controllers/login.js:17:16)\n at Layer.handle [as handle_request] (/var/www/application/node_modules/express/lib/router/layer.js:95:5)\n at next (/var/www/application/node_modules/express/lib/router/route.js:131:13)\n at Route.dispatch (/var/www/application/node_modules/express/lib/router/route.js:112:3)\n at Layer.handle [as handle_request] (/var/www/application/node_modules/express/lib/router/layer.js:95:5)\n at /var/www/application/node_modules/express/lib/router/index.js:277:22\n at 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)\n at Function.handle (/var/www/application/node_modules/express/lib/router/index.js:176:3)\n at router (/var/www/application/node_modules/express/lib/router/index.js:46:12)\n at Layer.handle [as handle_request] (/var/www/application/node_modules/express/lib/router/layer.js:95:5)\n at trim_prefix (/var/www/application/node_modules/express/lib/router/index.js:312:13)\n at /var/www/application/node_modules/express/lib/router/index.js:280:7\n at 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)","level":"error","timestamp":"2017-08-01T21:17:49.367Z"}
Node version is 8.2.1
Nginx version is 1.6.2
RESOLVED
I've found a solution - problem was with connect. I use redis as storage and I connected to localhost instead of linked redis