【发布时间】:2013-07-02 13:46:31
【问题描述】:
您好,我正在尝试将 nginx 配置为 websockets 的反向代理。我将我的服务器配置如下:
server {
listen 80;
server_name www.mydomain.com;
access_log off;
#error_log off;
location / {
proxy_pass http://127.0.0.1:8765;
proxy_redirect off;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "Upgrade";
proxy_set_header Host $http_host;
proxy_buffering off;
}
}
但我从客户端收到如下错误
与“ws://www.application.com/ws”的 WebSocket 连接失败:WebSocket 握手期间出错:“Connection”标头值不是“Upgrade”
我可能做错了一些配置,但我看不到它。
客户端的请求标头如下
GET ws://www.talkybee.com/ws HTTP/1.1
Pragma: no-cache
Origin: http://www.talkybee.com
Host: www.talkybee.com
Sec-WebSocket-Key: Ol+O1IdaLEsHxxWRBt2oqg==
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.116 Safari/537.36
Upgrade: websocket
Sec-WebSocket-Extensions: x-webkit-deflate-frame
Cache-Control: no-cache
Connection: Upgrade
Sec-WebSocket-Version: 13
当我进行正常的直接连接时,我的连接正常工作。这是有效的请求标头。
Cache-Control:no-cache
Connection:Upgrade
Host:www.talkybee.com:8765
Origin:http://www.talkybee.com:8765
Pragma:no-cache
Sec-WebSocket-Extensions:x-webkit-deflate-frame
Sec-WebSocket-Key:Y026b/84aUkMxVb0MaKE2A==
Sec-WebSocket-Version:13
Upgrade:websocket
User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.116 Safari/537.36
【问题讨论】:
-
如果你尝试
proxy_pass http://127.0.0.1:8765/;会发生什么 -
@rednaw 我遇到了同样的异常。我用当前的配置更改了问题中的配置。
-
您的客户端是否也将
Connection标头设置为Upgrade? -
@rednaw。是的,它似乎有 connection:Upgrade 标头。我将尝试获取工作直接连接标头和代理标头的差异。
-
@rednaw 看起来我缺少 Sec-WebSocket-Key: 标头。我想我也应该找到一种方法来集中这些标题。