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



我是诸如Meteor.js之类的东西,并且想知道这个错误。我启动了测试项目(使用按钮点击仪表),然后奏效,但随后我进入控制台,然后查看 WebSocket connection to 'ws://shibe.ninja/sockjs/243/5gtde_n9/websocket' failed: Error during WebSocket handshake: Unexpected response code: 400我不知道该如何解决。谢谢

也许有点晚了,但如果您仍然坚持下去。在部署应用程序并使用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/

我本人遇到了这个问题,但是我已经有正确的代理标题设置了,但它仍然无法正常工作。但是显然 Cloudflare 正在引起问题。这是一篇关于该主题的精彩文章:https://meteorhacks.com/cloudflare-meets-meteor

据我发现,有三种解决方案:

选项1:使用支持插座的Cloudflare Enterprise。

选项2:禁用Meteor Websockets,这将影响您的性能,因为它会退回后返回sock.js作为替换。为此,只需设置您的流星环境:

export DISABLE_WEBSOCKETS=1

选项3:在Cloudflare中,为Websocket(ddp.yourdomain.com)创建A ddp 子域,然后在新子域上禁用Cloudflare。之后,设置了这样的流星环境:

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。

相关内容

最新更新