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

我是像Meteor.JS这样的全新产品,并且对这个错误感到疑惑。 我开始testing项目(与button点击米),它的工作原理,但然后我进入控制台,并看到WebSocket connection to 'ws://shibe.ninja/sockjs/243/5gtde_n9/websocket' failed: Error during WebSocket handshake: Unexpected response code: 400我不知道如何解决它。 谢谢

我自己碰到这个问题,但我已经有我的代理标题设置正确 ,它仍然无法正常工作。 但显然Cloudflare是造成问题。 这里是关于这个主题的一个伟大的文章: https : //meteorhacks.com/cloudflare-meets-meteor

据我所知,有三个解决scheme:

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

选项2:禁用meteorWebSockets,这会影响你的performance,因为它回退到使用sock.js作为替代。 要做到这一点,只需像这样设置你的meteor环境:

 export DISABLE_WEBSOCKETS=1 

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

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

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

 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。

也许有点晚,但万一你仍然坚持这一点。 在部署应用程序并使用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/