与'ws://.../websocket'的Meteor WebSocket连接失败:WebSocket握手期间出错:意外响应代码:400 [英] Meteor WebSocket connection to 'ws://.../websocket' failed: Error during WebSocket handshake: Unexpected response code: 400

查看:196
本文介绍了与'ws://.../websocket'的Meteor WebSocket连接失败:WebSocket握手期间出错:意外响应代码:400的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对像Meteor.JS这样的东西很陌生,并且想知道这个错误。我启动了测试项目(使用按钮点击计)它可以工作,但后来我进入控制台,看到
WebSocket连接到'ws://shibe.ninja/sockjs/243 / 5gtde_n9 / websocket'失败:WebSocket握手期间出错:意外的响应代码:400
我不知道如何修复它。
谢谢

I am brand new to things like Meteor.JS, and was wondering about this error. I started the test project (with the button click meter) and it works, but then I go into the console and see WebSocket connection to 'ws://shibe.ninja/sockjs/243/5gtde_n9/websocket' failed: Error during WebSocket handshake: Unexpected response code: 400 I don't know how to fix it. Thanks

推荐答案

我自己遇到了这个问题,但我已经正确设置了我的代理标题它仍然无法正常工作。但显然 Cloudflare 正在引发问题。以下是关于此主题的精彩文章: https://meteorhacks.com/cloudflare-meets-meteor

I bumped into this problem myself, but I already had my proxy headers set correctly and it was still not working. But apparently Cloudflare is causing issues. Here is a great article on the subject: https://meteorhacks.com/cloudflare-meets-meteor

据我所知,有三种解决方案:

As far as I've found, there are three solutions:

选项1: 使用支持套接字的CloudFlare企业。

Option 1: Use CloudFlare enterprise, which supports sockets.

选项2:禁用Meteor WebSockets,这会影响您的回退性能使用sock.js作为替换。要做到这一点,只需设置这样的流星环境:

Option 2: Disable Meteor WebSockets, which will affect your performance as it fallbacks back to use sock.js as a replacment. To do this, just set your meteor environment like this:

export DISABLE_WEBSOCKETS=1

选项3:在Cloudflare中,为websocket创建 ddp 子域(ddp.yourdomain) .com),然后在新子域上禁用Cloudflare。之后设置你的流星环境如下:

Option 3: In Cloudflare, create a ddp subdomain for the websocket (ddp.yourdomain.com), then disable Cloudflare on the new subdomain. After that set your meteor environment like this:

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

在此之后我的nginx配置需要一些调整,因为这现在已成为跨源(CORS)设置。这是我的新nginx配置:

After this my nginx config needed some adjustments, as this has now become a cross-origin (CORS) setup. This is my new nginx config:

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 。

Finally, remember to restart nginx.

这篇关于与'ws://.../websocket'的Meteor WebSocket连接失败:WebSocket握手期间出错:意外响应代码:400的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆