AWS上的ActionCable:WebSocket握手错误:意外的响应代码:404 [英] ActionCable on AWS: Error during WebSocket handshake: Unexpected response code: 404

查看:1193
本文介绍了AWS上的ActionCable:WebSocket握手错误:意外的响应代码:404的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

We are attempting to deploy DHH's simple Rails 5 chat example to a single, self contained EC2 instance on AWS. Code is available here: https://github.com/HectorPerez/chat-in-rails5

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

We used Elastic Beanstalk to spin up a single instance thus:

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提交和部署。

This is a minimal installation, so there is no Elasticache, and no load balancer. To install redis on the EC2 instance we added an .ebextensions config file like this: https://gist.github.com/KeithP/08b38189372b7fd241e5#file-ebextensions-redis-config ; Git commit and deploy.

但是websocket不起作用:检查浏览器控制台,我们看到这个错误一再反复出现:

But the websocket doesnt work: Inspecting the browser console, we see this error repeating over and over:

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显示每个完成/有线呼叫的开始GET /电缆。
ActiveCable中没有DEBUG消息:

The server production.log shows 2 "Started GET /cable" for every "Finished /cable" call. There are no DEBUG messages from ActiveCable:

/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


推荐答案

p>要在一个实例上运行websocket聊天示例在AWS中弹性Beanstalk部署,您需要添加以下Nginx代理配置
(注意:替换env1.t3tiiauce6.us-west-2.elasticbeanstalk.com 你的网站名称):

To run the websocket chat example on a single instance Elastic Beanstalk deployment in AWS, you will need to add the following Nginx proxy configuration ( Note: replace "env1.t3tiiauce6.us-west-2.elasticbeanstalk.com" with your sitename ):

.ebextensions\\\
ginx_proxy.config

.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上的ActionCable:WebSocket握手错误:意外的响应代码:404的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆