[已解决]Nodejs WebSocket的Cookie问题
[已解决]Nodejs WebSocket的Cookie问题
我用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;
}
[已解决]Nodejs WebSocket的Cookie问题
问题描述
我在使用Nginx作为静态服务器,并且Node.js监听另一个端口作为WebSocket服务器。当客户端创建WebSocket实例时,如果origin
和host
不一致的话,req
实例的headers中就没有cookie信息。
线索
通过查阅一些资料(例如:线索),我发现WebSocket请求中的Cookie不会被发送到服务器,除非WebSocket主机与JavaScript的origin完全相同(端口号可以不同)。此外,带有HttpOnly
标志的Cookie也不会被发送。
解决办法
要解决这个问题,可以通过修改Nginx配置文件来实现。以下是一个示例配置:
location / {
set $Pupgrade "";
set $Pconnection "";
set $Phost "";
root $myroot;
index index.html index.htm;
# 检查HTTP头中的Upgrade字段
if ($http_upgrade != '') {
# 将请求代理到WebSocket服务器
proxy_pass $myurl;
set $Pupgrade $http_upgrade;
set $Pconnection "upgrade";
set $Phost $host;
}
# 设置代理相关的HTTP头
proxy_http_version 1.1;
proxy_set_header Upgrade $Pupgrade;
proxy_set_header Connection $Pconnection;
proxy_set_header Host $Phost;
}
示例代码解析
set $Pupgrade "";
和set $Pconnection "";
初始化变量,用于存储升级请求和连接类型。if ($http_upgrade != '') { ... }
这个条件判断客户端是否发起了WebSocket升级请求。proxy_pass $myurl;
将请求代理到WebSocket服务器。proxy_http_version 1.1;
设置代理使用的HTTP版本为1.1,因为WebSocket协议需要支持HTTP 1.1。proxy_set_header Upgrade $Pupgrade;
和proxy_set_header Connection $Pconnection;
设置代理请求中的Upgrade
和Connection
头,以确保WebSocket升级请求能够正确传递。proxy_set_header Host $Phost;
设置代理请求中的Host
头,以便服务器能够识别正确的主机名。
通过上述配置,可以确保WebSocket客户端能够正确地将Cookie传递给WebSocket服务器。
[已解决]Nodejs WebSocket的Cookie问题
在使用Nginx作为静态服务器,并且让Node.js监听另一个端口作为WebSocket服务器时,如果客户端的origin
和host
不一致,那么在req
实例的headers
中将不会包含Cookie。这是因为WebSocket连接请求的主机名与页面加载的主机名不一致,浏览器不会自动发送Cookie。
解决办法
可以通过修改Nginx配置来解决这个问题。以下是具体的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;
}
示例代码解释
set $Pupgrade "";
和set $Pconnection "";
分别用于存储从HTTP请求头中获取的Upgrade
和Connection
字段。if ($http_upgrade != '') { ... }
这段代码检查是否包含Upgrade
字段(通常在WebSocket握手请求中出现)。proxy_pass $myurl;
将请求代理到指定的后端URL。proxy_set_header Upgrade $Pupgrade;
和proxy_set_header Connection $Pconnection;
将更新后的Upgrade
和Connection
字段添加到代理请求中。proxy_set_header Host $Phost;
设置代理请求中的Host
头为原始请求中的Host
。
通过上述配置,Nginx可以正确地将客户端的请求转发给Node.js WebSocket服务器,并且保留Cookie信息。
希望这能帮助您解决WebSocket的Cookie问题!