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

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

问题描述

我们正在尝试将 DHH 的简单 Rails 5 聊天示例部署到 AWS 上的单个自包含 EC2 实例.代码可在此处获得:

服务器 production.log 为每个完成的/cable"调用显示 2 个Started GET/cable".没有来自 ActiveCable 的调试消息:

/var/app/containerfiles/logs/production.log-------------------------------------信息 -- : 由 RoomsController#show as HTML 处理调试 -- : [1m[36mMessage Load (0.1ms)[0m [1m[34mSELECT "messages".* FROM "messages"[0m INFO -- : 渲染集合 (0.0ms)信息 -- :在布局/应用程序中渲染的房间/show.html.erb(0.5ms)信息 -- : 在 2ms 内完成 200 OK (Views: 1.2ms | ActiveRecord: 0.1ms)信息 -- : 开始 GET "/cable" for <ip_address>在 2016-01-01 17:28:26 +0000信息 -- : 开始 GET "/cable/" for <ip_address>在 2016-01-01 17:28:26 +0000信息 -- :  的/cable/"已完成;在 2016-01-01 17:28:26 +0000

解决方案

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

.ebextensions/nginx_proxy.config

 文件:/etc/nginx/conf.d/websockets.conf":内容: |上游后端{服务器 unix:///var/run/puma/my_app.sock;}服务器 {听80;access_log/var/log/nginx/access.log;error_log/var/log/nginx/error.log;server_name env1.t3tiiauce6.us-west-2.elasticbeanstalk.com# 防止 502 错误网关错误large_client_header_buffers 8 32k;地点/{proxy_set_header X-Real-IP $remote_addr;proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;proxy_set_header 主机 $http_host;proxy_set_header X-NginX-Proxy true;# 防止 502 错误网关错误proxy_buffers 8 32k;proxy_buffer_size 64k;proxy_pass http://backend;proxy_redirect 关闭;位置/资产{根/var/app/current/public;}# 启用 WS 支持位置/电缆{proxy_pass http://backend;proxy_http_version 1.1;proxy_set_header 升级 $http_upgrade;proxy_set_header 连接升级";}}}容器命令:01restart_nginx:命令:nginx -t && service nginx reload"

`

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

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

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.

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

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

解决方案

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/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天全站免登陆