node.js – Meteor WebSocket握手错误400与nginx

前端之家收集整理的这篇文章主要介绍了node.js – Meteor WebSocket握手错误400与nginx前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

我设法在我的基础设施(Webfactions)上部署流星.
应用程序似乎工作正常,但是当我的应用程序启动时,我在浏览器控制台中收到以下错误

WebSocket连接到’ws://…/websocket’失败:WebSocket握手中出错:意外响应代码:400

最佳答案
WebSockets很快,你不必(不应该)禁用它们.

这个错误的真正原因是Webfactions使用Nginx,并且Nginx配置不正确.这里是如何correctly configure nginx to proxy WebSocket requests,通过设置proxy_set_header升级$http_upgrade;和proxy_set_header Connection $connection_upgrade ;:

  1. # we're in the http context here
  2. map $http_upgrade $connection_upgrade {
  3. default upgrade;
  4. '' close;
  5. }
  6. # the Meteor / Node.js app server
  7. server {
  8. server_name yourdomain.com;
  9. access_log /etc/Nginx/logs/yourapp.access;
  10. error_log /etc/Nginx/logs/yourapp.error error;
  11. location / {
  12. proxy_pass http://localhost:3000;
  13. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  14. proxy_set_header Host $host; # pass the host header - http://wiki.Nginx.org/HttpProxyModule#proxy_pass
  15. proxy_http_version 1.1; # recommended with keepalive connections - http://Nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_http_version
  16. # WebSocket proxying - from http://Nginx.org/en/docs/http/websocket.html
  17. proxy_set_header Upgrade $http_upgrade;
  18. proxy_set_header Connection $connection_upgrade;
  19. }
  20. }

这是基于David Weldon’s nginx config的改进的Nginx配置.安卓毛泽东已经达到了very similar configuration.

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

猜你在找的Nginx相关文章