Express.js with reverse proxy

I set the Apache reverse proxy to forward api.mydomain.comto localhost:2000, which works fine.

However, the real problem that arises with me is related to sessions - it seems that req.session is not saved when querying through api.mydomain.com. Sessions will work with a wonderful visit localhost:2000.

I assume this has something to do with the proxy server ...

server.js

var express = require('express'),
    app = express();

app.enable('trust proxy');

app.configure(function() {
    app.use(express.bodyParser());
    app.use(express.methodOverride());
    app.use(express.cookieParser());
    app.use(express.session({ secret: 'supersecret'});
    app.use(app.router);
});

app.get('/session', function(req, res, next){ 
    console.log(req.session.username)
    // this will be undefined when called from  api.mydomain.com...
});


app.post('/session', function(req, res, next){ 
    req.session.username = 'my username';
});

apache config

<VirtualHost *:80>
    ServerName api.mydomain.com   
    ProxyPass / http://localhost:2000/
    ProxyPassReverse / http://localhost:2000/

    ProxyPreserveHost On
    ProxyPassReverseCookieDomain api.domain.com localhost:2000
    ProxyPassReverseCookiePath / /
</VirtualHost>

EDIT

It is noteworthy that for further testing - adding the code below app.use(app.router)to app.configure()....

app.use(function (req, res) {
    res.send('<h2>Hello, your session id is ' + req.sessionID + '</h2>');
});

will lead to the following (each line represents a new GET / session request for the application - node also did not reboot).

local: 2000

Hello, your session id is sHkkESxJgzOyDhDwyTjwpNzq
Hello, your session id is sHkkESxJgzOyDhDwyTjwpNzq
Hello, your session id is sHkkESxJgzOyDhDwyTjwpNzq

api.mydomain.com

Hello, your session id is uuo4U5ierZAG8LSH1BdwTlVf
Hello, your session id is 8BxL97Bo35SDt4uliuPgnbia
Hello, your session id is 0xkqZZpzQNvTsQpbJtUlXgkR

Setup Information

NodeJS v0.8.8

Apache v2.4.2

ExpressJS v3.0.0rc4

UPDATE 2

, mydomain.com/api, , , . , , Apache Express!

+5
6

Apache ProxyPass Sessions apache ...

<VirtualHost *:80>
    ServerName api.mydomain.com   
    ProxyPass / http://localhost:2000/
    ProxyPassReverse / http://localhost:2000/
    ProxyPassReverseCookiePath / /
</VirtualHost>
+3

server.js :

app.enable('trust proxy');

ExpressJS API Docs: http://expressjs.com/api.html#app-settings...

trust proxy - ,

+4

, cookie , - , , .

Re: - , "ProxyPassReverseCookieDomain//" ?

ProxyPreserveHost, , , cookie.

+1

node 0.6.17, express 3.0.0rc4. , .

ServerName api
ServerAlias api.internal.local

Set-cookie connect.sid, .

+1

, . Apache SSL:

RequestHeader set X-Forwarded-Proto "https"

, https://github.com/expressjs/session/issues/251

, , , , :

  • Apache:

RequestHeader set X-Forwarded-Proto "https" ProxyPassReverseCookiePath / /

  1. Node .

app.enable('trust proxy');

0

This happened to us, and I don’t think the reverse cookies did anything to help us with this.

I tried: app.use ("trusted proxy");

to no avail.

however using

app.use("trust proxy");
app.set("trust proxy", 1);

Works.

We hope that this will help anyone who has problems with proxy files.

-1
source

All Articles