Meteor WebSocket握手错误400与nginx

wgmfuz8q  于 2023-08-03  发布在  Nginx
关注(0)|答案(6)|浏览(142)

我设法在我的基础设施(Webfactions)上部署了meteor。应用程序似乎工作正常,但当我的应用程序启动时,我在浏览器控制台中收到以下错误:
第一个月

iecba09b

iecba09b1#

WebSocket速度很快,您不必(也不应该)禁用它们。
此错误的真实的原因是Webfactions使用了nginx,而nginx的配置不正确。下面是如何通过设置proxy_set_header Upgrade $http_upgrade;proxy_set_header Connection $connection_upgrade;来实现correctly configure nginx to proxy WebSocket requests

# 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's nginx config的改进的nginx配置。“安德鲁·毛已经达到了very similar configuration
请记住,还要将HTTP_FORWARDED_COUNT环境变量设置为应用程序前面的代理数量(通常为1)。

byqmnocz

byqmnocz2#

如果你在浏览器控制台的客户端收到这个错误,你可以安全地忽略它-这意味着你的主机不支持websockets,meteor将回退到使用长轮询
meteor应用程序部署到heroku或任何其他平台没有websockets将得到相同的错误
更新:从meteor v0.6.4开始,你现在可以设置环境变量DISABLE_WEBSOCKETS来防止这种尝试发生,如果你知道它会失败的话
https://github.com/meteor/meteor/blob/devel/History.md

If you set the DISABLE_WEBSOCKETS environment variable, browsers will not attempt to connect to your app using Websockets. Use this if you know your server environment does not properly proxy Websockets to reduce connection startup time.

字符串

qyuhtwio

qyuhtwio3#

关于SEO:失败的WebSocket(代码400)也阻止了Phantomjs获得一个体面的页面加载(并且不会被终止)。
在我的例子中,Dan的新Nginx配置防止了websockets的失败,并让Phantomjs加载页面。

llew8vvj

llew8vvj4#

在使用AWS Elastic Load Balancer时搜索此错误时发现了此错误。设置环境变量可以工作,但更好的解决方案是在ELB上使用TCP协议而不是HTTPS。仅供参考

s5a0g9ez

s5a0g9ez5#

我们在托管Rocket Chat时遇到了禁用WebSocket的问题。我们的用户在尝试从iOS和Android的移动的客户端连接到服务器时会收到“WebSocket is disabled for this server”错误。
感谢Dan的帖子,将这几行添加到config帮助了我们:

#websocket support
         proxy_set_header Upgrade $http_upgrade;
         proxy_set_header Connection $connection_upgrade;

字符串
这样,我们的配置就变成了这样:

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

server {
    listen 443 ssl http2;
    server_name our_rocketchat_domain_url.com;

    ssl_certificate         "/etc/letsencrypt/live/our_rocketchat_domain_url.com/fullchain.pem";
    ssl_certificate_key     "/etc/letsencrypt/live/our_rocketchat_domain_url.com/privkey.pem";
    ssl_ciphers our_ciphers_list;
    ssl_prefer_server_ciphers on;
    ssl_protocols TLSv1.2 TLSv1.3;

    access_log /var/log/nginx/our_rocketchat_domain_url.com.access.log;
    error_log /var/log/nginx/our_rocketchat_domain_url.com.error.log warn;

        location / {
         proxy_buffers 16 4k;
         proxy_buffer_size 2k;
         proxy_set_header Host $host;
         proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
         proxy_set_header X-Real-IP $remote_addr;
         proxy_http_version 1.1;
         proxy_pass http://ip_of_our_rocketchat:3000/;
         #websocket support
         proxy_set_header Upgrade $http_upgrade;
         proxy_set_header Connection $connection_upgrade;
    }
}

vmdwslir

vmdwslir6#

解决方案是,你不仅要添加这两行:

proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;

字符串
在你的Nginx配置中,你必须把它们放在这里

location / {
    ...
    ...
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection $connection_upgrade;
}


花了几个小时才弄明白!

相关问题