流星 WebSocket 与 nginx 握手错误 400 [英] Meteor WebSocket handshake error 400 with nginx
问题描述
我设法在我的基础设施(Webfactions)上部署了meteor.该应用程序似乎工作正常,但当我的应用程序启动时,我在浏览器控制台中收到以下错误:
I managed to deploy meteor on my infrastructure (Webfactions). The application seems to work fine but I get the following error in the browser console when my application starts:
WebSocket 连接到ws://.../websocket"失败:WebSocket 握手期间出错:意外响应代码:400
推荐答案
WebSockets 速度很快,您不必(也不应该)禁用它们.
WebSockets are fast and you don't have to (and shouldn't) disable them.
这个错误的真正原因是Webfactions使用了nginx,nginx配置不当.以下是如何通过设置 proxy_set_header 来正确配置 nginx 以代理 WebSocket 请求升级 $http_upgrade;
和 proxy_set_header 连接 $connection_upgrade;
:
The real cause of this error is that Webfactions uses nginx, and nginx was improperly configured. Here's how to correctly configure nginx to proxy WebSocket requests, by setting proxy_set_header Upgrade $http_upgrade;
and 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 的 nginx 配置 改进的 nginx 配置.Andrew Mao 已经达到了非常相似的配置.
This is an improved nginx configuration based on David Weldon's nginx config. Andrew Mao has reached a very similar configuration.
记得还要将 HTTP_FORWARDED_COUNT 环境变量设置为前面的代理数量应用程序(通常为 1).
Remember to also set the HTTP_FORWARDED_COUNT environment variable to the number of proxies in front of the app (usually 1).
这篇关于流星 WebSocket 与 nginx 握手错误 400的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!