0

Ich versuche, eine WebSockets aktiviert App zum Laufen zu bringen, aber ich bin in einen Fehler läuft beim Verbindungsversuch:Websocket Schienen (Faye-websocket) Handshake-Fehlercode 200 - AWS ElasticBeanstalk, nginx, puma

WebSocket connection to 'ws://[domain.com]/' failed: Error during WebSocket handshake: Unexpected response code: 200 

Ich folge dem Tutorial hier (https://devcenter.heroku.com/articles/ruby-websockets), die den Edelstein Faye-Websocket verwendet. Es funktioniert gut lokal (mit Puma), aber wenn ich versuche, es in AWS ElasticBeanstalk, die Nginx + Puma läuft bereitstellen, kann die Websocket-Anfrage keine Verbindung herstellen.

ich eine neue WebSocket-Verbindung mit dem folgende Javascript bin zu schaffen (mit meiner eigenen Domain):

var ws = new WebSocket("ws://[domain.com]/"); 


Nach dem Tutorial, habe ich eine Middleware-Klasse, die für websocket eingehende Anfragen überprüft, und Griffe es entsprechend. Auf meinem lokalen Server sieht die Anfrageumgebung Hash wie folgt aus:

"rack.errors" => #>, "rack.multithread" => true "rack.multiprocess" => false, „Rack. run_once "=> false," SCRIPT_NAME "=>" "," CONTENT_TYPE "=>" text/plain "," QUERY_STRING "=>" "," SERVER_PROTOCOL "=>" HTTP/1.1 "," SERVER_SOFTWARE "=>" 2.11.1 "," GATEWAY_INTERFACE "=>" CGI/1.2 "," REQUEST_METHOD "=>" GET "," REQUEST_PATH "=>"/"," REQUEST_URI "=>"/"," HTTP_VERSION "=>" HTTP /1.1 "," HTTP_HOST "=>" localhost: 3000 "," HTTP_CONNECTION "=>" Aktualisierung "," HTTP_PRAGMA "=>" no-cache "," HTTP_CACHE_CONTROL "=>" no-cache "," HTTP_UPGRADE "= > "websocket", "HTTP_ORIGIN" => "http://localhost:3000", "HTTP_SEC_WEBSOCKET_VERSION" => "13", "HTTP_USER_AGENT" => "Mozilla/5.0 (Macintosh; Intel MacOS X 10_10_5) AppleWebKit/537.36 (KHTML, wie Gecko) Chrome/50.0.2661.102 Safari/537.36 "," HTTP_ACCEPT_ENCODING "=>" gz ip, abzulassen, sdch“, "HTTP_ACCEPT_LANGUAGE"=> "en-US, en; q = 0,8",

Während auf AWS es sieht wie folgt aus:

{ "rack.version" => [1, 3], "rack.errors" => #, "rack.multithread" => true, "rack.multiprocess" => false, "rack.run_once" => false, "SCRIPT_NAME" => " "," CONTENT_TYPE "=>" text/plain "," QUERY_STRING "=>" "," SERVER_PROTOCOL "=>" HTTP/1.1 "," SERVER_SOFTWARE "=>" 2.11.1 "," GATEWAY_INTERFACE "=>" CGI /1.2 "," REMOTE_ADDR "=>" 127.0.0.1 "," REQUEST_METHOD "=>" GET "," REQUEST_PATH "=>"/"," REQUEST_URI "=>"/"," HTTP_VERSION "=>" HTTP/1.0 "," HTTP_HOST "=>" [domain.com] "," HTTP_X_FORWARDED_FOR "=>" 194.80.196.162 "," HTTP_CONNECTION "=>" Schließen "," HTTP_PRAGMA "=>" no-cache "," HTTP_CACHE _CONTROL "=>" kein Cache "," HTTP_ORIGIN "=>" http://[domain.com] "," HTTP_SEC_WEBSOCKET_VERSION "=>" 13 "," HTTP_USER_AGENT "=>" Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) AppleWebKit/537.36 (KHTML, wie Gecko) Chrome/50.0.2661.102 Safari/537.36 "," HTTP_ACCEPT_ENCODING "=>" gzip, deflate, sdch "," HTTP_ACCEPT_LANGUAGE "=>" de-DE, de; q = 0,8" ,

So kann ich das auf AWS sehen, die Request-Header HTTP_CONNECTION und HTTP_UPGRADE werden auf WebSockets nicht umgeschaltet.

Allerdings habe ich versucht, meine nginx-Konfiguration einrichten, aber ich kann nicht herausfinden, wie es (zB https://gist.github.com/KeithP/f8534c04d20c2b4e4b1d)

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"; 
       } 
      } 
     } 


arbeitetVon dem, was ich verstehe, routet dies Anforderungen in Richtung/Kabel zur Backend-Rails-App und aktualisiert die Verbindung zu Websockets. Aber wenn ich/cable versuche, bekomme ich einen 404 Fehler (da es keine passende Route gibt).

Wie kann ich meine Rails App Handshake mit Websockets richtig bekommen? Jede Hilfe würde sehr geschätzt werden !! Ich habe auch versucht, die WebSocket-Schienen gem und bekam Handshake-Fehler-Code 500

Antwort

1

Endlich geschafft, es zur Arbeit zu bekommen! Am Ende bekam die folgende Datei in den Ordner .ebextensions legte es gehen:

server { 
    listen 80; 
    server_name localhost; # need to listen to localhost for worker tier 

    location/{ 
    proxy_pass http://my_app; # match the name of upstream directive which is defined in another file 
    proxy_set_header Host $host; 
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 
    } 

    location /websocket { 
    proxy_pass http://my_app/websocket; 
    proxy_set_header Host $host; 
    proxy_http_version 1.1; 
    proxy_set_header Upgrade $http_upgrade; 
    proxy_set_header Connection Upgrade; 
    } 

    location /assets { 
    alias /var/app/current/public/assets; 
    gzip_static on; 
    gzip on; 
    expires max; 
    add_header Cache-Control public; 
    } 

    location /public { 
    alias /var/app/current/public; 
    gzip_static on; 
    gzip on; 
    expires max; 
    add_header Cache-Control public; 
    } 
} 

Und mein Javascript geändert:

var ws = new WebSocket("ws://[domain.com]/websocket"); 

Hoffe, dass es jeder, der über ähnliche Probleme kommt hilft!

Verwandte Themen