鍍金池/ 問答/Linux  HTML/ nodejs 通過nginx反向代理后,服務(wù)器無法返回cookie給前端,該如何

nodejs 通過nginx反向代理后,服務(wù)器無法返回cookie給前端,該如何配置呢?

nginx配置

upstream mall {
 server 127.0.0.1:4000;
}
server {
 listen 80;
 server_name www.a.com;
 rewrite ^(.*) https://$host$1 permanent;
}
server {
 listen 443;
 server_name www.a.com; #填寫綁定證書的域名
 ssl on;
 ssl_certificate cert/1_www.a.com_bundle.crt;
 ssl_certificate_key cert/2_www.a.com.key;
 ssl_session_timeout 5m;
 ssl_protocols TLSv1 TLSv1.1 TLSv1.2; #按照這個協(xié)議配置
 ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;
 ssl_prefer_server_ciphers on;
 location / {
  add_header 'Access-Control-Allow-Origin' '*';
  add_header 'Access-Control-Allow-Credentials' 'true';
  add_header 'Access-Control-Allow-Methods' 'OPTION, POST, GET';
  add_header 'Access-Control-Allow-Headers' 'X-Requested-With, Content-Type';

  proxy_set_header X-Real-IP $remote_addr;
  proxy_set_header X-Forward-For $proxy_add_x_forwarded_for;
  proxy_set_header Host $http_host;
  proxy_set_header X-Nginx-Proxy true;

  proxy_pass http://mall;
  proxy_redirect off;
  }

express 接口如下

router.post('/login', function (req,res,next) {
  var param = {
    userName: req.body.userName,
    userPwd: req.body.userPwd
  }
  User.findOne(param,function (err, doc) {
    if(err) {
      res.json({
        status: '1',
        msg: err.message
      });
    }else {
      if(doc) {
        res.cookie("userId",doc.userId,{
          path: '/',
          maxAge: 1000*60*60
        });
        res.cookie("userName",doc.userName,{
          path: '/',
          maxAge: 1000*60*60
        });
        res.json({
          status: '0',
          msg:'',
          result:{
            userName: doc.userName
          }
        });
      } else {
        res.json({
          status: '1',
          msg:'賬號密碼錯誤',
          result:''
        });
      }
    }
  });
});
回答
編輯回答
哎呦喂

正常情況下是可以返回的吧

2018年9月4日 21:59