一尘不染

使用Nginx的流星WebSocket握手错误400

node.js

我设法在我的基础架构(Webfactions)上部署了流星。该应用程序似乎运行良好,但是当我的应用程序启动时,我在浏览器控制台中收到以下错误:

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


阅读 494

收藏
2020-07-07

共1个答案

一尘不染

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

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

}

这是基于David Weldon的nginxconfig的改进的nginx配置。安德鲁·毛(Andrew
Mao)已经达到了非常相似的配置。

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

2020-07-07