龙卷风nginx网络套接字握手400错误



问题出在websocket连接上,由nginx从tornado/tornado-sockjs代理到浏览器中的sockjs客户端。没有nginx(在本地主机上),事情可以正常工作。

加载页面时(当 sockjs 连接初始化时)浏览器中出现错误消息:

WebSocket connection to 'ws://my-domain.com/ws/844/blvz89pq/websocket' failed: Error during WebSocket handshake: Unexpected response code: 400

自定义nginx配置的相关部分:

location /ws/ {
  proxy_pass         http://127.0.0.1:8888;
  proxy_http_version 1.1;
  proxy_set_header Upgrade $http_upgrade;
  proxy_set_header Connection $connection_upgrade;
  proxy_redirect     off;
  proxy_set_header   Host             $host;
  proxy_set_header   X-Real-IP        $remote_addr;
  proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;
}

来自nginx.conf的$connection_upgrade的值:

    map $http_upgrade $connection_upgrade {
        default upgrade;
        ''      close;
    }

看起来 tornado-sockjs 返回 400 以防万一Upgrade标题未设置为 'websocket'(代码)。事实上,在第 46 行的 if 之前打印标题会给我们标题Connection=close的值,并且根本没有显示Upgrade

dict(self.request.headers)的价值

{
    "Accept-Language": "en-US,en;q=0.8,ru;q=0.6", 
    "Accept-Encoding": "gzip, deflate, sdch", 
    "Sec-Websocket-Key": "*******", 
    "Connection": "close", 
    "X-Real-Ip": "172.17.0.1", 
    "Origin": "http://my-domain.com", 
    "Sec-Websocket-Extensions": "permessage-deflate; client_max_window_bits", 
    "X-Forwarded-For": "172.17.0.1", 
    "Sec-Websocket-Version": "13", 
    "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.116 Safari/537.36", 
    "Host": "my-domain.com", 
    "X-Forwarded-Proto": "http", 
    "Cookie": "_ym_uid=1456149826627418608; sessionid=0pn2knqey7raw7w4dst9d7vqvtvsmook; csrftoken=0o1lqSW4XCZvvlvYtN4S7Stt53AyKWf6; __utmx=147844469.bAGHuGeARZy_vppa6db8lg$115132382-10:5.mu9KcCiZSDepsLZ385hDXA$117448007-11:0; __utmxx=147844469.bAGHuGeARZy_vppa6db8lg$115132382-10:1456307221:15552000.mu9KcCiZSDepsLZ385hDXA$117458007-11:1457421562:15552000; _ym_isad=0; _ga=GA1.2.2002408319.1456149825; JSESSIONID=dummy", 
    "Pragma": "no-cache", 
    "Cache-Control": "no-cache"
}

但是从浏览器检查标头显示,连接和升级标头都具有值,这些值应该对龙卷风有效。

来自浏览器的标头:

Accept-Encoding:gzip, deflate, sdch
Accept-Language:en-US,en;q=0.8,ru;q=0.6
Cache-Control:no-cache
Connection:Upgrade
Cookie:_ym_uid=1456149826627418608; sessionid=0pn2knqey7raw7w4dst9d7vqvtvsmook; __utmx=147844469.bAGHuGeARZy_vppa6db8lg$115132382-10:5.mu9KcCiZSDepsLZ385hDXA$117448007-11:0; __utmxx=147844469.bAGHuGeARZy_vppa6db8lg$115132382-10:1456307221:15552000.mu9KcCiZSDepsLZ385hDXA$117448007-11:1457421562:15552000; _ym_isad=0; _ga=GA1.2.2002408319.1456149825; csrftoken=0o1lqSW4XCZvvlvYtN4S7Stt53AyKWf6; JSESSIONID=dummy
Host:my-domain.com
Origin:http://my-domain.com
Pragma:no-cache
Sec-WebSocket-Extensions:permessage-deflate; client_max_window_bits
Sec-WebSocket-Key:/zRJijtAtPjjLNJK49SIDQ==
Sec-WebSocket-Version:13
Upgrade:websocket
User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.116 Safari/537.36

我可能的建议是龙卷风如何/何时解析标头。

上级:使用的软件版本:

1. sockjs-tornado==1.0.2
2. tornado==4.3
3. tornado-redis==2.4.18

+1 给 Riliam 的答案,但我需要添加我的 5 美分,因为这个问题最近又回来了。所以要解决这个问题,如果你在nginx驱动的docker容器前面有Chief nginx,你需要修改你的chief的nginx配置。

/etc/nginx

/nginx.conf

...
map $http_upgrade $connection_upgrade {
    default upgrade;
    ''      close;
}
...
/

etc/nginx/conf.d/virtual-site.conf

8001 – 从 docker 容器公开的端口

location / {
  proxy_pass         http://127.0.0.1:8001;
  proxy_http_version 1.1;
  proxy_set_header Upgrade $http_upgrade;
  proxy_set_header Connection $connection_upgrade;
  proxy_redirect     off;
  proxy_set_header   Host             $host;
  proxy_set_header   X-Real-IP        $remote_addr;
  proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;
}

所以问题在于通过另一个在Docker容器之外运行的Nginx来代理请求。

相关内容

  • 没有找到相关文章

最新更新