【问题标题】:ActionCable on AWS: Error during WebSocket handshake: Unexpected response code: 404AWS 上的 ActionCable:WebSocket 握手期间出错:意外响应代码:404
【发布时间】:2016-04-04 20:45:30
【问题描述】:

我们正在尝试将 DHH 的简单 Rails 5 聊天示例部署到 AWS 上的单个自包含 EC2 实例。代码在这里可用:https://github.com/HectorPerez/chat-in-rails5

因此,我们使用 Elastic Beanstalk 来启动单个实例:

eb create dev-env -p “64bit Amazon Linux 2015.09 v2.0.4 running Ruby
2.2 (Puma)” –single -i t2.micro --envvars
SECRET_KEY_BASE=g5dh9cg614a37d4bdece9126b42d50d0ab8b2fc785daa1e0dac0383d6387f36b

这是一个最小的安装,所以没有 Elasticache,也没有负载均衡器。为了在 EC2 实例上安装 redis,我们添加了一个 .ebextensions 配置文件,如下所示:https://gist.github.com/KeithP/08b38189372b7fd241e5#file-ebextensions-redis-config; Git 提交和部署。

但是 websocket 不起作用:检查浏览器控制台,我们看到这个错误一遍又一遍地重复:

application-a57354de3399cd895ca366df9bd7316ab69e81d266b63be7d7be563ebc78ab9d.js:27 
WebSocket connection to ‘ws://dev-env-y2e5dcrxqk.elasticbeanstalk.com/cable’ failed: 
Error during WebSocket handshake: Unexpected response code: 404

服务器 production.log 为每个“Finished /cable”调用显示 2 个“Started GET /cable”。 没有来自 ActiveCable 的 DEBUG 消息:

/var/app/containerfiles/logs/production.log
-------------------------------------

INFO -- : Processing by RoomsController#show as HTML 
DEBUG -- :   [1m[36mMessage Load (0.1ms)[0m  [1m[34mSELECT "messages".* FROM "messages"[0m INFO -- :   Rendered collection (0.0ms) 
INFO -- :   Rendered rooms/show.html.erb within layouts/application (0.5ms)   
INFO -- : Completed 200 OK in 2ms (Views: 1.2ms | ActiveRecord: 0.1ms) 
INFO -- : Started GET "/cable" for <ip_address> at 2016-01-01 17:28:26 +0000 
INFO -- : Started GET "/cable/" for <ip_address> at 2016-01-01 17:28:26 +0000 
INFO -- : Finished "/cable/" for <ip_address> at 2016-01-01 17:28:26 +0000

【问题讨论】:

  • 我们检查了redis似乎已经安装并启动正常;并在 production.rb 中尝试了“ActionCable.server.config.disable_request_forgery_protection = true”
  • 您是在使用像 nginx 这样的反向代理还是直接连接到运行在端口 80 上的 Rails 应用程序?
  • 已尝试使用和不使用此反向代理配置:gist.github.com/KeithP/f8534c04d20c2b4e4b1d
  • 我设法让它与以下 nginx 配置一起工作(替换你当前的第 35-38 行):gist.github.com/tpbowden/d85b72e5c3bf8ef8e97a
  • 谢谢。我们现在在 nginx/error.log 中得到这个:*1 connect() to unix:///var/www/my_app/tmp/sockets/my_app.sock failed (2: No such file or directory)

标签: amazon-web-services deployment redis ruby-on-rails-5 actioncable


【解决方案1】:

要在 AWS 中的单实例 Elastic Beanstalk 部署上运行 websocket 聊天示例,您需要添加以下 Nginx 代理配置 (注意:用您的站点名称替换“env1.t3tiiauce6.us-west-2.elasticbeanstalk.com”):

.ebextensions/nginx_proxy.config

files:
  "/etc/nginx/conf.d/websockets.conf" :
    content: |
      upstream backend {
          server unix:///var/run/puma/my_app.sock;
      }

  server {
      listen 80;

      access_log /var/log/nginx/access.log;
      error_log /var/log/nginx/error.log;

      server_name env1.t3tiiauce6.us-west-2.elasticbeanstalk.com

      # prevents 502 bad gateway error
      large_client_header_buffers 8 32k;

      location / {
          proxy_set_header X-Real-IP $remote_addr;
          proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
          proxy_set_header Host $http_host;
          proxy_set_header X-NginX-Proxy true;

          # prevents 502 bad gateway error
          proxy_buffers 8 32k;
          proxy_buffer_size 64k;

          proxy_pass http://backend;
          proxy_redirect off;

          location /assets {
            root /var/app/current/public;
          }

          # enables WS support
          location /cable {
            proxy_pass http://backend;
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection "upgrade";
          }
      }
  }

container_commands:
  01restart_nginx:
    command: "nginx -t && service nginx reload"

`

【讨论】:

  • AWS 服务器名称在 2016 年 1 月变长,导致 eb 部署失败消息“nginx: emerg could not build the server_names_hash, you should increase server_names_hash_bucket_size: 64”。作为一种解决方法 - 如果您使环境名称尽可能短,则不会因此而受到影响。例如,使用“dev1”而不是“dev-env”。答案中的配置文件已更新以反映这一点。
  • 文件扩展名是什么?我收到下一个错误
  • 应用程序版本 app-8b83-170330_144118 中的配置文件 .ebextensions/nginx_proxy.config 包含无效的 YAML 或 JSON。 YAML 异常
  • 无效的 YAML 是否正确缩进?在这样的解析器中尝试:yaml-online-parser.appspot.com
  • 谢谢,是缩进问题。但现在我遇到了另一个问题
猜你喜欢
  • 2018-01-09
  • 1970-01-01
  • 2019-09-06
  • 2020-11-24
  • 2015-03-21
  • 2014-05-15
  • 1970-01-01
  • 1970-01-01
  • 2018-02-23
相关资源
最近更新 更多