扫码关注官方订阅号
既然前后端分离了,就应该接受无状态这种模式。尝试从其他途径解决状态保存问题。
例如:可以在登录接口中返回用户信息,由前端进行处理。
使用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/* 为接口
http://example.com:8080/
http://example.com:8080/api/*
微信扫码关注PHP中文网服务号
QQ扫码加入技术交流群
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
PHP学习
技术支持
返回顶部
既然前后端分离了,就应该接受无状态这种模式。尝试从其他途径解决状态保存问题。
例如:可以在登录接口中返回用户信息,由前端进行处理。
使用nginx做反向代理,将不同端口的服务映射到统一端口,就可以实现cookie共享了
nginx配置文件范例:
之后访问
http://example.com:8080/为静态资源,http://example.com:8080/api/*为接口