Meteor WebSocket连接到'ws://.../websocket'失败:WebSocket握手期间出错:意外的响应代码:400

11
我刚刚接触Meteor.JS这种东西,碰到一个问题想请教一下。我启动了测试项目(点击按钮计时),它能正常工作,但是当我进入控制台时,发现出现了以下错误: WebSocket连接到 'ws://shibe.ninja/sockjs/243/5gtde_n9/websocket' 失败:在 WebSocket 握手期间出错:意外的响应代码:400 我不知道如何解决,请帮助一下,谢谢。

我在酒店住宿时遇到了这个问题,必须通过他们的代理连接网络。我的解决方案是使用我的手机作为WiFi热点 - icc97
2个回答

7

也许有点晚了,但以防你仍然卡在这个问题上。当我部署应用并使用nginx作为代理时,我遇到了相同的问题。

location / {
     proxy_pass http://127.0.0.1:3000;
     proxy_http_version 1.1;
     proxy_set_header Upgrade $http_upgrade;
     proxy_set_header Connection "Upgrade";
}

同时也请查看这里的nginx文档:http://nginx.com/blog/websocket-nginx/


3
我遇到了这个问题,但是我已经正确设置了代理头,但仍然无法正常工作。但显然Cloudflare会导致问题。这里有一篇关于这个主题的好文章:https://meteorhacks.com/cloudflare-meets-meteor 据我所知,有三种解决方案:
选项1:使用支持套接字的CloudFlare企业版。
选项2:禁用Meteor WebSockets,这将影响您的性能,因为它回退到使用sock.js作为替代品。要做到这一点,只需像这样设置您的Meteor环境:
export DISABLE_WEBSOCKETS=1

选项3:在Cloudflare中,创建一个ddp子域名用于websocket(ddp.yourdomain.com),然后禁用该子域名的Cloudflare。之后按以下方式设置你的meteor环境:

export DDP_DEFAULT_CONNECTION_URL=http://ddp.example.com

接下来我的nginx配置需要做一些调整,因为现在这已经成为了一个跨域(CORS)设置。这是我的新nginx配置:

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

server {
  listen 80 proxy_protocol;
  listen [::]:80 proxy_protocol;

  server_name mydomain.com ddp.mydomain.com;

  ## This allows the CORS setup to work
  add_header Access-Control-Allow-Origin 'http://example.com';

  ## This hides the CORS setup from the Meteor server
  ## Without this the header is added twice, not sure why?
  proxy_hide_header Access-Control-Allow-Origin;

  ## Idealy the two options above should be disabeled,
  ## Then use this one instead, but that caused issues in my setup.
  # proxy_set_header Access-Control-Allow-Origin 'http://example.com';

  location / {
    proxy_pass http://localhost:3000;
    proxy_set_header Host $host; # pass the host header
    proxy_set_header Upgrade $http_upgrade; # allow websockets
    proxy_set_header Connection $connection_upgrade;
    proxy_set_header X-Real-IP $remote_addr; # Preserve client IP
    proxy_set_header X-Forwarded-For $remote_addr;
    proxy_http_version 1.1;

    # Meteor browser cache settings (the root path should not be cached!)
    if ($uri != '/') {
      expires 30d;
    }
  }
}

最后,记得重新启动nginx。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接