7

Chúng tôi đang cố gắng triển khai ví dụ trò chuyện Rails 5 đơn giản của DHH thành một cá thể EC2 duy nhất, tự chứa trên AWS. Mã có sẵn ở đây: https://github.com/HectorPerez/chat-in-rails5ActionCable on AWS: Lỗi trong quá trình bắt tay WebSocket: Mã phản hồi không mong muốn: 404

Chúng tôi sử dụng đàn hồi Beanstalk để quay về một trường hợp duy nhất như sau:

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 

Đây là cài đặt tối thiểu, vì vậy không có Elasticache, và không cân bằng tải. Để cài đặt redis trên phiên bản EC2, chúng tôi đã thêm tệp cấu hình .ebextensions như sau: https://gist.github.com/KeithP/08b38189372b7fd241e5#file-ebextensions-redis-config; Git cam kết và triển khai.

Nhưng công việc WebSocket doesnt: Kiểm tra trình duyệt giao diện điều khiển, chúng ta thấy lỗi này lặp đi lặp lại nhiều lần:

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

enter image description here

Các production.log máy chủ cho thấy 2 "Bắt đầu GET/cáp" cho mọi cuộc gọi "Hoàn thành/cáp". Không có tin nhắn DEBUG từ 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 
+0

Chúng tôi đã kiểm tra rằng redis dường như đã cài đặt và bắt đầu ok; và đã thử 'ActionCable.server.config.disable_request_forgery_protection = true' trong production.rb – KeithP

+0

Bạn đang sử dụng proxy ngược lại như nginx hay kết nối trực tiếp với ứng dụng đường ray chạy trên cổng 80? – tpbowden

+0

Đã thử có và không có cấu hình proxy ngược này: https://gist.github.com/KeithP/f8534c04d20c2b4e4b1d – KeithP

Trả lời

4

Để chạy các ví dụ WebSocket trò chuyện trên một trường hợp duy nhất triển khai Elastic cây đậu trong AWS, bạn sẽ cần phải thêm sau Nginx định cấu hình proxy (Lưu ý: thay thế "env1. t3tiiauce6.us-west-2.elasticbeanstalk.com" với sitename của bạn):

.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" 

'

+2

Tên máy chủ AWS lâu hơn vào tháng 1 năm 2016, dẫn đến thông báo lỗi triển khai eb "nginx: xuất hiện không thể xây dựng server_names_hash, bạn nên tăng server_names_hash_bucket_size: 64". Như một cách giải quyết - nếu bạn làm cho tên môi trường của bạn càng ngắn càng tốt, nó sẽ không rơi vào tình trạng này. Ví dụ: sử dụng 'dev1' thay vì 'dev-env'. Tệp cấu hình trong câu trả lời đã được cập nhật để phản ánh điều này. – KeithP

+0

tiện ích mở rộng tệp là gì? Tôi đang gặp lỗi tiếp theo – jasmo2

+0

Tệp cấu hình .ebextensions/nginx_proxy.config trong phiên bản ứng dụng app-8b83-170330_144118 chứa YAML hoặc JSON không hợp lệ. YAML ngoại lệ – jasmo2

Các vấn đề liên quan