[已解决]WebSocket的Cookie问题
发布于 11 年前 作者 bigmusic 13010 次浏览 最后一次编辑是 8 年前

我用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;
    }
回到顶部