简体   繁体   中英

Nginx + Node.js + express.js + passport.js: Subdommain stay authenticated

I´ve got a Nginx server with following config
and an node.js Server.

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;
            }
        }    
    }

I´ve tried adding domain:".localhost" or even domain:"*.localhost" also i 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();
});

to the server.js

The problem is, when i authenticate on the localhost im not authenticated on sub.localhost.

From Login session across subdomains :

You can use: domain: ".app.localhost" and it will work. The 'domain' parameter needs 1 or more dots in the domain name for setting cookies.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM