search

Home  >  Q&A  >  body text

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

巴扎黑巴扎黑2867 days ago844

reply all(2)I'll reply

  • PHP中文网

    PHP中文网2017-04-17 15:07:54

    Now that the front and back ends are separated, the stateless model should be accepted. Try other ways to solve the state saving problem.

    For example: User information can be returned in the login interface and processed by the front end.

    reply
    0
  • 天蓬老师

    天蓬老师2017-04-17 15:07:54

    Use nginx as a reverse proxy and map services on different ports to a unified port to achieve cookie sharing

    nginx configuration file example:

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

    , access http://example.com:8080/ as a static resource and http://example.com:8080/api/* as an interface

    reply
    0
  • Cancelreply