D
D
d4rk_light2015-07-14 17:54:16
Nginx
d4rk_light, 2015-07-14 17:54:16

How to keep authentication when moving to a subdomain?

I have a server on Nginx with 2 virtual servers server 1: localhost and server 2: sub.localhost.
For authorization I use passport.js, express.js and express.js cookie-session. I can authorize separately on both domains, but I need the user not to have to do this for each subdomain in a new way.
server.js

app         = express(),
    cookieSession     = require('cookie-session'),
app.use(cookieSession({
    secret: config.session_secret,
    resave: true,
    saveUninitialized: true,
    store: new Redis({
        port: config.redis_port
    }),
    cookie: { max_age: 43200000, domain:"localhost"}
}));

nginx.conf
worker_processes  1;
events {
    worker_connections  1024;
}


http {
    upstream app {
    	server 127.0.0.1:3000;
    }

    server {
        listen       80;
        server_name  localhost;

        client_max_body_size 32m;

        location / {
        	proxy_set_header X-Real-IP $remote_addr;
        	proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        	proxy_set_header Host $http_host;
        	proxy_set_header X-NginX-Proxy true;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection "upgrade";
    	    proxy_pass http://app/;
    	    proxy_redirect off;
        }
    }

    server {
        listen       80;
        server_name  sub.localhost;
        client_max_body_size 32m;
        

        location / {
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header Host $http_host;
            proxy_set_header X-NginX-Proxy true;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection "upgrade";
            proxy_pass http://app/;
            proxy_redirect off;
        }
    }    
}

Where is the mistake?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
P
pomeo, 2015-07-14
@pomeo

That's where you have domain:"localhost" try a dot in front of localhost i.e. domain:".localhost"

D
d4rk_light, 2015-07-14
@d4rk_light

oh yes i forgot to mention i already tried domain:".localhost" , domain:"*.localhost" , domain:"sub.localhost" , domain:"*" and many more i also added $httpProvider.defaults to angular config. withCredentials = true; and tried adding

app.use(function(req, res, next){
   // Website you wish to allow to connect
   
    res.setHeader('Access-Control-Allow-Origin', req.headers.host)
    // Request methods you wish to allow
    res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE');

    // Request headers you wish to allow
    res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-type');

    // Set to true if you need the website to include cookies in the requests sent
    // to the API (e.g. in case you use sessions)
    res.setHeader('Access-Control-Allow-Credentials', true);    
    next();
});

in server.js

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question