AWS上的ActionCable:WebSocket握手期间出错:意外响应代码:404

Kei*_*thP 7 deployment amazon-web-services redis ruby-on-rails-5 actioncable

我们正在尝试将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
Run Code Online (Sandbox Code Playgroud)

这是一个最小的安装,因此没有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
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

服务器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
Run Code Online (Sandbox Code Playgroud)

Kei*_*thP 4

要在 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"
Run Code Online (Sandbox Code Playgroud)

`

  • AWS 服务器名称在 2016 年 1 月变得更长,导致 eb 部署失败消息“nginx: emerg 无法构建 server_names_hash,您应该增加 server_names_hash_bucket_size: 64”。作为一种解决方法 - 如果您使环境名称尽可能短,则不会出现这种情况。例如,使用“dev1”而不是“dev-env”。答案中的配置文件已更新以反映这一点。 (2认同)