node.js - 同一域名下不同端口cookie共享问题

【字号: 日期:2022-10-04浏览:14作者:雯心

问题描述

问题解答

回答1:

既然前后端分离了,就应该接受无状态这种模式。尝试从其他途径解决状态保存问题。

例如:可以在登录接口中返回用户信息,由前端进行处理。

回答2:

使用nginx做反向代理,将不同端口的服务映射到统一端口,就可以实现cookie共享了

nginx配置文件范例:

server { listen 8080; server_name example.com; # 将/api路径映射到3000端口 location ~ ^/(api)/ {proxy_pass http://127.0.0.1:3000;proxy_http_version 1.1;proxy_set_header Upgrade $http_upgrade;proxy_set_header Connection 'upgrade';proxy_set_header Host $host; }# 静态资源直接由nginx负责 location / {root /some/path;index index.html index.htm; }}

之后访问 http://example.com:8080/ 为静态资源,http://example.com:8080/api/* 为接口

相关文章: