与nginx的Meteor WebSocket握手错误400

gpa*_*sse 19 nginx node.js meteor

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

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

Dan*_*scu 54

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

此错误的真正原因是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;
  }

}
Run Code Online (Sandbox Code Playgroud)

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

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

  • 谢谢丹.启用websocket是可行的方法.还要将nginx升级到1.4或更高版本才能获得websocket. (2认同)

nat*_*ser 8

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

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


更新:从meteor 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.
Run Code Online (Sandbox Code Playgroud)

  • 讨厌downvote,但你[不需要禁用Websockets](http://stackoverflow.com/a/22750356/1269037) (5认同)
  • 把它设为1?export DISABLE_WEBSOCKETS = 1? (2认同)
  • 我做 DISABLE_WEBSOCKETS=true - 很确定任何值都适用于流星语 - 只需要设置为环境变量 (2认同)
  • Downvote - 抱歉.这个答案只是被错误地标记为最佳答案而不是真实的答案.更好的答案来自Dan Dascalescu,他描述了如何配置nginx以使Websocket正常工作.请参考这个答案:http://stackoverflow.com/a/22750356/581768 (2认同)