25

我设法在我的基础设施(Webfactions)上部署了流星。该应用程序似乎工作正常,但是当我的应用程序启动时,我在浏览器控制台中收到以下错误:

WebSocket connection to 'ws://.../websocket' failed: Error during WebSocket handshake: Unexpected response code: 400

4

4 回答 4

68

WebSocket 速度很快,您不必(也不应该)禁用它们。

这个错误的真正原因是Webfactions使用了nginx,而nginx配置不当。以下是如何通过设置和正确配置 nginx 以代理 WebSocket 请求proxy_set_header Upgrade $http_upgrade;proxy_set_header Connection $connection_upgrade;

# we're in the http context here
map $http_upgrade $connection_upgrade {
  default upgrade;
  ''      close;
}

# the Meteor / Node.js app server
server {
  server_name yourdomain.com;

  access_log /etc/nginx/logs/yourapp.access;
  error_log /etc/nginx/logs/yourapp.error error;

  location / {
    proxy_pass http://localhost:3000;

    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $host;  # pass the host header - http://wiki.nginx.org/HttpProxyModule#proxy_pass

    proxy_http_version 1.1;  # recommended with keepalive connections - http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_http_version

    # WebSocket proxying - from http://nginx.org/en/docs/http/websocket.html
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection $connection_upgrade;
  }

}

这是基于David Weldon 的 nginx 配置改进的 nginx 配置。Andrew Mao 也达到了非常相似的配置

请记住还要将HTTP_FORWARDED_COUNT环境变量设置为应用程序前面的代理数量(通常为 1)。

于 2014-03-30T21:52:19.483 回答
8

如果您在浏览器控制台中收到此错误客户端,您可以放心地忽略它 - 这意味着您的主机不支持 websockets 并且流星将回退到使用长轮询

部署到 heroku 或任何其他没有 websockets 的平台的流星应用程序将得到相同的错误


更新:从流星 v0.6.4 开始,您现在可以设置环境变量DISABLE_WEBSOCKETS以防止发生这种尝试,如果您知道它会失败

https://github.com/meteor/meteor/blob/devel/History.md

If you set the DISABLE_WEBSOCKETS environment variable, browsers will not attempt to connect to your app using Websockets. Use this if you know your server environment does not properly proxy Websockets to reduce connection startup time.
于 2013-06-10T00:18:26.440 回答
1

关于 SEO:失败的 websocket(代码 400)也阻止了 Phantomjs 获得体面的页面加载(并且不会被终止)。

在我的例子中,来自 Dan 的新 Nginx 配置可以防止 websockets 失败并让 Phantomjs 加载页面。

于 2014-08-17T17:27:25.173 回答
1

在使用 AWS Elastic Load Balancer 时搜索此错误时发现了这一点。设置环境变量是可行的,但更好的解决方案是在 ELB 上使用 TCP 协议而不是 HTTPS。供参考。

于 2015-03-17T20:29:29.507 回答