CNode

[已解决]WebSocket的Cookie问题

Bbigmusic发布于13 年前最后回复13 年前0 回复14086 浏览0 收藏

我用Nginx作静态服务器,Node.js监听另外端口作WebSocket服务器,客户端创建实例时,如果origin和host不一样的话,req实例的headers中没有cookie...

线索

Cookie is sent if Web Socket host is exactly the same as the origin of JavaScript (The port can be different). Otherwise it is not sent, because I don't know way to send right Cookie (which is Cookie of the host of Web Socket, I heard). Also, HttpOnly Cookies are not sent.  

###解决办法: Nginx配置文件搞定:

    location / {
        set $Pupgrade "";
        set $Pconnection "";
        set $Phost "";

        root $myroot;
        index index.html index.htm;
        
        if ($http_upgrade != ''){
            proxy_pass $myurl;
            set $Pupgrade $http_upgrade;
            set $Pconnection "upgrade";
            set $Phost $host;
        }
        proxy_http_version 1.1;
        proxy_set_header Upgrade $Pupgrade;
        proxy_set_header Connection $Pconnection;
        proxy_set_header Host $Phost;
    }
    
查看回复

回复 (0)

暂无回复,成为第一个参与讨论的人。
参与回复
登录后即可参与回复。登录